把 mails 命名為 SomethingMailer。 沒有 Mailer 字根的話,不能立即顯現哪個是一個 Mailer,以及哪個視圖與它有關。
提供 HTML 與純文本視圖模版。
在你的開發環境啟用信件失敗發送錯誤。這些錯誤缺省是被停用的。
# config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
在開發模式使用 smtp.gmail.com 設置 SMTP 服務器(當然了,除非你自己有本地 SMTP 服務器)。
# config/environments/development.rb
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
# 更多設置
}
提供缺省的配置給主機名。
# config/environments/development.rb
config.action_mailer.default_url_options = {host: "#{local_ip}:3000"}
# config/environments/production.rb
config.action_mailer.default_url_options = {host: 'your_site.com'}
# 在你的 mailer 類
default_url_options[:host] = 'your_site.com'
如果你需要在你的網站使用一個 email 鏈結,總是使用 _url 方法,而不是 _path 方法。 _url 方法包含了主機名,而 _path 方法沒有。
# 錯誤
You can always find more info about this course
= link_to 'here', url_for(course_path(@course))
# 正確
You can always find more info about this course
= link_to 'here', url_for(course_url(@course))
正確地顯示寄與收件人地址的格式。使用下列格式:
# 在你的 mailer 類別
default from: 'Your Name info@your_site.com>'
確定測試環境的 email 發送方法設置為 test :
# config/environments/test.rb
config.action_mailer.delivery_method = :test
開發與生產環境的發送方法應為 smtp :
# config/environments/development.rb, config/environments/production.rb
config.action_mailer.delivery_method = :smtp
當發送 HTML email 時,所有樣式應為行內樣式,由于某些用戶有關于外部樣式的問題。某種程度上這使得更難管理及造成代碼重用。有兩個相似的 gem 可以轉換樣式,以及將它們放在對應的 html 標簽里: premailer-rails3 和roadie。
應避免頁面產生響應時寄送 email。若多個 email 寄送時,造成了頁面載入延遲,以及請求可能逾時。使用 delayed_job gem 的幫助來克服在背景處理寄送 email 的問題。
您可能感興趣的文章:- Ruby on Rails遷移時的一些注意事項
- Ruby on Rails中的ActiveRecord編程指南
- 關于Ruby on Rails路由配置的一些建議