Aws::SES SMTP integration is a cake walk indeed. But SES API does not directly work with ActionMailer. I think there could be a better approach but for the current requirement I made it work this way:
1. Added SES as a delivery method:
In initializer, add your SES configuration and add the SES client as new delivery method:
1 2 3 |
ActionMailer::Base.add_delivery_method :ses, Aws::SES::Client, :access_key_id => ENV['AMAZON_ACCESS'], :secret_access_key => ENV['AMAZON_SECRET'] |
You have to specify Region as well. I had already added
1 |
AWS_REGION: 'eu-west-1' |
to my application.yml
2. Asked ActionMailer to use ses delivery method in your environment:
Add the following line to config/environments/<environment>.rb
1 |
config.action_mailer.delivery_method = :ses |
Once you are done with this you are ready to send mail. Well I also thought the same, but when I ran the code BOOM. So what we dont have is deliver! method in the Aws::SES module. So you extend Aws::SES:Client. I did it in the initializer itself.
3. Added the following code to config/initializer/ses.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
module Aws module SES class Client def deliver!(mail) ses_return = send_email( source: Array(mail.from).first, destination: { to_addresses: mail.destinations }, message: { subject: { data: mail.subject }, body: { text: { data: mail.body.encoded }, html: { data: mail.body.encoded } } }) return ses_return.message_id end end end end |
VOILA! You get back the message ID that the API send back and you can store it to track the delivery status of your mail.
Note: AWS SES provides SMTP service as well, which is easier to integrate but you wont be able to track the deliver from your application. Also SMTP access key pair is different from the access key pair that we use for API (which is global API key pair. I have not tried the IAM setting with this).