aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/html/action_mailer_basics.html
blob: 22b575af45d2717bf4f5461a35077537e15413c4 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
<!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="#_introduction">Introduction</a>
          </li>
          <li>
          <a href="#_sending_emails">Sending Emails</a>
            <ul>
            
              <li><a href="#_walkthrough_to_generating_a_mailer">Walkthrough to generating a Mailer</a></li>
            
              <li><a href="#_action_mailer_and_dynamic_deliver_methods">Action Mailer and dynamic deliver_ methods</a></li>
            
              <li><a href="#_complete_list_of_actionmailer_user_settable_attributes">Complete List of ActionMailer user-settable attributes</a></li>
            
              <li><a href="#_mailer_views">Mailer Views</a></li>
            
              <li><a href="#_action_mailer_layouts">Action Mailer Layouts</a></li>
            
              <li><a href="#_generating_url_8217_s_in_action_mailer_views">Generating URL&#8217;s in Action Mailer views</a></li>
            
              <li><a href="#_sending_multipart_emails">Sending multipart emails</a></li>
            
              <li><a href="#_sending_emails_with_attachments">Sending emails with attachments</a></li>
            
            </ul>
          </li>
          <li>
          <a href="#_receiving_emails">Receiving Emails</a>
          </li>
          <li>
          <a href="#_using_action_mailer_helpers">Using Action Mailer Helpers</a>
          </li>
          <li>
          <a href="#_action_mailer_configuration">Action Mailer Configuration</a>
            <ul>
            
              <li><a href="#_example_action_mailer_configuration">Example Action Mailer Configuration</a></li>
            
              <li><a href="#_action_mailer_configuration_for_gmail">Action Mailer Configuration for GMail</a></li>
            
              <li><a href="#_configure_action_mailer_to_recognize_haml_templates">Configure Action Mailer to recognize HAML templates</a></li>
            
            </ul>
          </li>
          <li>
          <a href="#_mailer_testing">Mailer Testing</a>
          </li>
          <li>
          <a href="#_epilogue">Epilogue</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 and receiving emails from/to your application, and many internals of the ActionMailer class. It will also cover how to test your mailers.</p></div>
