Ruby on Rails
HowToSendHtmlEmailsWithActionMailer (Version #32)

The simplest thing that could possibly work

The simplest way to send an HTML email involves two steps.

  1. Put HTML in your mail view
  2. Set the content type to text/html
example

class SignupNotifier < ActionMailer::Base
  def signup_notification(user)
    recipients = user.email
    from = 'webmaster@example.com'
    subject = ‘Welcome!’
    body :name => user.login
    content_type ‘text/html’   #    <== note this line
  end
end

Explicitly specify multipart messages

The message may be assembled out of explicitly listed parts, each with its own content-type and content.

example

    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

Implicitly multipart messages

ActionMailer will automatically detect and use multipart templates,
where each template is named after the name of the action, followed
by the content type. Each such detected template will be added as
a separate part to the message.

example

    def signup_notification(recipient)
      recipients      recipient.email_address_with_name
      subject         "New account information" 
      from            "system@example.com"
      body(:account => “recipient”)
    end

if the following templates existed:

  • signup_notification.text.plain.rhtml
  • signup_notification.text.html.rhtml
  • signup_notification.text.xml.rxml
  • signup_notification.text.x-yaml.rhtml

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.


The ActionMailer API documentation has more examples. Including how to send attachements and various configuration options.


Pre ActionMailer 1.0.0 (6 July, 2005)

Before ActionMailer 1.0.0, a more manual approach was required. You must first get a reference to a TMail object by calling the dynamic create_xxxxx method instead of the deliver_xxxxx method.

Once you have the reference, you can use the set_content_type method. For example:


email = Mailer.create_my_mail(params)
email.set_content_type("text/html")
Mailer.deliver(email)

category:Howto

The simplest thing that could possibly work

The simplest way to send an HTML email involves two steps.

  1. Put HTML in your mail view
  2. Set the content type to text/html
example

class SignupNotifier < ActionMailer::Base
  def signup_notification(user)
    recipients = user.email
    from = 'webmaster@example.com'
    subject = ‘Welcome!’
    body :name => user.login
    content_type ‘text/html’   #    <== note this line
  end
end

Explicitly specify multipart messages

The message may be assembled out of explicitly listed parts, each with its own content-type and content.

example

    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

Implicitly multipart messages

ActionMailer will automatically detect and use multipart templates,
where each template is named after the name of the action, followed
by the content type. Each such detected template will be added as
a separate part to the message.

example

    def signup_notification(recipient)
      recipients      recipient.email_address_with_name
      subject         "New account information" 
      from            "system@example.com"
      body(:account => “recipient”)
    end

if the following templates existed:

  • signup_notification.text.plain.rhtml
  • signup_notification.text.html.rhtml
  • signup_notification.text.xml.rxml
  • signup_notification.text.x-yaml.rhtml

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.


The ActionMailer API documentation has more examples. Including how to send attachements and various configuration options.


Pre ActionMailer 1.0.0 (6 July, 2005)

Before ActionMailer 1.0.0, a more manual approach was required. You must first get a reference to a TMail object by calling the dynamic create_xxxxx method instead of the deliver_xxxxx method.

Once you have the reference, you can use the set_content_type method. For example:


email = Mailer.create_my_mail(params)
email.set_content_type("text/html")
Mailer.deliver(email)

category:Howto