Setting up Rails to send emails with Amazon SES

Envelope

The Amazon Simple Email Service is a cloud service inside AWS (Amazon Web Services) to send and receive email. I have used it to send emails, and it worked flawlessly. It has a reasonable maximum sending quota, and it’s pretty fast for a modest web application.

It has serious competitors, especially the service from MailChimp: Mandrill. But since they switched it to an add-on for MailChimp premium users, Amazon SES is, in my opinion, the first choice for small and medium web applications. And I exclude big application on purpose because initially all users are placed in the Amazon SES sandbox (a test environment) to ensure the quality of service and avoid fraud. This has real limiting quotas for this kind of application, so a request must be filled to Amazon support to move out of the sandbox and get higher limits.

Before going on, you will need to sign up for an Amazon Web Services account. If you haven’t have one, go create one for yourself.

Verifying your domain

The first step is verifying the domain the emails will be sent from to prove the ownership. Don’t forget to generate DKIM settings, it is recommended to increase your domain trust.

This step will require to add some DNS records, so you will need access to your DNS panel.

Once all records are added, you will have to wait for them to propagate to the other internet DNS servers. This can take up to 24 hours to complete. For now you will see your domain listed in Amazon SES console as “pending verification”. Don’t worry, Amazon will check it automatically later on and verify your domain.

Creating the SMTP credentials

In order to use Amazon SES to send your emails you will need to authenticate using an SMTP endpoint.

Please go to your “SMTP Settings” page in your SES console. Here you can see your server name, note it down somewhere.

Click the “Create My SMTP Credentials” button. Amazon will ask you for a new IAS user name (IAS stands for Identity and Access Management), but they will provide you with a default one that you can safely accept. Click the “Create” button at the very bottom of the page.

Your credentials are now created! You can see them by unhiding them clicking the corresponding link.

SMTP configuration

There you will see your SMTP username and password. If you need to save a copy, click the “Download Credentials” button at the very bottom of this page, because you will not have access to these settings once this page is closed.

Configuring your Rails application

Open your config/environments/production.rb file and add this code inside the configure block:

Sending your emails

Your new account is still in the Amazon SES sandbox. That means that you can only send email to addresses using your verified domain. This is intended to test the service and your application.

If you want to test the service sending an email to an email address of yours, you will have to add and verify it in the “Email Addresses” in the “Identity Management” section of your SES console.

So click the “Verify a New Email Address” button. Enter an email of yours and you will receive a verification email. Click on the link you will find inside that email that you have received. That’s it! You now have a verified email recipient to test the service with. Bear in mind that you should send the test email from this address too.

When you are happy with it, go to “Sending Statistics” in the “Email Sending” section in your SES console. You will find a button to open a request to move out of the sandbox. When it’s accepted, your application will be able to send emails to any email address in the Internet.

Conclusion

Alright! We have set up our Amazon SES service and our Rails application to work together. Really. You don’t have to make any other modification, just use Rails email sending methods as usual and it will work.

With this article I wanted to show how easy is to configure and use a service like this. If you ever want to switch to another service, I’m sure the process will be similar. At least from Rails’ side you’ll only need to adapt your SMTP settings.

Happy email sending!

Did you like it? Please share it:

Get my ebook for free

10 ideas that helped me become a better developer (and may help you too)

Subscribe to my mailing list and get my ebook on 10 ideas that helped me become a better developer.

About Me

David Morales

David Morales

I'm David Morales, a computer engineer from Barcelona, working on projects using Ruby on Rails and training on web technologies.

Learn More