</div>
</div>
<h2 id="_introduction">1. Introduction</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="_sending_emails">2. Sending Emails</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="_walkthrough_to_generating_a_mailer">2.1. Walkthrough to generating a Mailer</h3>
<h4 id="_create_the_mailer">2.1.1. Create the mailer:</h4>
<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>
<h4 id="_edit_the_model">2.1.2. Edit the model:</h4>
<div class="paragraph"><p>If you look at app/models/user_mailer.rb, you will see:</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">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?</p></div>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="20%" />
<col width="80%" />
<tbody valign="top">
<tr>
<td align="left"><p class="table">recipients</p></td>
<td align="left"><p class="table">who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"]</p></td>
</tr>
<tr>
<td align="left"><p class="table">from</p></td>
<td align="left"><p class="table">Who the email will appear to come from in the recipients' mailbox</p></td>
</tr>
<tr>
<td align="left"><p class="table">subject</p></td>
<td align="left"><p class="table">The subject of the email</p></td>
</tr>
<tr>
<td align="left"><p class="table">sent_on</p></td>
<td align="left"><p class="table">Timestamp for the email</p></td>
</tr>
<tr>
<td align="left"><p class="table">content_type</p></td>
<td align="left"><p class="table">The content type, by default is text/plain</p></td>
</tr>
</tbody>
</table>
</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>
<h4 id="_create_the_mailer_view">2.1.3. Create the mailer view</h4>
<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>
<h4 id="_wire_it_up_so_that_the_system_sends_the_email_when_a_user_signs_up">2.1.4. Wire it up so that the system sends the email when a user signs up</h4>
<div class="paragraph"><p>There are 3 ways 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>Let&#8217;s see how we would go about wiring it up using an observer:</p></div>
<div class="paragraph"><p>In config/environment.rb:</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>There was a bit of a debate on where to put observers. Some people put them in app/models, but a cleaner method may be to create an app/observers folder to store all observers, and add that to your load path. Open config/environment.rb and make it 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-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:
Create a file called user_observer in app/models or app/observers depending on where you stored it, and make it 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: #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. The next section will go through this in more detail.</p></div>
<div class="paragraph"><p>That&#8217;s it! Now whenever your users signup, they will be greeted with a nice welcome email.</p></div>
<h3 id="_action_mailer_and_dynamic_deliver_methods">2.2. Action Mailer and dynamic deliver_ methods</h3>
<div class="paragraph"><p>So how does Action Mailer understand this deliver_welcome_email call? If you read the documentation (<a href="http://api.rubyonrails.org/files/vendor/rails/actionmailer/README.html">http://api.rubyonrails.org/files/vendor/rails/actionmailer/README.html</a>), you will find this in the "Sending Emails" section:</p></div>
<div class="paragraph"><p>You never instantiate your mailer class. Rather, your delivery instance
methods are automatically wrapped in class methods that start with the word
deliver_ followed by the name of the mailer method that you would
like to deliver. The signup_notification method defined above is
delivered by invoking Notifier.deliver_signup_notification.</p></div>
<div class="paragraph"><p>So, how exactly does this work?</p></div>
<div class="paragraph"><p>In ActionMailer:Base, you will find this:</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">def</span></span> method_missing<span style="color: #990000">(</span>method_symbol<span style="color: #990000">,</span> <span style="color: #990000">*</span>parameters<span style="color: #990000">)</span><span style="font-style: italic"><span style="color: #9A1900">#:nodoc:</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">case</span></span> method_symbol<span style="color: #990000">.</span>id2name
    <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^create_([_a-z]\w*)/</span>  <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> new<span style="color: #990000">(</span><span style="color: #009900">$1</span><span style="color: #990000">,</span> <span style="color: #990000">*</span>parameters<span style="color: #990000">).</span>mail
    <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF6600">/^deliver_([_a-z]\w*)/</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> new<span style="color: #990000">(</span><span style="color: #009900">$1</span><span style="color: #990000">,</span> <span style="color: #990000">*</span>parameters<span style="color: #990000">).</span>deliver!
    <span style="font-weight: bold"><span style="color: #0000FF">when</span></span> <span style="color: #FF0000">"new"</span> <span style="font-weight: bold"><span style="color: #0000FF">then</span></span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
    <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> <span style="font-weight: bold"><span style="color: #0000FF">super</span></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>Ah, this makes things so much clearer :) so if the method name starts with deliver_ followed by any combination of lowercase letters or underscore, method missing calls new on your mailer class (UserMailer in our example above), sending the combination of lower case letters or underscore, along with the parameter. The resulting object is then sent the deliver! method, which well... delivers it.</p></div>
