From 77d1817ad876e550a3284726cf35a68392589d73 Mon Sep 17 00:00:00 2001 From: Jonas Brusman Date: Wed, 11 Sep 2024 10:39:03 +0200 Subject: [PATCH 1/2] Support different message_id domains for different AWS SES regions Emails sent from, for example, the eu-west-1 region will have a different message_id, ending in `eu-west-1.amazonses.com` instead of `email.amazonses.com`. --- lib/mail/ses.rb | 5 +++-- spec/mail_ses_spec.rb | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/mail/ses.rb b/lib/mail/ses.rb index b83a5be..1cba7e0 100644 --- a/lib/mail/ses.rb +++ b/lib/mail/ses.rb @@ -23,7 +23,7 @@ def initialize(options = {}) @error_handler = options.delete(:error_handler) raise ArgumentError.new(':error_handler must be a Proc') if @error_handler && !@error_handler.is_a?(Proc) - @settings = { return_response: options.delete(:return_response) } + @settings = { return_response: options.delete(:return_response), message_id_domain: options.delete(:message_id_domain) } options[:credentials] = Aws::InstanceProfileCredentials.new if options.delete(:use_iam_profile) @client = Aws::SESV2::Client.new(options) @@ -43,7 +43,8 @@ def deliver!(message, options = {}) begin response = client.send_email(send_options) - message.message_id = "#{response.to_h[:message_id]}@email.amazonses.com" + message_id_domain = settings[:message_id_domain] || "email.amazonses.com" + message.message_id = "#{response.to_h[:message_id]}@#{message_id_domain}" settings[:return_response] ? response : self rescue StandardError => e handle_error(e, send_options) diff --git a/spec/mail_ses_spec.rb b/spec/mail_ses_spec.rb index 38f8445..3578ff9 100644 --- a/spec/mail_ses_spec.rb +++ b/spec/mail_ses_spec.rb @@ -26,7 +26,7 @@ describe '#settings' do it do expect(ses).to respond_to(:settings, :settings=) - expect(ses.settings).to eq(return_response: nil) + expect(ses.settings).to eq(return_response: nil, message_id_domain: nil) end end @@ -99,9 +99,19 @@ end end - it 'sets mail.message_id' do - ses.deliver!(mail) - expect(mail.message_id).to eq('OutboundMessageId@email.amazonses.com') + context "without message_id_domain config" do + it 'sets the default domain as mail.message_id' do + ses.deliver!(mail) + expect(mail.message_id).to eq('OutboundMessageId@email.amazonses.com') + end + end + + context "with message_id_domain config" do + let(:ses_options) { { stub_responses: true, message_id_domain: 'eu-west-1.amazonses.com' } } + it 'sets as mail.message_id with the configured domain' do + ses.deliver!(mail) + expect(mail.message_id).to eq('OutboundMessageId@eu-west-1.amazonses.com') + end end it 'returns the AWS response' do From 2d7e406bf1f1c4f4fcaf045953277e5e05ee39bf Mon Sep 17 00:00:00 2001 From: Jonas Brusman Date: Wed, 11 Sep 2024 10:49:28 +0200 Subject: [PATCH 2/2] Format according to Rubocop --- lib/mail/ses.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/mail/ses.rb b/lib/mail/ses.rb index 1cba7e0..b18257a 100644 --- a/lib/mail/ses.rb +++ b/lib/mail/ses.rb @@ -23,7 +23,10 @@ def initialize(options = {}) @error_handler = options.delete(:error_handler) raise ArgumentError.new(':error_handler must be a Proc') if @error_handler && !@error_handler.is_a?(Proc) - @settings = { return_response: options.delete(:return_response), message_id_domain: options.delete(:message_id_domain) } + @settings = { + return_response: options.delete(:return_response), + message_id_domain: options.delete(:message_id_domain) + } options[:credentials] = Aws::InstanceProfileCredentials.new if options.delete(:use_iam_profile) @client = Aws::SESV2::Client.new(options) @@ -43,8 +46,7 @@ def deliver!(message, options = {}) begin response = client.send_email(send_options) - message_id_domain = settings[:message_id_domain] || "email.amazonses.com" - message.message_id = "#{response.to_h[:message_id]}@#{message_id_domain}" + message.message_id = "#{response.to_h[:message_id]}@#{settings[:message_id_domain] || 'email.amazonses.com'}" settings[:return_response] ? response : self rescue StandardError => e handle_error(e, send_options)