Skip to content

Commit

Permalink
Support SES options via mail message headers
Browse files Browse the repository at this point in the history
This makes it possible to set new options (like `email_tags`) for each individual email message.
  • Loading branch information
himynameisjonas committed Oct 29, 2024
1 parent 6cc0f32 commit b59053d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/mail/ses/options_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize(message, options = {})

# Returns the options for Aws::SESV2::Client#send_email.
def build
message_options.merge(ses_options)
message_options.merge(ses_options, ses_options_from_message)
end

private
Expand All @@ -32,6 +32,17 @@ def ses_options
slice_hash(@options, *SES_FIELDS)
end

def ses_options_from_message
mail_options = @message[:mail_options]&.unparsed_value

if mail_options
@message[:mail_options] = nil
slice_hash(mail_options, *SES_FIELDS)
else
{}
end
end

def message_options
{
from_email_address: extract_value(:from)&.first,
Expand Down
36 changes: 36 additions & 0 deletions spec/options_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@
it { expect(subject).to eq(exp) }
end

context "with options on the mail object" do
let(:mail) do
Mail.new do
from '"My From" <[email protected]>'
reply_to ["[email protected]", "", "My Reply-To <[email protected]>"]
to ["[email protected]", "My To <[email protected]>", ""]
cc ["", "[email protected]", "My CC <[email protected]>"]
bcc ["My BCC <[email protected]>", "", "[email protected]"]
body "This is the body"
headers(mail_options: {email_tags: [{name: "Foo", value: "Bar"}]})
end
end

let(:exp) do
{
from_email_address: "My From <[email protected]>",
reply_to_addresses: ["[email protected]", "My Reply-To <[email protected]>"],
destination: {
to_addresses: ["[email protected]", "My To <[email protected]>"],
cc_addresses: ["[email protected]", "My CC <[email protected]>"],
bcc_addresses: ["My BCC <[email protected]>", "[email protected]"]
},
email_tags: [
{name: "Foo", value: "Bar"}
],
content: {
raw: {
data: "Fixed message body"
}
}
}
end

it { expect(subject).to eq(exp) }
end

context "when addresses contain non-ascii chars" do
let(:mail) do
Mail.new do
Expand Down

0 comments on commit b59053d

Please sign in to comment.