Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming you know how to send normal plain-text emails from Rails using ActionMailer, to get <code>HTML</code> emails working you need to set a content type for your email.</p> <p>For example, your notifer might look like this:</p> <pre><code>class MyMailer &lt; ActionMailer::Base def signup_notification(recipient) recipients recipient.email_address_with_name subject "New account information" from "system@example.com" body :user =&gt; recipient content_type "text/html" end end </code></pre> <p>Note the <code>content_type "text/html"</code> line. This tells ActionMailer to send an email with a content type of <code>text/html</code> instead of the default <code>text/plain</code>.</p> <p>Next you have to make your mailer views output <code>HTML</code>. For example, your view file <code>app/views/my_mailer/signup_notification.html.erb</code> might look like the following:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;style type="text/css" media="screen"&gt; h3 { color: #f00; } ul { list-style: none; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h3&gt;Your account signup details are below&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;Name: &lt;%= @user.name %&gt;&lt;/li&gt; &lt;li&gt;Login: &lt;%= @user.login %&gt;&lt;/li&gt; &lt;li&gt;E-mail: &lt;%= @user.email_address %&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>As you can see the <code>HTML</code> view can include a <code>&lt;style&gt;</code> tag to define basic styles. Not all <code>HTML</code> and <code>CSS</code> is supported, especially across all mail clients, but you should definitely have sufficient formatting control over text styles.</p> <p>Embedding images is a bit tricker if you plan to display attached emails. If you are simply including emails from external sites, you can use an <code>&lt;img /&gt;</code> tag as you usually would in <code>HTML</code>. However, many mail clients will block these images from being displayed until the user authorises it. If you need to display attached images, the Rails plug-in <a href="http://rubyforge.org/frs/?group_id=4569" rel="noreferrer">Inline Attachments</a> might be worth a look.</p> <p>For more information on Rails mailing support, the <a href="http://api.rubyonrails.org/classes/ActionMailer/Base.html" rel="noreferrer">ActionMailer documentation</a> is a great resource</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload