The simplest way to send an HTML email involves two steps.
text/html
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
The message may be assembled out of explicitly listed parts, each with its own content-type and content.
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
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.
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:
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.
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