-
Notifications
You must be signed in to change notification settings - Fork 166
Change prod emails from mailchimp to SES #1651
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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 | ||
| end | ||
|
|
||
| alias deliver! deliver | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Fixed the |
||
| end | ||
| end | ||
| end | ||
| end | ||
| 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 |
There was a problem hiding this comment.
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.