Ruby-on-rails 将 Amazon SES 与 Rails ActionMailer 结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4798437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:11:31  来源:igfitidea点击:

Using Amazon SES with Rails ActionMailer

ruby-on-railsruby-on-rails-3amazon-web-services

提问by AdamB

What would be the best way to go about making ActionMailer send mail via Amazon SES in Rails 3?

在 Rails 3 中通过 Amazon SES 使 ActionMailer 发送邮件的最佳方法是什么?

Edit:

编辑:

This is now a gem:

这现在是一个宝石:

gem install amazon-ses-mailer

https://rubygems.org/gems/amazon-ses-mailer

https://rubygems.org/gems/amazon-ses-mailer

https://github.com/abronte/Amazon-SES-Mailer

https://github.com/abronte/Amazon-SES-Mailer

采纳答案by Drew Blas

I also have a gem out that supports sending e-mail through SES from Rails 3:

我还有一个支持从 Rails 3 通过 SES 发送电子邮件的 gem:

https://github.com/drewblas/aws-ses

https://github.com/drewblas/aws-ses

It also has all the API for verifying/managing e-mail addresses

它还具有用于验证/管理电子邮件地址的所有 API

回答by Sujoy Gupta

Setting up Rails 3.2 for sending emails using Amazon's Simple Email Service (SES) is easy. You do not require any additional gemor monkey patching to make it work.

设置 Rails 3.2 以使用 Amazon 的简单电子邮件服务 (SES) 发送电子邮件很容易。您不需要任何额外的 gem或猴子补丁来使其工作。

SES supports both STARTTLS over SMTP as well as TLS/SSL. The following demonstrates how to set up Rails for STARTTLS with SES.

SES 支持通过 SMTP 的 STARTTLS 以及 TLS/SSL。下面演示了如何使用 SES 为 STARTTLS 设置 Rails。

Prerequisites

先决条件

  1. If you are running rails Mac OS X, you may need to configure OpenSSL for Ruby correctly before you can use STARTTLS. If you are using Ruby 1.9.3 and RVM, here is one way to do this:

    rvm pkg install openssl
    rvm reinstall 1.9.3 --with-openssl-dir=$rvm_path/usr 
    
  1. 如果您运行的是 Rails Mac OS X,您可能需要先为 Ruby 正确配置 OpenSSL,然后才能使用 STARTTLS。如果您使用的是 Ruby 1.9.3 和 RVM,这里是执行此操作的一种方法:

    rvm pkg install openssl
    rvm reinstall 1.9.3 --with-openssl-dir=$rvm_path/usr 
    

If you do not do this, there is a possibility that Ruby will segfault when you try to send an email.

如果您不这样做,当您尝试发送电子邮件时,Ruby 可能会出现段错误。

  1. Make sure you have verified your sender email address with AWS. You can only send emails with a verified email address as the sender. Go to the "Verified Senders" option on the left menu in AWS console for SES.

  2. Make sure you have the AWS SMTP user name and password for authentication. Go to the "SMTP Settings" option on the left menu in AWS console for SES to set this up. You will first be prompted to create an IAM user (default: ses-smtp-user) and then you will be shown the SMTP user and password, which look like usual AWS key and secret. Note that the IAM user, i.e., ses-smtp-user is notthe SMTP user that you will be using for authentication.

  1. 确保您已通过 AWS 验证您的发件人电子邮件地址。您只能使用经过验证的电子邮件地址作为发件人发送电子邮件。转到 SES 的 AWS 控制台左侧菜单上的“已验证发件人”选项。

  2. 确保您拥有用于身份验证的 AWS SMTP 用户名和密码。转到 SES 的 AWS 控制台左侧菜单上的“SMTP 设置”选项进行设置。您将首先被提示创建一个 IAM 用户(默认:ses-smtp-user),然后您将看到 SMTP 用户和密码,它们看起来像通常的 AWS 密钥和秘密。请注意,IAM 用户(即 ses-smtp-user)不是您将用于身份验证的 SMTP 用户。

Configuring Rails

配置 Rails

In config/environments/development.rb and config/environments/production.rb, add the following:

在 config/environments/development.rb 和 config/environments/production.rb 中,添加以下内容:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address => "email-smtp.us-east-1.amazonaws.com",
      :port => 587, # Port 25 is throttled on AWS
      :user_name => "...", # Your SMTP user here.
      :password => "...", # Your SMTP password here.
      :authentication => :login,
      :enable_starttls_auto => true
  }

Sending an email

发送电子邮件

This is it. Now you can go ahead and create a mailer and start sending emails for fun and profit!

就是这个。现在您可以继续创建邮件程序并开始发送电子邮件以获得乐趣和利润!

Create a sample mailer

创建示例邮件程序

rails g mailer user_mailer

In app/mailer/user_mailer.rb:

在 app/mailer/user_mailer.rb 中:

    class UserMailer < ActionMailer::Base
      # Make sure to set this to your verified sender!
      default from: "[email protected]"  

      def test(email)
        mail(:to => email, :subject => "Hello World!")
      end
    end 

In views/user_mailer/test.erb:

