简体   繁体   English

如何使用ActionMailer通过AWS SES发送电子邮件

[英]How to send email with AWS SES using ActionMailer

I am trying to use html templates to send invitation emails. 我正在尝试使用html模板发送邀请电子邮件。 Currently I'm using aws-ses gem like this: 目前,我正在使用aws-ses gem,如下所示:

  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 . 我将HTML代码作为字符串发送到: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/ . 我想要做的是使用模板,并将其存储在单独的文件中,例如, invite_email.html.erb ,该文件将存储在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 . 我设置动作邮件程序以使用AWS :: SES gem,并按照rails指南使用rails g mailer UserMailer设置动作邮件rails g mailer UserMailer I have a UserMailer, a layout, and I restarted the server. 我有一个UserMailer,一个布局,然后重新启动了服务器。 My developement.rb looks like this: 我的developement.rb看起来像这样:

  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 : 并且我在initializers/action_mailer.rb初始化了:ses这样的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 服务器响应: 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. 我在另一台计算机上的environments/test.rb使用相同设置在测试环境中进行了尝试,但仍然没有电子邮件。 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. 我正在使用以下方法使ActionMailer使用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. 我决定使用fog-aws gem,因为它允许我使用IAM角色,而不是显式指定访问凭据。

I've created lib/aws/ses_mailer.rb file with following content: 我创建了具有以下内容的lib/aws/ses_mailer.rb文件:

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 : 然后在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. 现在,您可以使用AWS SES发送电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM