Login Sign up

Sending emails with SMTP and Amazon SES in Ruby on Rails

Setup Rails to send emails over SMTP using your Amazon SES account

October 07, 2017 6 minutes read
1 nw3af5 bwgf mpg zktwpa
Amazon Simple Email Service is a cloud service inside AWS (Amazon Web Services) to send and receive emails. Amazon SES lets you send transactional emails, marketing messages, or any other type of high-quality content to your customers. With Amazon SES, you have no required minimum commitments you pay as you go and only pay for what you use.

In the past I have used services such as SendGrid and Mandrill. But since they switched Mandrill to an add-on for MailChimp premium users, Amazon SES in my opinion is the first choice for small to medium web applications. Cost of sending 40,000 emails per month with Amazon SES is mere $4 which is way cheaper than the other competitors.

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

Step One: Domain Verification

Verifying the ownership of the domain that will be used to send emails.

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 requires you to add some DNS records, so you will need access to your DNS panel. I prefer using Cloudflare to manage my domain’s DNS records as it not only helps with the DNS management but it has a lot more to offer.

Once all records are added, you will have to wait for upto 24 hours for them to propagate to the other internet DNS servers. For now your domain will be listed in AWS console as “Pending Verification”. Wait until the domain status changes to “Verified”.

Step Two: Generating SMTP Credentials

Generating SMTP Username, Password and Address from AWS Console

  1. Login to AWS management console. Sign up for AWS if you haven’t already. It’s pretty simple.
2. It will take you to the AWS Dashboard, look for SES at the Dashboard.

3. Click on “SES” to go to the Amazon SES Dashboard. Click on “Email Addresses”

4. Verify two email addresses (You must have two email addresses to complete the email sending flow in sandbox mode). I have already added two emails and verified them. Click on Verify a New Email Address button to add your sending and receiving email addresses.

5. Create your SMTP credentials to use in your app.

6. Copy the SMTP server name to use in your app.

Here you can 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

Now that all is set at Amazon’s end, we need to configure our RoR application.

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

We have now set up our Amazon SES service and Ruby on Rails application to work together. You don’t have to make any other changes, just use Rails email sending methods as usual and it shall work.

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.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.


Get the inside scoop

Get product updates, launch info, and special deals by signing up for my product newsletter.

You're signing up to receive amazing marketing emails from Rahul Lakhaney