<h3 id="_complete_list_of_actionmailer_user_settable_attributes">2.3. Complete List of ActionMailer user-settable attributes</h3>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="20%" />
<col width="80%" />
<tbody valign="top">
<tr>
<td align="left"><p class="table">bcc</p></td>
<td align="left"><p class="table">Specify the BCC addresses for the message</p></td>
</tr>
<tr>
<td align="left"><p class="table">body</p></td>
<td align="left"><p class="table">Define the body of the message. This is either a Hash (in which case it specifies the variables to pass to the template when it is rendered), or a string, in which case it specifies the actual text of the message.</p></td>
</tr>
<tr>
<td align="left"><p class="table">cc</p></td>
<td align="left"><p class="table">Specify the CC addresses for the message.</p></td>
</tr>
<tr>
<td align="left"><p class="table">charset</p></td>
<td align="left"><p class="table">Specify the charset to use for the message. This defaults to the default_charset specified for ActionMailer::Base.</p></td>
</tr>
<tr>
<td align="left"><p class="table">content_type</p></td>
<td align="left"><p class="table">Specify the content type for the message. This defaults to &lt;text/plain in most cases, but can be automatically set in some situations.</p></td>
</tr>
<tr>
<td align="left"><p class="table">from</p></td>
<td align="left"><p class="table">Specify the from address for the message.</p></td>
</tr>
<tr>
<td align="left"><p class="table">reply_to</p></td>
<td align="left"><p class="table">Specify the address (if different than the "from" address) to direct replies to this message.</p></td>
</tr>
<tr>
<td align="left"><p class="table">headers</p></td>
<td align="left"><p class="table">Specify additional headers to be added to the message.</p></td>
</tr>
<tr>
<td align="left"><p class="table">implicit_parts_order</p></td>
<td align="left"><p class="table">Specify the order in which parts should be sorted, based on content-type. This defaults to the value for the default_implicit_parts_order.</p></td>
</tr>
<tr>
<td align="left"><p class="table">mime_version</p></td>
<td align="left"><p class="table">Defaults to "1.0", but may be explicitly given if needed.</p></td>
</tr>
<tr>
<td align="left"><p class="table">recipient</p></td>
<td align="left"><p class="table">The recipient addresses for the message, either as a string (for a single address) or an array (for multiple addresses).</p></td>
</tr>
<tr>
<td align="left"><p class="table">sent_on</p></td>
<td align="left"><p class="table">The date on which the message was sent. If not set (the default), the header will be set by the delivery agent.</p></td>
</tr>
<tr>
<td align="left"><p class="table">subject</p></td>
<td align="left"><p class="table">Specify the subject of the message.</p></td>
</tr>
<tr>
<td align="left"><p class="table">template</p></td>
<td align="left"><p class="table">Specify the template name to use for current message. This is the "base" template name, without the extension or directory, and may be used to have multiple mailer methods share the same template.</p></td>
</tr>
</tbody>
</table>
</div>
<h3 id="_mailer_views">2.4. Mailer Views</h3>
<div class="paragraph"><p>Mailer views are located in /app/views/name_of_mailer_class. The specific mailer view is known to the class because it&#8217;s name is the same as the mailer method. So for example, in our example from above, our mailer view for the welcome_email method will be in /app/views/user_mailer/welcome_email.html.erb for the html version and welcome_email.txt.erb for the plain text version.</p></div>
<div class="paragraph"><p>To change the default mailer view for your action you do something 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: #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-style: italic"><span style="color: #9A1900"># change the default from welcome_email.[html, txt].erb</span></span>
    template <span style="color: #FF0000">"some_other_template"</span> <span style="font-style: italic"><span style="color: #9A1900"># this will be in app/views/user_mailer/some_other_template.[html, txt].erb</span></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>
