Skip to content

Set all mail attributes including template #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [2.4.1] - 2025-08-21
- Set `template_uuid` and `template_variables` when building mail from `Mail::Message`

## [2.4.0] - 2025-08-04

- Add Email Templates API
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
mailtrap (2.4.0)
mailtrap (2.4.1)
base64

GEM
Expand Down
19 changes: 11 additions & 8 deletions lib/mailtrap/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,21 @@ def batch_base_from_content( # rubocop:disable Metrics/ParameterLists
# Builds a mail object from Mail::Message
# @param message [Mail::Message]
# @return [Mailtrap::Mail::Base]
def from_message(message) # rubocop:disable Metrics/AbcSize
def from_message(message)
Mailtrap::Mail::Base.new(
from: prepare_addresses(address_list(message['from'])&.addresses).first,
to: prepare_addresses(address_list(message['to'])&.addresses),
cc: prepare_addresses(address_list(message['cc'])&.addresses),
bcc: prepare_addresses(address_list(message['bcc'])&.addresses),
from: prepare_addresses(message['from']).first,
to: prepare_addresses(message['to']),
cc: prepare_addresses(message['cc']),
bcc: prepare_addresses(message['bcc']),
subject: message.subject,
text: prepare_text_part(message),
html: prepare_html_part(message),
headers: prepare_headers(message),
attachments: prepare_attachments(message.attachments),
category: message['category']&.unparsed_value,
custom_variables: message['custom_variables']&.unparsed_value
custom_variables: message['custom_variables']&.unparsed_value,
template_uuid: message['template_uuid']&.unparsed_value,
template_variables: message['template_variables']&.unparsed_value
)
end

Expand All @@ -219,8 +221,9 @@ def address_list(header)
header.respond_to?(:element) ? header.element : header.address_list
end

# @param addresses [Array<Mail::Address>, nil]
def prepare_addresses(addresses)
# @param header [Mail::Field, nil]
def prepare_addresses(header)
addresses = address_list(header)&.addresses
Array(addresses).map { |address| prepare_address(address) }
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mailtrap/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Mailtrap
VERSION = '2.4.0'
VERSION = '2.4.1'
end
12 changes: 12 additions & 0 deletions spec/mailtrap/mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@
its(:from) { is_expected.to be_nil }
end

context 'when template is set' do
let(:message_params) do
super().merge(
template_uuid: 'c0746b78-f422-46ce-bce5-6f52ad5aab7f',
template_variables: { first_name: 'John' }
)
end

its(:template_uuid) { is_expected.to eq('c0746b78-f422-46ce-bce5-6f52ad5aab7f') }
its(:template_variables) { is_expected.to eq('first_name' => 'John') }
end

%i[from to cc bcc].each do |header|
context "when '#{header}' is invalid" do
let(:message_params) { super().merge(header => 'invalid [email protected]') }
Expand Down