Skip to content
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
1 change: 1 addition & 0 deletions .reek
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DuplicateMethodCall:
FeatureEnvy:
exclude:
- ActiveJob::Logging::LogSubscriber#json_for
- Aws::SES::Base#deliver
- track_registration
- append_info_to_payload
- generate_slo_request
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ GEM
erubi (1.6.1)
erubis (2.7.0)
eventmachine (1.2.5)
excon (0.57.0)
excon (0.58.0)
execjs (2.7.0)
factory_girl (4.8.0)
activesupport (>= 3.0.0)
Expand Down
2 changes: 1 addition & 1 deletion config/application.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ production:
lockout_period_in_minutes: '10'
logins_per_ip_limit: '20'
logins_per_ip_period: '8'
mandrill_api_token: '123abc'
mandrill_api_token: '' # Empty string to switch to SES
newrelic_license_key: 'xxx'
newrelic_browser_key: ''
newrelic_browser_app_id: ''
Expand Down
4 changes: 3 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
config.action_mailer.default_options = { from: Figaro.env.email_from }
config.action_mailer.delivery_method = if Figaro.env.disable_email_sending == 'true'
:test
else
elsif Figaro.env.mandrill_api_token.present?
:mandrill
else
:ses
end

routes.default_url_options[:protocol] = :https
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/ses_delivery_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require Rails.root.join('lib', 'aws', 'ses.rb')
ActionMailer::Base.add_delivery_method :ses, Aws::SES::Base
24 changes: 24 additions & 0 deletions lib/aws/ses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
##
# ActionMailer delivery method for SES inspired by https://github.com/drewblas/aws-ses
#
module Aws
module SES
class Base
def initialize(*); end

def deliver(mail)
response = ses_client.send_raw_email(raw_message: { data: mail.to_s })
mail.message_id = "#{response.message_id}@email.amazonses.com"
response
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to log some message when we're about to send an email and with some data about the response we get back. The current logs are super silent, which makes diagnosing problems difficult.

end

alias deliver! deliver
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice trick to get around the reek complaint


private

def ses_client
@ses_client ||= Aws::SES::Client.new(region: Figaro.env.aws_region)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need to provide any creds here since we can use AWS instance profiles.

With that in mind, we'll want to hold off on merging this until we have an issue resolved with devops to grant the necessary SES permissions to the IAM role associated with the EC2 instance profiles.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to log a message here that we're instantiating a new SES client with the given region. Ran into errors in testing this because Figaro.env.aws_region was set to "not-used-yet".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end
end
end
end
41 changes: 41 additions & 0 deletions spec/lib/aws/ses_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'

describe Aws::SES::Base do
let(:mail) do
Mail.new(
to: 'asdf@example.com',
cc: 'ghjk@example.com',
body: 'asdf1234'
)
end
let(:ses_response) do
response = double
allow(response).to receive(:message_id).and_return('123abc')
response
end
let(:ses_client) { instance_double(Aws::SES::Client) }

before do
allow(ses_client).to receive(:send_raw_email).and_return(ses_response)
allow(Aws::SES::Client).to receive(:new).and_return(ses_client)
end

describe '#deliver!' do
it 'sends the message to the correct recipients' do
raw_mail_data = mail.to_s

subject.deliver!(mail)

expect(ses_client).to have_received(:send_raw_email).with(
raw_message: {
data: raw_mail_data,
}
)
end

it 'sets the message id on the mail argument' do
subject.deliver!(mail)
expect(mail.message_id).to eq('123abc@email.amazonses.com')
end
end
end