Ada*_*amB 37 ruby-on-rails amazon-web-services ruby-on-rails-3
什么是使ActionMailer在Rails 3中通过Amazon SES发送邮件的最佳方式?
编辑:
这是一个宝石:
gem install amazon-ses-mailer
Run Code Online (Sandbox Code Playgroud)
Suj*_*pta 78
设置Rails 3.2以使用亚马逊的简单电子邮件服务(SES)发送电子邮件很容易.您不需要任何额外的宝石或猴子补丁来使其工作.
SES支持通过SMTP的STARTTLS以及TLS/SSL.以下演示了如何使用SES为STARTTLS设置Rails.
先决条件
如果您正在运行rails Mac OS X,则可能需要在使用STARTTLS之前正确配置OpenSSL for Ruby.如果您使用的是Ruby 1.9.3和RVM,以下是一种方法:
rvm pkg install openssl
rvm reinstall 1.9.3 --with-openssl-dir=$rvm_path/usr
Run Code Online (Sandbox Code Playgroud)如果您不这样做,当您尝试发送电子邮件时,Ruby可能会出现段错误.
确保您已使用AWS验证了发件人电子邮件地址.您只能发送带有经过验证的电子邮件地址的电子邮件作为发件人.转到AWS控制台左侧菜单中的"已验证发件人"选项以获取SES.
确保您具有用于身份验证的AWS SMTP用户名和密码.转到AWS控制台左侧菜单中的"SMTP设置"选项以供SES进行设置.首先会提示您创建一个IAM用户(默认值:ses-smtp-user),然后您将看到SMTP用户和密码,它看起来像通常的AWS密钥和密码.请注意,IAM用户(即ses-smtp-user)不是您将用于身份验证的SMTP用户.
配置Rails
在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
}
Run Code Online (Sandbox Code Playgroud)
发送电子邮件
就是这个.现在,您可以继续创建邮件程序并开始发送电子邮件以获得乐趣和利润!
创建一个示例邮件程序
rails g mailer user_mailer
Run Code Online (Sandbox Code Playgroud)
在app/mailer/user_mailer.rb中:
class UserMailer < ActionMailer::Base
# Make sure to set this to your verified sender!
default from: "your@verifiedsender.com"
def test(email)
mail(:to => email, :subject => "Hello World!")
end
end
Run Code Online (Sandbox Code Playgroud)
在views/user_mailer/test.erb中:
A quick brown fox jumped over the lazy dog.
Run Code Online (Sandbox Code Playgroud)
现在,启动控制台并拍摄测试电子邮件:
rails c
Loading development environment (Rails 3.2.1)
1.9.3p125 :001 > UserMailer.test("your@email.com").deliver
Run Code Online (Sandbox Code Playgroud)
在探讨了一下后,我最后只是做了一个简单的课程.
https://github.com/abronte/Amazon-SES-Mailer
在rails中,您可以获取编码的电子邮件消息:
m = UserMailer.welcome.encoded
AmazonSES.new.deliver(m)
Run Code Online (Sandbox Code Playgroud)
对于TLS SSL设置[亚马逊SES推荐]
这件事你不需要宝石.
smtp是在rails中发送电子邮件的默认方式,但您可以添加此行以在config/application.rb文件中明确定义
config.action_mailer.delivery_method = :smtp
Run Code Online (Sandbox Code Playgroud)
在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
}
Run Code Online (Sandbox Code Playgroud)
在亚马逊SES SMTP HOSTNAME是具体的每一个区域,让你的名字,你都在,以下是主机名WRT地区.
StackOverFlow | 使用-SMTP亚马逊工具入门-发送-
我使用以下宝石:
https://github.com/aws/aws-sdk-rails
它引入了标准的aws-sdk,并允许将 ActionMailer 设置为使用 AWS SES。例子:
# config/production.rb
# ...
config.action_mailer.delivery_method = :aws_sdk
Run Code Online (Sandbox Code Playgroud)
使用 Amazon SES 配置您的 Rails 应用程序
将 action_mailer.perform_deliveries 设置为 true,因为在开发/生产环境中默认设置为 false
config.action_mailer.perform_deliveries = true
Run Code Online (Sandbox Code Playgroud)
然后将此代码粘贴到您的开发/生产环境中
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25545 次 |
| 最近记录: |