aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/html/action_mailer_basics.html
blob: 56451818eb4c19a827ae25f94ec926c8c4095b2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!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>
      </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>

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