<h3 id="_action_mailer_layouts">2.5. Action Mailer Layouts</h3>
<div class="paragraph"><p>Just like controller views, you can also have mailer layouts. The layout name needs to end in _mailer to be automatically recognized by your mailer as a layout. So in our UserMailer example, we need to call our layout user_mailer.[html,txt].erb. In order to use a different file just use:</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

  layout <span style="color: #FF0000">'awesome'</span> <span style="font-style: italic"><span style="color: #9A1900"># will use awesome.html.erb as the layout</span></span>

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>Just like with controller views, use yield to render the view inside the layout.</p></div>
<h3 id="_generating_url_8217_s_in_action_mailer_views">2.6. Generating URL&#8217;s in Action Mailer views</h3>
<div class="paragraph"><p>URLs can be generated in mailer views using url_for or named routes.
Unlike controllers from Action Pack, the mailer instance doesn&#8217;t have any context about the incoming request, so you&#8217;ll need to provide all of the details needed to generate a URL.</p></div>
<div class="paragraph"><p>When using url_for you&#8217;ll need to provide the :host, :controller, and :action:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;%= url_for(:host =&gt; "example.com", :controller =&gt; "welcome", :action =&gt; "greeting") %&gt;</tt></pre>
</div></div>
<div class="paragraph"><p>When using named routes you only need to supply the :host:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;%= users_url(:host =&gt; "example.com") %&gt;</tt></pre>
</div></div>
<div class="paragraph"><p>You will want to avoid using the name_of_route_path form of named routes because it doesn&#8217;t make sense to generate relative URLs in email messages. The reason that it doesn&#8217;t make sense is because the email is opened on a mail client outside of your environment. Since the email is not being served by your server, a URL like "/users/show/1", will have no context. In order for the email client to properly link to a URL on your server it needs something like "http://yourserver.com/users/show/1".</p></div>
<div class="paragraph"><p>It is also possible to set a default host that will be used in all mailers by setting the :host option in
the ActionMailer::Base.default_url_options hash as follows:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>ActionMailer::Base.default_url_options[:host] = "example.com"</tt></pre>
</div></div>
<div class="paragraph"><p>This can also be set as a configuration option in config/environment.rb:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>config.action_mailer.default_url_options = { :host =&gt; "example.com" }</tt></pre>
</div></div>
<div class="paragraph"><p>If you do decide to set a default :host for your mailers you will want to use the :only_path =&gt; false option when using url_for. This will ensure that absolute URLs are generated because the url_for view helper will, by default, generate relative URLs when a :host option isn&#8217;t explicitly provided.</p></div>
<h3 id="_sending_multipart_emails">2.7. Sending multipart emails</h3>
<div class="paragraph"><p>Action Mailer will automatically send multipart emails if you have different templates for the same action. So, for our UserMailer example, if you have welcome_email.txt.erb and welcome_email.html.erb in app/views/user_mailer, Action Mailer will automatically send a multipart email with the html and text versions setup as different parts.</p></div>
<div class="paragraph"><p>To explicitly specify multipart messages, you can do something 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: #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_address
    subject         <span style="color: #FF0000">"New account information"</span>
    from            <span style="color: #FF0000">"system@example.com"</span>
    content_type    <span style="color: #FF0000">"multipart/alternative"</span>

    part <span style="color: #990000">:</span>content_type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"text/html"</span><span style="color: #990000">,</span>
      <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"&lt;p&gt;html content, can also be the name of an action that you call&lt;p&gt;"</span>

    part <span style="color: #FF0000">"text/plain"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>p<span style="color: #990000">|</span>
      p<span style="color: #990000">.</span>body <span style="color: #990000">=</span> <span style="color: #FF0000">"text content, can also be the name of an action that you call"</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>

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_sending_emails_with_attachments">2.8. Sending emails with attachments</h3>
<div class="paragraph"><p>Attachments can be added by using the attachment method:</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_address
    subject         <span style="color: #FF0000">"New account information"</span>
    from            <span style="color: #FF0000">"system@example.com"</span>
    content_type    <span style="color: #FF0000">"multipart/alternative"</span>

    attachment <span style="color: #990000">:</span>content_type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"image/jpeg"</span><span style="color: #990000">,</span>
      <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> File<span style="color: #990000">.</span>read<span style="color: #990000">(</span><span style="color: #FF0000">"an-image.jpg"</span><span style="color: #990000">)</span>

    attachment <span style="color: #FF0000">"application/pdf"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>a<span style="color: #990000">|</span>
      a<span style="color: #990000">.</span>body <span style="color: #990000">=</span> generate_your_pdf_here<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>

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_receiving_emails">3. Receiving Emails</h2>
<div class="sectionbody">
<div class="paragraph"><p>Receiving and parsing emails with Action Mailer can be a rather complex endeavour. Before your email reaches your Rails app, you would have had to configure your system to somehow forward emails to your app, which needs to be listening for that.
So, to receive emails in your Rails app you&#8217;ll need:</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Configure your email server to forward emails from the address(es) you would like your app to receive to /path/to/app/script/runner 'UserMailer.receive(STDIN.read)'
</p>
</li>
<li>
<p>
Implement a receive method in your mailer
</p>
</li>
</ol></div>
<div class="paragraph"><p>Once a method called receive is defined in any mailer, Action Mailer will parse the raw incoming email into an email object, decode it, instantiate a new mailer, and pass the email object to the mailer object‘s receive method. Here&#8217;s an example:</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> receive<span style="color: #990000">(</span>email<span style="color: #990000">)</span>
    page <span style="color: #990000">=</span> Page<span style="color: #990000">.</span>find_by_address<span style="color: #990000">(</span>email<span style="color: #990000">.</span>to<span style="color: #990000">.</span>first<span style="color: #990000">)</span>
    page<span style="color: #990000">.</span>emails<span style="color: #990000">.</span>create<span style="color: #990000">(</span>
      <span style="color: #990000">:</span>subject <span style="color: #990000">=&gt;</span> email<span style="color: #990000">.</span>subject<span style="color: #990000">,</span>
      <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> email<span style="color: #990000">.</span>body
    <span style="color: #990000">)</span>

    <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> email<span style="color: #990000">.</span>has_attachments?
      <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> attachment <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> email<span style="color: #990000">.</span>attachments
        page<span style="color: #990000">.</span>attachments<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span>
          <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> attachment<span style="color: #990000">,</span>
          <span style="color: #990000">:</span>description <span style="color: #990000">=&gt;</span> email<span style="color: #990000">.</span>subject
        <span style="color: #FF0000">}</span><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>
  <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>
