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 |
|
} |
Then add following code in mailers/ses_mailer.rb
|
Class SesMailer < ActionMailer::Base |
|
default from: ‘test@example.com‘ |
|
def welcome |
|
mail(to:‘somebody@email.com‘, subject: ‘I emailed from AmazonSES!‘) |
|
end |
|
end |
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
Call Mailer:
|
#call deliver from mailer action |
|
SesMailer.welcome.deliver |
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‘ |
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 :ses, AWS::SES::Base, |
|
:access_key_id =>ENV[‘AMAZON_ACCESS_KEY‘], |
|
:secret_access_key =>ENV[‘AMAZON_SECRET_KEY‘] |
3. Then set the delivery method in `config/environments/*.rb` as appropriate:
|
Config.action_mailer.delivery_method = :ses |
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!!