2011-01-25 201 views
37

要使ActionMailer通過Amazon SES在Rails 3中發送郵件,最好的方法是什麼?在Rails 3中使用Amazon SES ActionMailer

編輯:

現在這是一個寶石:

gem install amazon-ses-mailer 

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

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

+0

如果有人仍在訪問這個答案,上面的gem在JRuby(也許在JRuby之外)有SSL證書驗證問題,可以像修改gem那樣修復這個問題:http://martinottenwaelter.fr/2010/12/ ruby19-and-the-ssl-error/ – laker 2012-09-21 23:45:30

+2

它爲什麼是「官方」? – lulalala 2012-11-08 03:30:03

回答

-2

SES今天剛剛被釋放到測試階段,所以我懷疑是有現成(至少,不是我見過的)。您可以根據自己的開發人員文檔編寫自定義模塊:

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

我知道問這個問題還是很早。最主要的是我想使用通過郵件生成的MIME編碼消息欄。 – AdamB 2011-01-25 21:31:01

-3

您可以在您的環境中爲操作郵件程序提供傳遞方法。

config.action_mailer.delivery_method = AmazonSES.deliver 

現在,您很可能會自行編寫發貨代碼。

9

經過一番討論後,我最終做出了一個簡單的課程來做到這一點。

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

在軌,你可以得到編碼的電子郵件消息:

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

好的開始,謝謝你跳入戰鬥! – jwarchol 2011-01-26 03:27:05

+0

偉大的工作,謝謝! – jschorr 2011-01-26 03:31:39

77

設置Rails 3.2使用Amazon的簡單電子郵件服務(SES)發送電子郵件很容易。 你不需要任何額外的寶石或猴子修補,使其工作。

SES支持SMTP以及TLS/SSL上的STARTTLS。以下演示如何使用SES爲STARTTLS設置Rails。

先決條件

  1. 如果您正在運行軌道的Mac OS X,你可能需要正確配置OpenSSL,以便紅寶石,然後才能使用STARTTLS。如果您在使用Ruby 1.9.3和RVM,這裏是做到這一點的一種方法:

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

如果你不這樣做,有一種可能性,即紅寶石會出現段錯誤,當您嘗試發送電子郵件。

  1. 確保您已使用AWS驗證了您的發件人電子郵件地址。您只能發送帶有經過驗證的電子郵件地址的電子郵件作爲發件人。轉到SES的AWS控制檯左側菜單中的「已驗證發件人」選項。

  2. 確保您具有用於身份驗證的AWS SMTP用戶名和密碼。進入SES的AWS控制檯左側菜單中的「SMTP設置」選項進行設置。您將首先被提示創建一個IAM用戶(默認:ses-smtp-user),然後您將看到SMTP用戶和密碼,這看起來像通常的AWS密鑰和祕密。請注意,IAM用戶,即ses-smtp用戶是而不是您將用於身份驗證的SMTP用戶。

配置的Rails

在配置/環境/ development.rb和配置/環境/ 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 
    } 

發送電子郵件

就是這樣。現在,您可以繼續創建郵件並開始發送電子郵件,以獲得樂趣和利潤!

創建示例郵包

rails g mailer user_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 

在視圖/ user_mailer文件/ test.erb:

A quick brown fox jumped over the lazy dog. 

現在,啓動控制檯並拍攝測試郵件:

rails c 

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

使用:sendmail,我設法讓所有的電子郵件發送運行apt-get install postfix作爲root在我的AWS機器上,並使用所有的默認答案。

6

對於TLS SSL設置[亞馬遜SES推薦]

你不需要的寶石爲這件事。

SMTP是在軌發送電子郵件的方式defualt,但你可以加入這一行到配置明確定義/ application.rb中文件

config.action_mailer.delivery_method = :smtp 

的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 
} 

亞馬遜SES SMTP HOSTNAME特定對於每個地區,所以你所在的名字,以下是主機名和區域。

  1. email-smtp.us-east-1.amazonaws.com (用於區域美國東部-1)
  2. 電子郵件smtp.us - 西2.amazonaws。COM (用於區域美國西-2)
  3. email-smtp.eu-west-1.amazonaws.com (用於區域歐盟 - 西-1)

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

0

我創建了一個使用Signature v4簽名請求的簡單Rails/SES API gem。這是最適合用於交易電子郵件,如與我們聯繫,用戶註冊等

Rails SES API integration gem

請隨時加以改進&貢獻。

1

我用下面的寶石:

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

它拉在標準aws-sdk,再加上允許設置的ActionMailer使用AWS SES。例如:

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

配置與亞馬遜SES

集action_mailer.perform_deliveries到真正的Rails應用程序,因爲它是在開發/生產環境中設置爲false默認

config.action_mailer.perform_deliveries = true 

再將其粘貼代碼在您的開發/生產環境中

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 
}