Home
Ruby on Rails
Cheatsheets
To use ActionMailer, you need to create a mailer model. Emails are defined by creating methods within the mailer model which are then used to set variables to be used in the mail template, to change options on the mail, or to add attachments.
ruby script/generate mailer NameOfMailer method1 method2 method3
class OrderMailer < ActionMailer::Base def confirm(order,sent_at = Time.now) subject "Subject line goes here" body :order => order recipients ["bill@microsoft.com", "steve@apple.com"] from "david@dizzy.co.uk" sent_on sent_at end end
Once a mailer action and template are defined, you can deliver your message or create and save it for delivery later by calling the mailer class and prefixing your chosen class method with deliver_ or create_
Notifier.deliver_signup_notification(customer)
mail = Notifier.create_signup_notification(customer) Notifier.deliver(mail)
You can pass the mailer model any variables you need to use in the generation of the email. In the example above we have passed it a variable named customer which could be an instance of an ActiveRecord Customer model. We can then access our customer's details in the mailer model.
There are two ways to send multipart email messages, explicity by manually defining each part, and implicitly by letting ActionMailer do the donkey work.
You can explicitly define multipart messages using the part method...
part "text/plain" do |p| p.body = render_message("signup-as-plain", :account => recipient) p.transfer_encoding = "base64" end part :content_type => "text/html", :body => render_message("signup-as-html", :account => recipient)
ActionMailer will automatically detect and use multipart templates, where each template is named after the name of the method, followed by the content type. Each such detected template will be added as a separate part to the message. For example:
signup_notification.text.plain.erb
signup_notification.text.html.erb
signup_notification.text.xml.builder
Each would be rendered and added as a separate part to the message with the corresponding content type. The same body hash is passed to each template.
Like ActionController, each mailer class has a corresponding view directory in which each method of the class looks for a template with its own name. For example...
|
Mailer model |
Class method |
Corresponding template |
|
|
|
|
|
|
|
|
|
|
|
|
|
recipients = [array] or "string" |
A string containing the email of address of the recipient, or an array of strings containing email addresses of multiple recipients. Will use the email's |
|
sent_on = Time object |
A |
|
subject = "string" |
The subject line to be used to set the email's |
|
from = [array] or "string" |
A string containing the email address to appear on the |
|
body = {hash} |
The body method sets instance variables to be available in the view template. For example, to make the variables order and name accessible as body :order => order, :name => name |
|
attachment = {hash} or block |
Enables you to add attachments to your email message. attachment :content_type => "image/jpeg", :body => File.read("an-image.jpg") attachment "application/pdf" do |a| a.body = generate_your_pdf_here() end |
|
bcc = [array] or "string" |
Blind carbon copy recipients in the same format as |
|
*cc* = [array] or "string" |
Carbon copy recipients in the same format as |
|
content_type = "string" |
Set the content type of the message. Defaults to |
|
headers = {hash} |
A hash containing name/value pairs to be converted into abitrary header lines. For example... headers "X-Mail-Count" => 107370 |
|
mime_version = "string" |
The mime version for the message. Defaults to |
|
charset = "string" |
The charset for the body and to encode the subject. Defaults to |
|
implicit_parts_order = [array] |
When an email is built implicitly, this variable controls how the parts are ordered. Defaults to |
If your view includes URLs from the application, you need to use url_for in the mailer class method instead of in the view template. You can pass the result to the view via the body method. Unlike controllers from ActionPack, the mailer instance doesn't have any context about the incoming request.
body :home_page => url_for(:host => "dizzy.co.uk", :controller => "welcome", :action => "index")
ActionMailer is configured by acessing configuration methods at the class level, for example, ActionMailer::Base.template_root = "/my/templates". These methods allow you to define the overall settings to be used by your application whenever it invokes ActionMailer. Define these settings in your config/environment.rb file using config.action_mailer.method_name_here. If you require different settings for each of your Rails' environments, define settings separately via config/environments.
|
smtp_settings = {hash} |
|
|
sendmail_settings = {hash} |
|
|
raise_delivery_errors = true or false |
Whether or not errors should be raised if the email fails to be delivered. |
|
delivery_method = :smtp, :sendmail or :test |
Defines a delivery method, defaults to |
|
perform_deliveries = true or false |
Determines whether deliver_* methods are actually carried out. By default they are, but this can be turned off to help functional testing. |
|
template_root = "/path" |
The root from which template references will be made |
|
logger |
Used for generation information on the mailing run if available. Can be set to |
|
default_charset = "string" |
the default charset used for the body and to encode the subject. Defaults to |
|
default_mime_version = "string" |
The default mime version used for the message. Defaults to |
|
default_implicit_parts_order = [array] |
When an email is built implicitly, this variable controls how the parts are ordered. Defaults to |
|
default_content_type = "string" |
The default content type used for the main part of the message. Defaults to |
2 MONTHS AGO
Andrew Degenhardt
content type
What are all the content types?