Getting started quickly with Amazon Email Sending Service (SES) and Ruby on Rails

Amazon does a lot of things really well: shopping, building a bookshelf for your kindle, streaming media and of course, their awesome Web Services products. Not only do they do these things well, they also make them easy and secure for the rest of us. 

Amazon Email Sending Service, or Amazon SES, part of the Amazon AWS toybox, provides a secure outbound email service. To get up and running with Ruby on Rails apps, you only need to complete a handful of steps:

  1. Sign up for Amazon AWS (I'll assume you've already done this at http://aws.amazon.com.
  2. Configure Amazon SES.
  3. Initialize TLS for SMTP in your Rails app.
  4. Configure your mailer.
  5. Send email.
  6. win!, plus a couple other things.

Step 1: Sign up, now! http://aws.amazon.com

Step 2: Configure Amazon SES.

  • Open the Amazon Web Services management console and click on SES.

  • Click on SMTP Settings and click 'Create My SMTP Credentials'. After you click, you will be taken through a wizard to create credentials which will be used to authenticate against the SMTP server. You can use the default name or create your own, but if you have already created other Amazon AWS resources, this should be obvious.

  • After creating your credentials, open up the Verified Senders/Email Addresses screen. To help minimize the amount of abuse Amazon SES servers take and/or inflict on the world, Amazon SES employs verified senders. This means you have to own an address or addresses which are going to be used as senders across the SES SMTP mail servers. We will not cover verifying domains, which allows provisioning of SMTP sender access to any email address within a domain, but it's a simple process of adding a TXT record to your domain's DNS record.

    After clicking on the Verified Senders/Email Address section, and then on the Verify New Email Address, you should see this screen which prompts you for an email address to verify through a verification email:

After verifying your email address by clicking on the email you received, the required configuration is complete on the Amazon side of the house, so lets move on to Rails.

Step 3: Initialize TLS for SMTP in your Rails app.

To enable TLS for your mailer, simply drop this into an intiailizer.

require 'net/smtp'

module Net  
  class SMTP
    def tls?
      true
    end
  end
end  

Step 4: Configure your mailer.

To configure your mailer, add this to each environment for which you want to provision SMTP access to Amazon SES.

config.action_mailer.delivery_method = :smtp  
config.action_mailer.smtp_settings = {  
  :address => 'email-smtp.us-east-1.amazonaws.com',
  :authentication => :login,
  :user_name => '<previously configured smtp username>',
  :password => '<previously configured smtp password>',
  :enable_starttls_auto => true,
  :port => 465
}

Now just code an ActionMailer subclass, a rendering view, and send email with the power of Amazon.

ActionMailer: mailers/awesome_ses_mailer.rb

class SesMailer < ActionMailer::Base  
  default from: [email protected]'
  def hello_world
    mail(to: [email protected]', subject: 'I emailed from AmazonSES!')
  end
end  

View: views/ses_mailer/hello_world.text.erb

This sample email was sent from Amazon SES.  

Step 5: Send email.

Make the mailer call:

SesMailer.hello_world.deliver  

Step 6: That's it! The email should have arrived at the destination. It didn't make it? Oh yeah! A couple more things..

Sending email from Amazon is easy, but there are two things I need to mention:

  1. Handling errors - Amazon SES handles hard bounces through a supression list, as seen on the main SES dashboard. If an email bounces hard (is not deliverable), Amazon SES will automagically blacklist the email address from further delivery for 14 days. If you want to relieve an email of this, you can go into the supression list and remove it. This email address, as noted, is supressed across all SES servers.

  2. Requesting production access - By default, production (or live) access is not enabled. This is to ensure that you and your usages of the Amazon SES are hopefully under control. Once you test your app and ensure it doesn't spam the entire world, you can go ahead and request Production access through this screen which will prompt you to fill out a service ticket. Amazon engineers will work quickly to approve it. Before requesting access, your emails will not be delivered to the wild, but their status can be seen through the SES dashboard. Like with most other Amazon AWS offerings, when you send email, the meter is running. So keep an eye on your emails and make sure you don't break the bank.