Ruby on Rails/ActionMailer
注意: 以下文件直接來自 Rails API 文件
ActionMailer 允許您使用郵件模型和檢視從應用程式傳送電子郵件。
要使用 ActionMailer,您需要建立一個郵件模型。
bin/rails mailer Notifier
生成的模型繼承自ActionMailer::Base。電子郵件透過在模型中建立方法來定義,這些方法隨後用於設定要在郵件模板中使用的變數,以更改郵件上的選項或新增附件。
示例
class Notifier < ActionMailer::Base
def signup_notification(recipient)
recipients recipient.email_address_with_name
from "system@example.com"
subject "New account information"
body "account" => recipient
end
end
郵件方法具有以下可用配置方法。
- recipients接受一個或多個電子郵件地址。這些地址是您的電子郵件將被髮送到的地址。設定 To: 標頭。
- subject您的電子郵件的主題。設定 Subject: 標頭。
- from您正在傳送的電子郵件來自誰。設定 From: 標頭。
- cc接受一個或多個電子郵件地址。這些地址將收到您的電子郵件的副本。設定 Cc: 標頭。
- bcc接受一個或多個電子郵件地址。這些地址將收到您的電子郵件的密件副本。設定 Bcc: 標頭。
- sent_on郵件傳送的日期。如果未設定,則標頭將由傳遞代理設定。
- content_type指定郵件的媒體型別。預設為 text/plain。
- headers指定要為郵件設定的附加標頭,例如 headers ‘X-Mail-Count’ => 107370。
body 方法具有特殊行為。它接受一個雜湊,該雜湊生成一個以雜湊中每個鍵命名的例項變數,其中包含鍵指向的值。
例如,body "account" => recipient 將導致一個名為 @account 的例項變數,其值為 recipient,可以在檢視中訪問。
與 ActionController 類似,每個郵件類都有一個相應的檢視目錄,其中該類的每個方法都在其中查詢與其名稱相同的模板。要定義要與郵件一起使用的模板,請建立一個與郵件模型中方法同名的 .rhtml 檔案。例如,在上面定義的郵件中,模板位於
app/views/notifier/signup_notification.rhtml
將用於生成電子郵件。
在模型中定義的變數可以在檢視中作為例項變數訪問。
預設情況下,電子郵件以純文字形式傳送,因此我們模型示例的樣本檢視可能如下所示
Hi <%= @account.name %>, Thanks for joining our service! Please check back often.
要以 HTML 形式傳送郵件,請確保您的檢視(.rhtml 檔案)生成 HTML 並將媒體型別設定為 html。
class MyMailer < ActionMailer::Base
def signup_notification(recipient)
recipients recipient.email_address_with_name
subject "New account information"
body "account" => recipient
from "system@example.com"
content_type "text/html" #Here's where the magic happens
end
end
您可以顯式指定多部件郵件
class ApplicationMailer < ActionMailer::Base
def signup_notification(recipient)
recipients recipient.email_address_with_name
subject "New account information"
from "system@example.com"
part :content_type => "text/html",
:body => render_message("signup-as-html", :account => recipient)
part "text/plain" do |p|
p.body = render_message("signup-as-plain", :account => recipient)
p.transfer_encoding = "base64"
end
end
end
多部件郵件也可以隱式使用,因為 ActionMailer 將自動檢測並使用多部件模板,其中每個模板都以操作的名稱命名,後跟媒體型別。每個這樣的檢測到的模板將被新增為郵件的單獨部分。
例如,如果存在以下模板
- signup_notification.text.plain.rhtml
- signup_notification.text.html.rhtml
- signup_notification.text.xml.rxml
- signup_notification.text.x-yaml.rhtml
每個都將被渲染並作為郵件的單獨部分新增,具有相應的媒體型別。相同的 body 雜湊將傳遞給每個模板。
可以使用 attachment 方法新增附件。
示例
class ApplicationMailer < ActionMailer::Base
# attachments
def signup_notification(recipient)
recipients recipient.email_address_with_name
subject "New account information"
from "system@example.com"
attachment :content_type => "image/jpeg",
:body => File.read("an-image.jpg")
attachment "application/pdf" do |a|
a.body = generate_your_pdf_here()
end
end
end
這些選項在類級別指定,例如 ActionMailer::Base.template_root = "/my/templates"
- template_roottemplate root 決定模板引用將從中進行的基準。
- logger記錄器用於生成有關郵件執行的資訊(如果可用)。可以設定為 nil 以不進行記錄。與 Ruby 自己的記錄器和 Log4r 記錄器相容。
- server_settings允許對伺服器進行詳細配置
- :address允許您使用遠端郵件伺服器。只需將其從預設的“localhost”設定更改即可。
- :port如果您的郵件伺服器不是在埠 25 上執行,您可以更改它。
- :domain如果您需要指定 HELO 域名,您可以在此處進行。
- :user_name如果您的郵件伺服器需要身份驗證,請在此設定中設定使用者名稱。
- :password如果您的郵件伺服器需要身份驗證,請在此設定中設定密碼。
- :authentication如果您的郵件伺服器需要身份驗證,您需要在此處指定身份驗證型別。這是一個符號,可以是 :plain、:login 或 :cram_md5 之一。
- raise_delivery_errors如果電子郵件無法傳送,是否應該引發錯誤。
- delivery_method定義傳遞方法。可能的值是 :smtp(預設)、:sendmail 和 :test。Sendmail 假設存在於“/usr/sbin/sendmail”中。
- perform_deliveries確定是否實際執行 deliver_* 方法。預設情況下,它們是開啟的,但這可以關閉以幫助功能測試。
- deliveries保留透過 Action Mailer 使用 delivery_method :test 傳送的所有郵件的陣列。最適合單元測試和功能測試。
- default_charset用於主體和編碼主題的預設字元集。預設為 UTF-8。您也可以在方法內部使用 @charset 選擇其他字元集。
- default_content_type用於郵件主體部分的預設媒體型別。預設為“text/plain”。您也可以在方法內部使用 @content_type 選擇其他媒體型別。
- default_mime_version用於郵件的預設 mime 版本。預設為 nil。您也可以在方法內部使用 @mime_version 選擇其他值。在使用多部件郵件時,如果在方法內部未設定 @mime_version,它將被設定為“1.0”。
- default_implicit_parts_order當郵件被隱式構建時(即多個部分從指定了媒體型別的模板中組裝起來),此變數控制部分的排序方式。預設為 ["text/html", "text/enriched", "text/plain"]。陣列中先出現的專案在郵件客戶端中具有更高的優先順序,並且在 mime 編碼郵件中最後出現。您也可以在方法內部使用 @implicit_parts_order 選擇其他順序。