aboutsummaryrefslogblamecommitdiffstats
path: root/railties/doc/guides/html/action_mailer_basics.html
blob: 317f135e4c84934cd7a32087c105276545aa1aa5 (plain) (tree)



















































                                                                                                                                                                                 


                                           


                                             










































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          


























                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
       



                                                                                                                             




          
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Action Mailer Basics</title>
  <!--[if lt IE 8]>
  <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
  <![endif]-->
  <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
  <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
  <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
  <div id="header" >
    <div id="logo">
      <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
    </div>

    <h1 id="site_title"><span>Ruby on Rails</span></h1>
    <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>

    <ul id="navMain">
      <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
      <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
    </ul>
  </div>

  <div id="container">
    
    <div id="sidebar">
      <h2>Chapters</h2>
      <ol>
          <li>
          <a href="#_what_is_action_mailer">What is Action Mailer?</a>
          </li>
          <li>
          <a href="#_quick_walkthrough_to_creating_a_mailer">Quick walkthrough to creating a Mailer</a>
            <ul>
            
              <li><a href="#_1_create_the_mailer">1. Create the mailer:</a></li>
            
              <li><a href="#_2_edit_the_model">2. Edit the model:</a></li>
            
              <li><a href="#_3_create_the_mailer_view">3. Create the mailer view</a></li>
            
              <li><a href="#_4_wire_it_up_so_that_the_system_sends_the_email_when_a_user_signs_up">4. Wire it up so that the system sends the email when a user signs up</a></li>
            
            </ul>
          </li>
          <li>
          <a href="#_mailer_testing">Mailer Testing</a>
          </li>
          <li>
          <a href="#_epilogue">Epilogue</a>
          </li>
          <li>
          <a href="#_changelog">Changelog</a>
          </li>
      </ol>
    </div>
    
    <div id="content">
        <h1>Action Mailer Basics</h1>
      <div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>This guide should provide you with all you need to get started in sending emails from your application, and will also cover how to test your mailers.</p></div>
</div>
</div>
<h2 id="_what_is_action_mailer">1. What is Action Mailer?</h2>
<div class="sectionbody">
<div class="paragraph"><p>Action Mailer allows you to send email from your application using a mailer model and views.
Yes, that is correct, in Rails, emails are used by creating Models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views.</p></div>
</div>
<h2 id="_quick_walkthrough_to_creating_a_mailer">2. Quick walkthrough to creating a Mailer</h2>
<div class="sectionbody">
<div class="paragraph"><p>Let&#8217;s say you want to send a welcome email to a user after they signup. Here is how you would go about this:</p></div>
<h3 id="_1_create_the_mailer">2.1. 1. Create the mailer:</h3>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">.</span>/script/generate mailer UserMailer
exists  app/models<span style="color: #990000">/</span>
create  app/views/user_mailer
exists  test/unit<span style="color: #990000">/</span>
create  test/fixtures/user_mailer
create  app/models/user_mailer<span style="color: #990000">.</span>rb
create  test/unit/user_mailer_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
<div class="paragraph"><p>So we got the model, the fixtures, and the tests all created for us</p></div>
<h3 id="_2_edit_the_model">2.2. 2. Edit the model:</h3>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailer <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>Base

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>Lets add a method called welcome_email, that will send an email to the user&#8217;s registered email address:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailer <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>Base

  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> welcome_email<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
    recipients    user<span style="color: #990000">.</span>email
    from          <span style="color: #FF0000">"My Awesome Site Notifications&lt;notifications@example.com&gt;"</span>
    subject       <span style="color: #FF0000">"Welcome to My Awesome Site"</span>
    sent_on       Time<span style="color: #990000">.</span>now
    body          <span style="color: #FF0000">{</span><span style="color: #990000">:</span>user <span style="color: #990000">=&gt;</span> user<span style="color: #990000">,</span> <span style="color: #990000">:</span>url <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"http://example.com/login"</span><span style="color: #FF0000">}</span>
    content_type  <span style="color: #FF0000">"text/html"</span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>So what do we have here?
recipients: who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"]
from: Who the email will appear to come from in the recipients' mailbox
subject: The subject of the email
sent_on: Timestamp for the email
content_type: The content type, by default is text/plain</p></div>
<div class="paragraph"><p>How about @body[:user]? Well anything you put in the @body hash will appear in the mailer view (more about mailer views below) as an instance variable ready for you to use, ie, in our example the mailer view will have a @user instance variable available for its consumption.</p></div>
<h3 id="_3_create_the_mailer_view">2.3. 3. Create the mailer view</h3>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>The file can look like:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">&lt;!DOCTYPE</span></span> <span style="color: #009900">html</span> <span style="color: #009900">PUBLIC</span> <span style="color: #FF0000">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="color: #FF0000">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="font-weight: bold"><span style="color: #000080">&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
    <span style="font-weight: bold"><span style="color: #0000FF">&lt;meta</span></span> <span style="color: #009900">content</span><span style="color: #990000">=</span><span style="color: #009900">'text/html;</span> <span style="color: #009900">charset</span><span style="color: #990000">=</span><span style="color: #009900">iso-8859-1'</span> <span style="color: #009900">http-equiv</span><span style="color: #990000">=</span><span style="color: #009900">'Content-Type'</span> <span style="font-weight: bold"><span style="color: #0000FF">/&gt;</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
    <span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Welcome to example.com, &lt;%= @user.first_name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>

    <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
      You have successfully signed up to example.com, and your username is: &lt;%= @user.login %&gt;.<span style="font-weight: bold"><span style="color: #0000FF">&lt;br/&gt;</span></span>
      To login to the site, just follow this link: &lt;%= @url %&gt;.
    <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
    <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Thanks for joining and have a great day!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">&lt;/body&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
