Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Amazon SES with Rails ActionMailer

What would be the best way to go about making ActionMailer send mail via Amazon SES in Rails 3?

Edit:

This is now a gem:

gem install amazon-ses-mailer 

https://rubygems.org/gems/amazon-ses-mailer

https://github.com/abronte/Amazon-SES-Mailer

like image 932
AdamB Avatar asked Jan 25 '11 20:01

AdamB


People also ask

Can I configure SES with my own domain?

You can configure a MAIL FROM domain for an entire domain. When you do, all of the messages that you send from addresses on that domain use the same MAIL FROM domain. Open the Amazon SES console at https://console.aws.amazon.com/ses/ . In the left navigation pane, under Configuration, choose Verified identities.

Does Amazon SES use TLS?

Amazon SES supports TLS 1.2, TLS 1.1, and TLS 1.0 for TLS connections. By default, Amazon SES uses opportunistic TLS. This means that Amazon SES always attempts to make a secure connection to the receiving mail server. If Amazon SES can't establish a secure connection, it sends the message unencrypted.


2 Answers

Setting up Rails 3.2 for sending emails using Amazon's Simple Email Service (SES) is easy. You do not require any additional gem or monkey patching to make it work.

SES supports both STARTTLS over SMTP as well as TLS/SSL. The following demonstrates how to set up Rails for STARTTLS with SES.

Prerequisites

  1. If you are running rails Mac OS X, you may need to configure OpenSSL for Ruby correctly before you can use STARTTLS. If you are using Ruby 1.9.3 and RVM, here is one way to do this:

    rvm pkg install openssl rvm reinstall 1.9.3 --with-openssl-dir=$rvm_path/usr  

If you do not do this, there is a possibility that Ruby will segfault when you try to send an email.

  1. Make sure you have verified your sender email address with AWS. You can only send emails with a verified email address as the sender. Go to the "Verified Senders" option on the left menu in AWS console for SES.

  2. Make sure you have the AWS SMTP user name and password for authentication. Go to the "SMTP Settings" option on the left menu in AWS console for SES to set this up. You will first be prompted to create an IAM user (default: ses-smtp-user) and then you will be shown the SMTP user and password, which look like usual AWS key and secret. Note that the IAM user, i.e., ses-smtp-user is not the SMTP user that you will be using for authentication.

Configuring Rails

In config/environments/development.rb and config/environments/production.rb, add the following:

  config.action_mailer.delivery_method = :smtp   config.action_mailer.smtp_settings = {       :address => "email-smtp.us-east-1.amazonaws.com",       :port => 587, # Port 25 is throttled on AWS       :user_name => "...", # Your SMTP user here.       :password => "...", # Your SMTP password here.       :authentication => :login,       :enable_starttls_auto => true   } 

Sending an email

This is it. Now you can go ahead and create a mailer and start sending emails for fun and profit!

Create a sample mailer

rails g mailer user_mailer 

In app/mailer/user_mailer.rb:

    class UserMailer < ActionMailer::Base       # Make sure to set this to your verified sender!       default from: "[email protected]"          def test(email)         mail(:to => email, :subject => "Hello World!")       end     end  

In views/user_mailer/test.erb:

    A quick brown fox jumped over the lazy dog. 

Now, launch the console and shoot off a test email:

    rails c      Loading development environment (Rails 3.2.1)     1.9.3p125 :001 > UserMailer.test("[email protected]").deliver 
like image 199
Sujoy Gupta Avatar answered Sep 21 '22 12:09

Sujoy Gupta


I also have a gem out that supports sending e-mail through SES from Rails 3:

https://github.com/drewblas/aws-ses

It also has all the API for verifying/managing e-mail addresses

like image 39
Drew Blas Avatar answered Sep 23 '22 12:09

Drew Blas