I am trying to use html templates to send invitation emails. Currently I'm using aws-ses gem like this:
ses = AWS::SES::Base.new(
:access_key_id => 'XXXXXXXXXXXXXXX',
:secret_access_key => 'XXXXXXXXXXXXXXX')
ses.send_email(:to => ..., :source => ..., :subject => ..., :html_body => <p> Hi how are you</p>)
and I send the html code as a string in :html_body
. This works fine.
What I want to do is use a template, and store it in a separate file, for example invite_email.html.erb
, which will be stored under app/views/user_mailer/
.
So I figured I had to use action mailer to use rendered views. I setup action mailer to use AWS::SES gem, and followed the rails guides to setup action mailer with rails g mailer UserMailer
. I have a UserMailer, a layout, and I restarted the server. My developement.rb
looks like this:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :ses
and I initialized :ses
like this in initializers/action_mailer.rb
:
ActionMailer::Base.add_delivery_method :ses, AWS::SES::Base,
access_key_id: 'XXXXX',
secret_access_key: 'XXXXX'
The server responds: UserMailer#invite_email: processed outbound mail in 184.0ms
The problem is, I still don't get any emails. I tried in the test environment with the same settings in environments/test.rb
on a different machine, and still no emails. The server shows the layout is being rendered and the email is being processed. Am I missing a setting?
I'm using the following approach to make ActionMailer
to send emails using AWS SES. I have created a simple wrapper around fog-aws gem and added delivery method similar to what you use in your question. I've decided to use fog-aws
gem because it allows me to use IAM roles instead of specifying access credentials explicitly.
I've created lib/aws/ses_mailer.rb
file with following content:
module AWS
class SESMailer
attr_reader :settings
def initialize(options = {})
@fog_mailer = Fog::AWS::SES.new(options)
@settings = {}
end
delegate :send_raw_email, to: :@fog_mailer
alias_method :deliver!, :send_raw_email
alias_method :deliver, :send_raw_email
end
end
Then add delivery method in config/initializers/amazon_ses.rb
:
ActionMailer::Base.add_delivery_method :ses, AWS::SESMailer, use_iam_profile: true
Then enable it for particular environment:
config.action_mailer.delivery_method = :ses
And now you may send emails with AWS SES.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.