在 views/user_mailer/test.erb 中:

    A quick brown fox jumped over the lazy dog.

Now, launch the console and shoot off a test email:

现在,启动控制台并发送测试电子邮件:

    rails c

    Loading development environment (Rails 3.2.1)
    1.9.3p125 :001 > UserMailer.test("[email protected]").deliver

回答by AdamB

After poking around a bit I ended up just making a simple class to do this.

在摸索了一下之后,我最终只做了一个简单的类来做到这一点。

https://github.com/abronte/Amazon-SES-Mailer

https://github.com/abronte/Amazon-SES-Mailer

In rails, you can get the encoded email message:

在 rails 中,您可以获得编码的电子邮件消息:

m = UserMailer.welcome.encoded
AmazonSES.new.deliver(m)

回答by Taimoor Changaiz

For TLS SSL setup [Recommended by Amazon SES]

对于 TLS SSL 设置 [Amazon SES 推荐]

Spoiler Alert: NO GEM Required

剧透警报:无需 GEM

smtp is defualt way of sending email in rails but you can add this line to explicitly define in config/application.rbfile

smtp 是在 rails 中发送电子邮件的默认方式,但您可以添加此行以在config/application.rb文件中明确定义

config.action_mailer.delivery_method = :smtp

In config/application.rbor you can specify in certain environment file

config/application.rb或者你可以在某些环境文件中指定

config.action_mailer.smtp_settings = {
    address: 'Amazon SES SMTP HOSTNAME',
    port: 465,   #TLS port
    domain: 'example.com',
    user_name: 'SMTP_USERNAME',
    password: 'SMTP_PASSWORD',
    authentication: 'plain',   #you can also use login
    ssl: true,   #For TLS SSL connection
}

The Amazon SES SMTP HOSTNAMEis specific for every region, so you that name which you are in, following are hostnames wrt regions.

亚马逊SES SMTP HOSTNAME是具体的每一个区域,让你的名字,你都在,下面是WRT地区的主机名。

  1. email-smtp.us-east-1.amazonaws.com (for region us-east-1)
  2. email-smtp.us-west-2.amazonaws.com (for region us-west-2)
  3. email-smtp.eu-west-1.amazonaws.com (for region eu-west-1)
  1. email-smtp.us-east-1.amazonaws.com (适用于区域 us-east-1)
  2. email-smtp.us-west-2.amazonaws.com (适用于区域 us-west-2)
  3. email-smtp.eu-west-1.amazonaws.com (适用于 eu-west-1 区域)

StackOverFlow| Amazon-getting-started-send-using-smtp

StackOverFlow| Amazon-getting-started-send-using-smtp

回答by Marcelo Austria

Configuring your Rails application with Amazon SES

使用 Amazon SES 配置您的 Rails 应用程序

set action_mailer.perform_deliveries to true as it is set to false by default in the development/production environment

将 action_mailer.perform_deliveries 设置为 true,因为它在开发/生产环境中默认设置为 false

config.action_mailer.perform_deliveries = true

then paste this code in your development/production environment

然后将此代码粘贴到您的开发/生产环境中

config.action_mailer.smtp_settings = {
  :address => ENV["SES_SMTP_ADDRESS"],
  :port => 587,
  :user_name => ENV["SES_SMTP_USERNAME"], 
  :password => ENV["SES_SMTP_PASSWORD"],
  :authentication => :login,
  :enable_starttls_auto => true
}

回答by Andreas Profous

I use the following gem:

我使用以下宝石:

https://github.com/aws/aws-sdk-rails

https://github.com/aws/aws-sdk-rails

It pulls in the standard aws-sdk, plus allows to set ActionMailer to use AWS SES. Example:

它引入了标准的aws-sdk,并允许将 ActionMailer 设置为使用 AWS SES。例子:

# config/production.rb
# ...
config.action_mailer.delivery_method     = :aws_sdk

回答by csi

I created a simple Rails / SES API gem that uses Signature v4 to sign the request. This is best used for transactional emails such as contact us, user registration, etc.

我创建了一个简单的 Rails / SES API gem,它使用 Signature v4 来签署请求。这最适合用于交易电子邮件,例如联系我们、用户注册等。

Rails SES API integration gem

Rails SES API 集成 gem

Please feel free to improve on it & contribute.

请随时改进它并做出贡献。

回答by bigpotato

using :sendmail, I managed to get all emails to send running apt-get install postfixas root on my AWS machine and using all the default answers.

使用:sendmail,我设法让所有电子邮件以apt-get install postfixroot 身份在我的 AWS 机器上运行并使用所有默认答案。

回答by jschorr

SES just was released into beta today, so I doubt that there is a ready-to-go gem (at least, not that I've seen). You could write a custom module based upon their developer documents:

SES 今天刚刚发布到测试版,所以我怀疑是否有现成的 gem(至少,我没见过)。您可以根据他们的开发人员文档编写自定义模块:

http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/

回答by bbck

You can provide delivery method to action mailer in your environment.

您可以在您的环境中为操作邮件程序提供传递方法。

config.action_mailer.delivery_method = AmazonSES.deliver

For now you are likely on your own writing the delivery code.

现在,您可能需要自己编写交付代码。