giant robots smashing into other giant robots

We are thoughtbot. We make web & mobile apps.

Tagged:

Comments (View)

Delivering email with Amazon SES in a Rails 3 app

We’ve been using and loving Sendgrid on all our apps. However, Amazon SES came out last week and… you know… shiny.

Why use Amazon SES?

Right now, price. At our current email rates, we would save more than $10,000 in 2011 using Amazon SES over Sendgrid for Hoptoad.

However, Sendgrid’s a reliable entity with more features (analytics, spam reports, etc.) so even with that dollar figure staring us in the face, we’re not jumping ship quite yet on Hoptoad.

In the meantime, we’re trying Amazon SES on another project that is in private beta to see how well it performs in terms of deliverability, blacklisting, etc.

Already plenty of open source libraries

A week after Amazon announced the service, there were plenty of libraries on Github for Amazon SES. I chose to use drewblas/aws-ses (the aws-ses gem) for the usual reasons:

  • It works.
  • It has a decent-looking test suite that makes me believe it will keep working.

It’s got some fairly intrusive monkey-patching but hey, it’s only a few days old.

Comparing Sendgrid implementation in a Rails app

Another thing that rocks about Sendgrid is how simple it is to use in a Rails app:

ActionMailer::Base.smtp_settings = {
  :address        => "smtp.sendgrid.net",
  :port           => "25",
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => ENV['SENDGRID_DOMAIN']
}

You don’t need any special gem, it’s just SMTP.

Using the aws-ses gem in a Rails app

Amazon SES requires some HMAC‘ing and other stuff, but when using a library, it’s still pretty easy and it has the same dependencies as Rails.

Add the gem to your Gemfile:

gem "aws-ses", "~> 0.3.2", :require => 'aws/ses'

Extend ActionMailer in config/initializers/amazon_ses.rb:

ActionMailer::Base.add_delivery_method :ses, AWS::SES::Base,
  :access_key_id     => ENV['AMAZON_ACCESS_KEY'],
  :secret_access_key => ENV['AMAZON_SECRET_KEY']

Set the delivery method in config/environments/*rb:

config.action_mailer.delivery_method = :ses

That’ll do it. Happy emailing!