<h2 id="_using_action_mailer_helpers">4. Using Action Mailer Helpers</h2>
<div class="sectionbody">
<div class="paragraph"><p>Action Mailer classes have 4 helper methods available to them:</p></div>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="20%" />
<col width="80%" />
<tbody valign="top">
<tr>
<td align="left"><p class="table">add_template_helper(helper_module)</p></td>
<td align="left"><p class="table">Makes all the (instance) methods in the helper module available to templates rendered through this controller.</p></td>
</tr>
<tr>
<td align="left"><p class="table">helper(*args, &amp;block)</p></td>
<td align="left"><p class="table">Declare a helper:
  helper :foo
requires <em>foo_helper</em> and includes FooHelper in the template class.
  helper FooHelper
includes FooHelper in the template class.
evaluates the block in the template class, adding method foo.
  helper(:three, BlindHelper) { def mice() <em>mice</em> end }
does all three.</p></td>
</tr>
<tr>
<td align="left"><p class="table">helper_method</p></td>
<td align="left"><p class="table">Declare a controller method as a helper.  For example,
  helper_method :link_to
  def link_to(name, options) ... end
makes the link_to controller method available in the view.</p></td>
</tr>
<tr>
<td align="left"><p class="table">helper_attr</p></td>
<td align="left"><p class="table">Declare a controller attribute as a helper.  For example,
  helper_attr :name
  attr_accessor :name
makes the name and name= controller methods available in the view.
The is a convenience wrapper for helper_method.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2 id="_action_mailer_configuration">5. Action Mailer Configuration</h2>
<div class="sectionbody">
<div class="paragraph"><p>The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc...)</p></div>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="20%" />
<col width="80%" />
<tbody valign="top">
<tr>
<td align="left"><p class="table">template_root</p></td>
<td align="left"><div><div class="paragraph"><p>Determines the base from which template references will be made.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">logger</p></td>
<td align="left"><div><div class="paragraph"><p>the logger is used for generating information on the mailing run if available.
  Can be set to nil for no logging. Compatible with both Ruby&#8217;s own Logger and Log4r loggers.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">smtp_settings</p></td>
<td align="left"><div><div class="paragraph"><p>Allows detailed configuration for :smtp delivery method:</p></div>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="20%" />
<col width="80%" />
<tbody valign="top">
<tr>
<td align="left"><p class="table">:address</p></td>
<td align="left"><p class="table">Allows you to use a remote mail server. Just change it from its default "localhost" setting.</p></td>
</tr>
<tr>
<td align="left"><p class="table">:port</p></td>
<td align="left"><p class="table">On the off chance that your mail server doesn&#8217;t run on port 25, you can change it.</p></td>
</tr>
<tr>
<td align="left"><p class="table">:domain</p></td>
<td align="left"><p class="table">If you need to specify a HELO domain, you can do it here.</p></td>
</tr>
<tr>
<td align="left"><p class="table">:user_name</p></td>
<td align="left"><p class="table">If your mail server requires authentication, set the username in this setting.</p></td>
</tr>
<tr>
<td align="left"><p class="table">:password</p></td>
<td align="left"><p class="table">If your mail server requires authentication, set the password in this setting.</p></td>
</tr>
<tr>
<td align="left"><p class="table">:authentication</p></td>
<td align="left"><p class="table">If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain, :login, :cram_md5.</p></td>
</tr>
</tbody>
</table>
</div></div></td>
</tr>
<tr>
<td align="left"><p class="table">sendmail_settings</p></td>
<td align="left"><div><div class="paragraph"><p>Allows you to override options for the :sendmail delivery method.</p></div>
<div class="tableblock">
<table rules="all"
width="100%"
frame="border"
cellspacing="0" cellpadding="4">
<col width="20%" />
<col width="80%" />
<tbody valign="top">
<tr>
<td align="left"><p class="table">:location</p></td>
<td align="left"><p class="table">The location of the sendmail executable. Defaults to /usr/sbin/sendmail.</p></td>
</tr>
<tr>
<td align="left"><p class="table">:arguments</p></td>
<td align="left"><p class="table">The command line arguments. Defaults to -i -t.</p></td>
</tr>
</tbody>
</table>
</div></div></td>
</tr>
<tr>
<td align="left"><p class="table">raise_delivery_errors</p></td>
<td align="left"><div><div class="paragraph"><p>Whether or not errors should be raised if the email fails to be delivered.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">delivery_method</p></td>
<td align="left"><div><div class="paragraph"><p>Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">perform_deliveries</p></td>
<td align="left"><div><div class="paragraph"><p>Determines whether deliver_* methods are actually carried out. By default they are,
  but this can be turned off to help functional testing.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">deliveries</p></td>
