Use of Amazon SES in Rails App to Send emails

What is Amazon SES?

Amazon SES is an email platform that provides an easy, cost-effective way to send and receive email using your own email addresses and domains. Sending Email with Amazon SES using SMTP You can use Simple Mail Transfer Protocol(SMTP) to send emails by using Amazon SES. You need an Amazon SES SMTP username and password to access the Amazon SES SMTP interface. To create SMTP credentials: 1. Sign into the AWS Management Console and open the Amazon SES console at https://console.aws.amazon.com/ses. 2. In the navigation pane, click SMTP Settings. 3. In the content pane, click Create My SMTP Credentials. 4. In the Create User for SMTP dialog box, you will see that an SMTP username has been filled in for you. You can accept this suggested username or enter a different one. To proceed, click Create 5. Click on Show User SMTP Credentials. Your SMTP credentials will be displayed on the screen; copy them and store them in a safe place. You can also click Download Credentials to download a file that contains your credentials. After creating your SMTP credentials, open the Verified Senders/Email Addresses screen. Before you can send an email using Amazon SES, you have to own an address or addresses which are going to be used as senders across the SES SMTP mail servers. Configure mailer with SMTP with rails app: Add the following code in config/environments/*.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings= {
:address => email-smtp.us-east-1.amazonaws.com,
:authentication => :login,
:user_name => <previously configured smtp username>,
:password => <previously configured smtp password>,
:enable_starttls_auto => true,
:port => 465
}
view raw*.rb hosted with ❤ by GitHub
Then add following code in mailers/ses_mailer.rb
Class SesMailer < ActionMailer::Base
default from: test@example.com
def welcome
mail(to:somebody@email.comsubject: I emailed from AmazonSES!)
end
end
view rawses_mailer.rb hosted with ❤ by GitHub
Note: If your account is in Amazon SES sandbox, then you must also verify the email address of every recipient. For moving out of the sandbox, see Moving Out of the Amazon SES Sandbox. View: view/ses_mailer/welcome.html.erb
Welcome email!
view rawwelcome.html.erb hosted with ❤ by GitHub
Call Mailer:
#call deliver from mailer action
SesMailer.welcome.deliver
view rawcall_mailer.rb hosted with ❤ by GitHub
You can track your email sending statistics on Sending Statistics of Amazon Console.

Sending emails using aws-ses gem:

1. Add aws-ses to the gem file – https://github.com/drewblas/aws-ses
gem aws-ses~> 0.6.0:require => aws/ses
view rawGemfile hosted with ❤ by GitHub
2. Extend ActionMailer in config/initializers/your_config_file.rb, where config_file.rb is the name of the file which contains initialization routines for ses gem:
ActionMailer::Base.add_delivery_method :sesAWS::SES::Base,
:access_key_id =>ENV[AMAZON_ACCESS_KEY],
:secret_access_key =>ENV[AMAZON_SECRET_KEY]
view rawconfig_file.rb hosted with ❤ by GitHub
3. Then set the delivery method in `config/environments/*.rb` as appropriate:
Config.action_mailer.delivery_method = :ses
view rawenvironment.rb hosted with ❤ by GitHub
You are now ready to send emails from rails app through Amazon SES. Just make sure you have verified email addresses through which you are going to send the emails and you are out of Amazon SES sandbox. Happy Mailing!!

Share this post