HOWTO send email with Rails & Amazon SES

Amazon Simple Email Service (Amazon SES) is a service which provides a set of APIs for sending and receiving email. This guide will show you how to setup your Rails application to send email via Amazon SES.

Finding your credentials

First you’ll need to get your access key credentials from the AWS Management Console.

IAM access keys are best for production applications, and can be managed from the "Security Credentials" tab of the IAM user page. Alternatively you can use a root access key, which you can find in the "Access Keys" section of the account-wide "Security Credentials" page. Root keys are more of a security risk because they grant access to everything in your AWS account, but easier to use if you’re new to AWS.

You’ll also need to pick a region (see the AWS General Reference for a list).

Setting up your credentials

For a production app it’s best to store your credentials in environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION.

If you want to test Amazon SES from your development environment you can set the same environment variables in your .env file (when using something like heroku local or foreman), or you can store your credentials in an ini formatted config file which the AWS SDK detects automatically (~/.aws/credentials).

Configuring your application

Add the aws-sdk-rails gem to your Gemfile:

gem 'aws-sdk-rails', '~> 1.0'

Then run bundle to install the AWS SDK gems.

Configure ActionMailer to use Amazon SES by specifying the delivery method in config/environments/production.rb:

config.action_mailer.delivery_method = :aws_sdk

That’s all the configuration code you need, but there’s a bit more setup involved.

Verifying domains & email addresses

Aws::SES::Errors::MessageRejected exceptions? Before you can send any email you’ll need to verify the addresses and/or domains that you’ll be sending email from.

Click either the "Verify a New Email Address" or "Verify a New Domain" buttons in the SES Management Console and follow the instructions.

Verifying an email address is easier and quicker: you’ll receive an email with a verification link. Verifying a domain is a bit more involved: you’ll need to update the DNS settings for the domain, which can take up to 72 hours for Amazon to verify.

Moving out of the sandbox

Initially your SES usage will be sandboxed with various limitations to prevent email fraud and abuse. You can lift the restrictions and move out of the sandbox by creating a new support case in the AWS Support Center. See the developer guide for details.

Once out of the sandbox you’ll be able to send email to unverified addresses.

New to ActionMailer? Start with the Rails guide.

Need to debug emails in development? Try mailcatcher.

Looking for an alternative to Amazon SES? Try sending email with Mailgun instead.