I am using Rails 4.2, the AWS-SES gem and the Mailform gem. I am trying to set up AWS SES in development and have added this to config/development.rb
:
# Configure mail using AWS SES
config.after_initialize do
ActionMailer::Base.delivery_method = :amazon_ses
ActionMailer::Base.custom_amazon_ses_mailer = AWS::SES::Base.new(
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:access_key_id => ENV['AWS_SECRET_KEY_ID'],
:server => 'email.eu-west-2.amazonaws.com'
)
end
When I attempt to send emails from the console, I am getting a timeout after 30 seconds. I started to write all this up asking for help, but then it occurred to me that MailForm
may not be derived from ActionMailer
. Sure enough, MailForm::Base
has superclass Object
, so configuring ActionMailer
is pointless.
I changed these two lines to configure MailForm::Base
, but I still get a timeout. Is it possible that these two gems are not compatible? Otherwise, any suggestions to either resolve or troubleshoot would be appreciated.
As I mentioned in my question, the MailForm
and AWS-SES
gems are not compatible out of the box. It is possible that they can be made to work together but I took a different route.
Some keys to setting up AWS-SES
(code included below for reference):
- AWS set up - with AWS you start off in sandbox mode. You need to register all of your destination email addresses in the SES console for anything to work. Click on the
Email Addresses
link to list your verified addresses and add more. Also, you will need to set up AWS IAM
credentials to use with the gem. When you do this, make sure the user has the SES Full Access managed policy attached (on the IAM console).
:server
setting - AWS operates in multiple regions but your SES account will be set up in one of them. To determine your region, go to the AWS console and click on SES. You will see your region in the URL - for me it is region=us-west-2
. I recommend setting up an initializer as described in Dan Croak's excellent article. I did it just as Dan recommended, except that I set the delivery method to :amazon-ses
and added a server configuration line.
- Configuration - Dan's article (mentioned above) explains how to set
delivery_method
in your environment configuration file. Again, I used :amazon-ses
.
- Once you have AWS configured and your gem installed, you can test your setup in the rails console. Much easier to troubleshoot there than in your code base.
- Somewhat unrelated, but I used the
Dotenv
gem to manage my environment settings. In a nutshell, once you install the gem, you can stick all of your environment settings in ~/.env
and have access to them in ENV
throughout your code.
/config/initializers/amazon-ses.rb
ActionMailer::Base.add_delivery_method :amazon_ses, AWS::SES::Base,
:access_key_id => ENV['AWS_SECRET_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:server => 'email.us-west-2.amazonaws.com'
/config/environments/development.rb (excerpts):
# Configure mailer for development test
config.action_mailer.raise_delivery_errors = true
# Configure mail using AWS SES
config.action_mailer.delivery_method = :amazon_ses
# Configure URL options
host = 'www.example.com'
config.action_mailer.default_url_options = { host: host }
Of course, to make this work in production, you'll need to make these changes to /config/environments/production.rb
. You'll also need to make your AWS secret settings on your production server. If you are using Heroku:
$ heroku config:add AWS_SECRET_KEY_ID=12345XYZ
$ heroku config:add AWS_SECRET_ACCESS_KEY=67890ABC