<td align="left"><div><div class="paragraph"><p>Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful
  for unit and functional testing.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">default_charset</p></td>
<td align="left"><div><div class="paragraph"><p>The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
  pick a different charset from inside a method with charset.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">default_content_type</p></td>
<td align="left"><div><div class="paragraph"><p>The default content type used for the main part of the message. Defaults to "text/plain". You
  can also pick a different content type from inside a method with content_type.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">default_mime_version</p></td>
<td align="left"><div><div class="paragraph"><p>The default mime version used for the message. Defaults to 1.0. You
  can also pick a different value from inside a method with mime_version.</p></div></div></td>
</tr>
<tr>
<td align="left"><p class="table">default_implicit_parts_order</p></td>
<td align="left"><div><div class="paragraph"><p>When a message is built implicitly (i.e. multiple parts are assembled from templates
  which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
  ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client
  and appear last in the mime encoded message. You can also pick a different order from inside a method with
  implicit_parts_order.</p></div></div></td>
</tr>
</tbody>
</table>
</div>
<h3 id="_example_action_mailer_configuration">5.1. Example Action Mailer Configuration</h3>
<div class="paragraph"><p>An example would be:</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>ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>delivery_method <span style="color: #990000">=</span> <span style="color: #990000">:</span>sendmail
ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>sendmail_settings <span style="color: #990000">=</span> <span style="color: #FF0000">{</span>
  <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/usr/sbin/sendmail'</span><span style="color: #990000">,</span>
  <span style="color: #990000">:</span>arguments <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'-i -t'</span>
<span style="color: #FF0000">}</span>
ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>perform_deliveries <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>raise_delivery_errors <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>default_charset <span style="color: #990000">=</span> <span style="color: #FF0000">"iso-8859-1"</span></tt></pre></div></div>
<h3 id="_action_mailer_configuration_for_gmail">5.2. Action Mailer Configuration for GMail</h3>
<div class="paragraph"><p>Instructions copied from <a href="http://http://www.fromjavatoruby.com/2008/11/actionmailer-with-gmail-must-issue.html">http://http://www.fromjavatoruby.com/2008/11/actionmailer-with-gmail-must-issue.html</a></p></div>
<div class="paragraph"><p>First you must install the action_mailer_tls plugin from <a href="http://code.openrain.com/rails/action_mailer_tls/">http://code.openrain.com/rails/action_mailer_tls/</a>, then all you have to do is configure action mailer.</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>ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>smtp_settings <span style="color: #990000">=</span> <span style="color: #FF0000">{</span>
  <span style="color: #990000">:</span>address        <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"smtp.gmail.com"</span><span style="color: #990000">,</span>
  <span style="color: #990000">:</span>port           <span style="color: #990000">=&gt;</span> <span style="color: #993399">587</span><span style="color: #990000">,</span>
  <span style="color: #990000">:</span>domain         <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"domain.com"</span><span style="color: #990000">,</span>
  <span style="color: #990000">:</span>user_name      <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"user@domain.com"</span><span style="color: #990000">,</span>
  <span style="color: #990000">:</span>password       <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"password"</span><span style="color: #990000">,</span>
  <span style="color: #990000">:</span>authentication <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>plain
<span style="color: #FF0000">}</span></tt></pre></div></div>
<h3 id="_configure_action_mailer_to_recognize_haml_templates">5.3. Configure Action Mailer to recognize HAML templates</h3>
<div class="paragraph"><p>In environment.rb, add the following line:</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>ActionMailer<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>register_template_extension<span style="color: #990000">(</span><span style="color: #FF0000">'haml'</span><span style="color: #990000">)</span></tt></pre></div></div>
</div>
<h2 id="_mailer_testing">6. 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">7. 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>

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