All Articles

Fast and affordable emails with Amazon SES in Ruby on Rails

Amazon SES (Simple Email Service) is one of the easiest and most affordable ways to send emails from a Rails application.

It only takes a couple steps to configure your domain and set up your server to send emails via Amazon SES.

Prepare your domain in AWS

The first step is to verify your domain if you have not done so already. This may take up to 24 hours for the new DNS records to propagate.

Then from the SMPT Settings tab of the Amazon SES console, create your SMTP credentials. Make sure to save your credentials in a safe place, as we will use these credentials later in our Rails config.

Finally, you'll need to request production access to leave sandbox mode. In sandbox mode, you can only send emails to verified email addresses. This request usually takes less than 24 hours to be processed.

Configure Rails for SMPT

Open up your config/application.rb file and add these lines of code to the bottom.

# config/application.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true

config.action_mailer.smtp_settings = {
  address: ENV['AWS_SES_SERVER_NAME'],
  port: 587,
  user_name: ENV['AWS_SES_SMTP_USERNAME'],
  password: ENV['AWS_SES_SMTP_PASSWORD'],
  authentication: :login,
  enable_starttls_auto: true
}

Rails will now send your emails through Amazon SES!

Configuring Action Mailer in application.rb means Amazon SES will be used in both the development and production environments (emails are disabled in the test environment by default). I prefer my development environment to send real emails because it's super easy to catch formatting problems and to observe email based features like password reset.

Conclusion

It only takes a few lines of code to send real emails from your custom domain through Rails. Amazon SES is super affordable and reliable, so give it a try.

Happy email sending!