<h3 id="_4_wire_it_up_so_that_the_system_sends_the_email_when_a_user_signs_up">2.4. 4. Wire it up so that the system sends the email when a user signs up</h3>
<div class="paragraph"><p>There are 3 was to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it&#8217;s wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.</p></div>
<div class="paragraph"><p></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>

Rails<span style="color: #990000">::</span>Initializer<span style="color: #990000">.</span>run <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>config<span style="color: #990000">|</span>

  <span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>

  config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>user_observer

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>

Rails<span style="color: #990000">::</span>Initializer<span style="color: #990000">.</span>run <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>config<span style="color: #990000">|</span>

  <span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>

  config<span style="color: #990000">.</span>load_paths <span style="color: #990000">+=</span> <span style="color: #990000">%</span>W<span style="color: #990000">(#</span><span style="color: #FF0000">{</span>RAILS_ROOT<span style="color: #FF0000">}</span><span style="color: #FF6600">/app/</span>observers<span style="color: #990000">)</span>

  config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>user_observer

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>ALMOST THERE :) Now all we need is that danged observer, and we&#8217;re done:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserObserver <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Observer
  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_create<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
    UserMailer<span style="color: #990000">.</span>deliver_welcome_email<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of rails, we deliver the email identified by welcome_email by calling deliver_welcome_email.</p></div>
<div class="paragraph"><p>That&#8217;s it! Now whenever your users signup, they will be greeted with a nice welcome email. Next up, we&#8217;ll talk about how to test a mailer model.</p></div>
</div>
<h2 id="_mailer_testing">3. Mailer Testing</h2>
<div class="sectionbody">
<div class="paragraph"><p>Testing mailers involves 2 things. One is that the mail was queued and the other that the body contains what we expect it to contain. With that in mind, we could test our example mailer from above like so:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailerTest <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>TestCase
    tests UserMailer

    <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_welcome_email
      user <span style="color: #990000">=</span> users<span style="color: #990000">(:</span>some_user_in_your_fixtures<span style="color: #990000">)</span>

      <span style="font-style: italic"><span style="color: #9A1900"># Send the email, then test that it got queued</span></span>
      email <span style="color: #990000">=</span> UserMailer<span style="color: #990000">.</span>deliver_welcome_email<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
      assert <span style="color: #990000">!</span>ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>deliveries<span style="color: #990000">.</span>empty?

      <span style="font-style: italic"><span style="color: #9A1900"># Test the body of the sent email contains what we expect it to</span></span>
      assert_equal <span style="color: #990000">[</span><span style="color: #009900">@user</span><span style="color: #990000">.</span>email<span style="color: #990000">],</span>                 email<span style="color: #990000">.</span>to
      assert_equal <span style="color: #FF0000">"Welcome to My Awesome Site"</span><span style="color: #990000">,</span>  email<span style="color: #990000">.</span>subject
      assert       email<span style="color: #990000">.</span>body <span style="color: #990000">=~</span> <span style="color: #FF6600">/Welcome to example.com, #{user.first_name}/</span>
    <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>What have we done? Well, we sent the email and stored the returned object in the email variable. We then ensured that it was sent (the first assert), then, in the second batch of assertion, we ensure that the email does indeed contain the values that we expect.</p></div>
</div>
<h2 id="_epilogue">4. Epilogue</h2>
<div class="sectionbody">
<div class="paragraph"><p>This guide presented how to create a mailer and how to test it. In reality, you may find that writing your tests before you actually write your code to be a rewarding experience. It may take some time to get used to TDD (Test Driven Development), but coding this way achieves two major benefits. Firstly, you know that the code does indeed work, because the tests fail (because there&#8217;s no code), then they pass, because the code that satisfies the tests was written. Secondly, when you start with the tests, you don&#8217;t have to make time AFTER you write the code, to write the tests, then never get around to it. The tests are already there and testing has now become part of your coding regimen.</p></div>
</div>
<h2 id="_changelog">5. Changelog</h2>
<div class="sectionbody">
<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213/tickets/25">Lighthouse ticket</a></p></div>
</div>

    </div>
  </div>
</body>
</html>