railties 3.0.11 → 3.0.12.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/rails/version.rb +2 -2
  2. metadata +305 -461
  3. data/guides/output/2_2_release_notes.html +0 -565
  4. data/guides/output/2_3_release_notes.html +0 -713
  5. data/guides/output/3_0_release_notes.html +0 -652
  6. data/guides/output/3_1_release_notes.html +0 -670
  7. data/guides/output/action_controller_overview.html +0 -925
  8. data/guides/output/action_mailer_basics.html +0 -658
  9. data/guides/output/action_view_overview.html +0 -1471
  10. data/guides/output/active_model_basics.html +0 -349
  11. data/guides/output/active_record_basics.html +0 -364
  12. data/guides/output/active_record_querying.html +0 -1272
  13. data/guides/output/active_record_validations_callbacks.html +0 -1292
  14. data/guides/output/active_resource_basics.html +0 -252
  15. data/guides/output/active_support_core_extensions.html +0 -3374
  16. data/guides/output/ajax_on_rails.html +0 -412
  17. data/guides/output/api_documentation_guidelines.html +0 -317
  18. data/guides/output/asset_pipeline.html +0 -691
  19. data/guides/output/association_basics.html +0 -1742
  20. data/guides/output/caching_with_rails.html +0 -533
  21. data/guides/output/command_line.html +0 -662
  22. data/guides/output/configuring.html +0 -811
  23. data/guides/output/contribute.html +0 -216
  24. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  25. data/guides/output/credits.html +0 -210
  26. data/guides/output/debugging_rails_applications.html +0 -791
  27. data/guides/output/engines.html +0 -673
  28. data/guides/output/form_helpers.html +0 -850
  29. data/guides/output/generators.html +0 -725
  30. data/guides/output/getting_started.html +0 -1980
  31. data/guides/output/i18n.html +0 -1054
  32. data/guides/output/images/belongs_to.png +0 -0
  33. data/guides/output/images/book_icon.gif +0 -0
  34. data/guides/output/images/bullet.gif +0 -0
  35. data/guides/output/images/challenge.png +0 -0
  36. data/guides/output/images/chapters_icon.gif +0 -0
  37. data/guides/output/images/check_bullet.gif +0 -0
  38. data/guides/output/images/credits_pic_blank.gif +0 -0
  39. data/guides/output/images/csrf.png +0 -0
  40. data/guides/output/images/customized_error_messages.png +0 -0
  41. data/guides/output/images/edge_badge.png +0 -0
  42. data/guides/output/images/error_messages.png +0 -0
  43. data/guides/output/images/feature_tile.gif +0 -0
  44. data/guides/output/images/footer_tile.gif +0 -0
  45. data/guides/output/images/fxn.png +0 -0
  46. data/guides/output/images/grey_bullet.gif +0 -0
  47. data/guides/output/images/habtm.png +0 -0
  48. data/guides/output/images/has_many.png +0 -0
  49. data/guides/output/images/has_many_through.png +0 -0
  50. data/guides/output/images/has_one.png +0 -0
  51. data/guides/output/images/has_one_through.png +0 -0
  52. data/guides/output/images/header_backdrop.png +0 -0
  53. data/guides/output/images/header_tile.gif +0 -0
  54. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  55. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  57. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  58. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  59. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  60. data/guides/output/images/icons/README +0 -5
  61. data/guides/output/images/icons/callouts/1.png +0 -0
  62. data/guides/output/images/icons/callouts/10.png +0 -0
  63. data/guides/output/images/icons/callouts/11.png +0 -0
  64. data/guides/output/images/icons/callouts/12.png +0 -0
  65. data/guides/output/images/icons/callouts/13.png +0 -0
  66. data/guides/output/images/icons/callouts/14.png +0 -0
  67. data/guides/output/images/icons/callouts/15.png +0 -0
  68. data/guides/output/images/icons/callouts/2.png +0 -0
  69. data/guides/output/images/icons/callouts/3.png +0 -0
  70. data/guides/output/images/icons/callouts/4.png +0 -0
  71. data/guides/output/images/icons/callouts/5.png +0 -0
  72. data/guides/output/images/icons/callouts/6.png +0 -0
  73. data/guides/output/images/icons/callouts/7.png +0 -0
  74. data/guides/output/images/icons/callouts/8.png +0 -0
  75. data/guides/output/images/icons/callouts/9.png +0 -0
  76. data/guides/output/images/icons/caution.png +0 -0
  77. data/guides/output/images/icons/example.png +0 -0
  78. data/guides/output/images/icons/home.png +0 -0
  79. data/guides/output/images/icons/important.png +0 -0
  80. data/guides/output/images/icons/next.png +0 -0
  81. data/guides/output/images/icons/note.png +0 -0
  82. data/guides/output/images/icons/prev.png +0 -0
  83. data/guides/output/images/icons/tip.png +0 -0
  84. data/guides/output/images/icons/up.png +0 -0
  85. data/guides/output/images/icons/warning.png +0 -0
  86. data/guides/output/images/jaimeiniesta.jpg +0 -0
  87. data/guides/output/images/nav_arrow.gif +0 -0
  88. data/guides/output/images/polymorphic.png +0 -0
  89. data/guides/output/images/posts_index.png +0 -0
  90. data/guides/output/images/radar.png +0 -0
  91. data/guides/output/images/rails_guides_logo.gif +0 -0
  92. data/guides/output/images/rails_logo_remix.gif +0 -0
  93. data/guides/output/images/rails_welcome.png +0 -0
  94. data/guides/output/images/session_fixation.png +0 -0
  95. data/guides/output/images/tab_grey.gif +0 -0
  96. data/guides/output/images/tab_info.gif +0 -0
  97. data/guides/output/images/tab_note.gif +0 -0
  98. data/guides/output/images/tab_red.gif +0 -0
  99. data/guides/output/images/tab_yellow.gif +0 -0
  100. data/guides/output/images/tab_yellow.png +0 -0
  101. data/guides/output/images/validation_error_messages.png +0 -0
  102. data/guides/output/images/vijaydev.jpg +0 -0
  103. data/guides/output/index.html +0 -300
  104. data/guides/output/initialization.html +0 -1087
  105. data/guides/output/javascripts/guides.js +0 -7
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  131. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  132. data/guides/output/layout.html +0 -312
  133. data/guides/output/layouts_and_rendering.html +0 -1257
  134. data/guides/output/migrations.html +0 -751
  135. data/guides/output/nested_model_forms.html +0 -350
  136. data/guides/output/performance_testing.html +0 -858
  137. data/guides/output/plugins.html +0 -590
  138. data/guides/output/rails_application_templates.html +0 -368
  139. data/guides/output/rails_on_rack.html +0 -408
  140. data/guides/output/routing.html +0 -1246
  141. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  142. data/guides/output/security.html +0 -968
  143. data/guides/output/stylesheets/fixes.css +0 -16
  144. data/guides/output/stylesheets/main.css +0 -445
  145. data/guides/output/stylesheets/print.css +0 -52
  146. data/guides/output/stylesheets/reset.css +0 -43
  147. data/guides/output/stylesheets/style.css +0 -13
  148. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  156. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  165. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  166. data/guides/output/testing.html +0 -1182
@@ -1,658 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: Action Mailer Basics</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>Action Mailer Basics</h2>
99
- <p>This guide should provide you with all you need to get started in sending and receiving emails from and to your application, and many internals of Action Mailer. It also covers how to test your mailers.</p>
100
-
101
- <div id="subCol">
102
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
103
- <ol class="chapters">
104
- <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="#auto-encoding-header-values">Auto encoding header values</a></li> <li><a href="#complete-list-of-action-mailer-methods">Complete List of Action Mailer Methods</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-urls-in-action-mailer-views">Generating URLs 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></ul></li><li><a href="#mailer-testing">Mailer Testing</a></li></ol></div>
105
- </div>
106
- </div>
107
-
108
- <div id="container">
109
- <div class="wrapper">
110
- <div id="mainCol">
111
- <div class="warning"><p>This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.</p></div>
112
- <h3 id="introduction">1 Introduction</h3>
113
- <p>Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from <tt>ActionMailer::Base</tt> and live in <tt>app/mailers</tt>. Those mailers have associated views that appear alongside controller views in <tt>app/views</tt>.</p>
114
- <h3 id="sending-emails">2 Sending Emails</h3>
115
- <p>This section will provide a step-by-step guide to creating a mailer and its views.</p>
116
- <h4 id="walkthrough-to-generating-a-mailer">2.1 Walkthrough to Generating a Mailer</h4>
117
- <h5 id="create-the-mailer">2.1.1 Create the Mailer</h5>
118
- <div class="code_container">
119
- <pre class="brush: plain; gutter: false; toolbar: false">
120
- $ rails generate mailer UserMailer
121
- create app/mailers/user_mailer.rb
122
- invoke erb
123
- create app/views/user_mailer
124
- invoke test_unit
125
- create test/functional/user_mailer_test.rb
126
- </pre>
127
- </div>
128
- <p>So we got the mailer, the views, and the tests.</p>
129
- <h5 id="edit-the-mailer">2.1.2 Edit the Mailer</h5>
130
- <p><tt>app/mailers/user_mailer.rb</tt> contains an empty mailer:</p>
131
- <div class="code_container">
132
- <pre class="brush: ruby; gutter: false; toolbar: false">
133
- class UserMailer &lt; ActionMailer::Base
134
- default :from =&gt; &quot;from@example.com&quot;
135
- end
136
- </pre>
137
- </div>
138
- <p>Let&#8217;s add a method called <tt>welcome_email</tt>, that will send an email to the user&#8217;s registered email address:</p>
139
- <div class="code_container">
140
- <pre class="brush: ruby; gutter: false; toolbar: false">
141
- class UserMailer &lt; ActionMailer::Base
142
- default :from =&gt; &quot;notifications@example.com&quot;
143
-
144
- def welcome_email(user)
145
- @user = user
146
- @url = &quot;http://example.com/login&quot;
147
- mail(:to =&gt; user.email, :subject =&gt; &quot;Welcome to My Awesome Site&quot;)
148
- end
149
- end
150
- </pre>
151
- </div>
152
- <p>Here is a quick explanation of the items presented in the preceding method. For a full list of all available options, please have a look further down at the Complete List of Action Mailer user-settable attributes section.</p>
153
- <ul>
154
- <li><tt>default Hash</tt> &#8211; This is a hash of default values for any email you send, in this case we are setting the <tt>:from</tt> header to a value for all messages in this class, this can be overridden on a per email basis</li>
155
- <li><tt>mail</tt> &#8211; The actual email message, we are passing the <tt>:to</tt> and <tt>:subject</tt> headers in.</li>
156
- </ul>
157
- <p>Just like controllers, any instance variables we define in the method become available for use in the views.</p>
158
- <h5 id="create-a-mailer-view">2.1.3 Create a Mailer View</h5>
159
- <p>Create a file called <tt>welcome_email.html.erb</tt> in <tt>app/views/user_mailer/</tt>. This will be the template used for the email, formatted in <span class="caps">HTML</span>:</p>
160
- <div class="code_container">
161
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
162
- &lt;!DOCTYPE html&gt;
163
- &lt;html&gt;
164
- &lt;head&gt;
165
- &lt;meta content=&quot;text/html; charset=UTF-8&quot; http-equiv=&quot;Content-Type&quot; /&gt;
166
- &lt;/head&gt;
167
- &lt;body&gt;
168
- &lt;h1&gt;Welcome to example.com, &lt;%= @user.name %&gt;&lt;/h1&gt;
169
- &lt;p&gt;
170
- You have successfully signed up to example.com,
171
- your username is: &lt;%= @user.login %&gt;.&lt;br/&gt;
172
- &lt;/p&gt;
173
- &lt;p&gt;
174
- To login to the site, just follow this link: &lt;%= @url %&gt;.
175
- &lt;/p&gt;
176
- &lt;p&gt;Thanks for joining and have a great day!&lt;/p&gt;
177
- &lt;/body&gt;
178
- &lt;/html&gt;
179
- </pre>
180
- </div>
181
- <p>It is also a good idea to make a text part for this email, to do this, create a file called <tt>welcome_email.text.erb</tt> in <tt>app/views/user_mailer/</tt>:</p>
182
- <div class="code_container">
183
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
184
- Welcome to example.com, &lt;%= @user.name %&gt;
185
- ===============================================
186
-
187
- You have successfully signed up to example.com,
188
- your username is: &lt;%= @user.login %&gt;.
189
-
190
- To login to the site, just follow this link: &lt;%= @url %&gt;.
191
-
192
- Thanks for joining and have a great day!
193
- </pre>
194
- </div>
195
- <p>When you call the <tt>mail</tt> method now, Action Mailer will detect the two templates (text and <span class="caps">HTML</span>) and automatically generate a <tt>multipart/alternative</tt> email.</p>
196
- <h5 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</h5>
197
- <p>There are several ways to do this, some people create Rails Observers to fire off emails, others do it inside of the User Model. However, in Rails 3, mailers are really just another way to render a view. Instead of rendering a view and sending out the <span class="caps">HTTP</span> protocol, they are just sending it out through the Email protocols instead. Due to this, it makes sense to just have your controller tell the mailer to send an email when a user is successfully created.</p>
198
- <p>Setting this up is painfully simple.</p>
199
- <p>First off, we need to create a simple <tt>User</tt> scaffold:</p>
200
- <div class="code_container">
201
- <pre class="brush: plain; gutter: false; toolbar: false">
202
- $ rails generate scaffold user name:string email:string login:string
203
- $ rake db:migrate
204
- </pre>
205
- </div>
206
- <p>Now that we have a user model to play with, we will just edit the <tt>app/controllers/users_controller.rb</tt> make it instruct the UserMailer to deliver an email to the newly created user by editing the create action and inserting a call to <tt>UserMailer.welcome_email</tt> right after the user is successfully saved:</p>
207
- <div class="code_container">
208
- <pre class="brush: ruby; gutter: false; toolbar: false">
209
- class UsersController &lt; ApplicationController
210
- # POST /users
211
- # POST /users.json
212
- def create
213
- @user = User.new(params[:user])
214
-
215
- respond_to do |format|
216
- if @user.save
217
- # Tell the UserMailer to send a welcome Email after save
218
- UserMailer.welcome_email(@user).deliver
219
-
220
- format.html { redirect_to(@user, :notice =&gt; 'User was successfully created.') }
221
- format.json { render :json =&gt; @user, :status =&gt; :created, :location =&gt; @user }
222
- else
223
- format.html { render :action =&gt; &quot;new&quot; }
224
- format.json { render :json =&gt; @user.errors, :status =&gt; :unprocessable_entity }
225
- end
226
- end
227
- end
228
- end
229
- </pre>
230
- </div>
231
- <p>This provides a much simpler implementation that does not require the registering of observers and the like.</p>
232
- <p>The method <tt>welcome_email</tt> returns a <tt>Mail::Message</tt> object which can then just be told <tt>deliver</tt> to send itself out.</p>
233
- <div class="note"><p>In previous versions of Rails, you would call <tt>deliver_welcome_email</tt> or <tt>create_welcome_email</tt>. This has been deprecated in Rails 3.0 in favour of just calling the method name itself.</p></div>
234
- <div class="warning"><p>Sending out an email should only take a fraction of a second, but if you are planning on sending out many emails, or you have a slow domain resolution service, you might want to investigate using a background process like Delayed Job.</p></div>
235
- <h4 id="auto-encoding-header-values">2.2 Auto encoding header values</h4>
236
- <p>Action Mailer now handles the auto encoding of multibyte characters inside of headers and bodies.</p>
237
- <p>If you are using <span class="caps">UTF</span>-8 as your character set, you do not have to do anything special, just go ahead and send in <span class="caps">UTF</span>-8 data to the address fields, subject, keywords, filenames or body of the email and Action Mailer will auto encode it into quoted printable for you in the case of a header field or Base64 encode any body parts that are non US-<span class="caps">ASCII</span>.</p>
238
- <p>For more complex examples such as defining alternate character sets or self encoding text first, please refer to the Mail library.</p>
239
- <h4 id="complete-list-of-action-mailer-methods">2.3 Complete List of Action Mailer Methods</h4>
240
- <p>There are just three methods that you need to send pretty much any email message:</p>
241
- <ul>
242
- <li><tt>headers</tt> &#8211; Specifies any header on the email you want, you can pass a hash of header field names and value pairs, or you can call <tt>headers[:field_name] = &#8216;value&#8217;</tt></li>
243
- <li><tt>attachments</tt> &#8211; Allows you to add attachments to your email, for example <tt>attachments[&#8216;file-name.jpg&#8217;] = File.read(&#8216;file-name.jpg&#8217;)</tt></li>
244
- <li><tt>mail</tt> &#8211; Sends the actual email itself. You can pass in headers as a hash to the mail method as a parameter, mail will then create an email, either plain text, or multipart, depending on what email templates you have defined.</li>
245
- </ul>
246
- <h5 id="custom-headers">2.3.1 Custom Headers</h5>
247
- <p>Defining custom headers are simple, you can do it one of three ways:</p>
248
- <ul>
249
- <li>Defining a header field as a parameter to the <tt>mail</tt> method:</li>
250
- </ul>
251
- <div class="code_container">
252
- <pre class="brush: ruby; gutter: false; toolbar: false">
253
- mail(&quot;X-Spam&quot; =&gt; value)
254
- </pre>
255
- </div>
256
- <ul>
257
- <li>Passing in a key value assignment to the <tt>headers</tt> method:</li>
258
- </ul>
259
- <div class="code_container">
260
- <pre class="brush: ruby; gutter: false; toolbar: false">
261
- headers[&quot;X-Spam&quot;] = value
262
- </pre>
263
- </div>
264
- <ul>
265
- <li>Passing a hash of key value pairs to the <tt>headers</tt> method:</li>
266
- </ul>
267
- <div class="code_container">
268
- <pre class="brush: ruby; gutter: false; toolbar: false">
269
- headers {&quot;X-Spam&quot; =&gt; value, &quot;X-Special&quot; =&gt; another_value}
270
- </pre>
271
- </div>
272
- <div class="info"><p>All <tt>X-Value</tt> headers per the RFC2822 can appear more than one time. If you want to delete an <tt>X-Value</tt> header, you need to assign it a value of <tt>nil</tt>.</p></div>
273
- <h5 id="adding-attachments">2.3.2 Adding Attachments</h5>
274
- <p>Adding attachments has been simplified in Action Mailer 3.0.</p>
275
- <ul>
276
- <li>Pass the file name and content and Action Mailer and the Mail gem will automatically guess the mime_type, set the encoding and create the attachment.</li>
277
- </ul>
278
- <div class="code_container">
279
- <pre class="brush: ruby; gutter: false; toolbar: false">
280
- attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
281
- </pre>
282
- </div>
283
- <div class="note"><p>Mail will automatically Base64 encode an attachment, if you want something different, pre-encode your content and pass in the encoded content and encoding in a <tt>Hash</tt> to the <tt>attachments</tt> method.</p></div>
284
- <ul>
285
- <li>Pass the file name and specify headers and content and Action Mailer and Mail will use the settings you pass in.</li>
286
- </ul>
287
- <div class="code_container">
288
- <pre class="brush: ruby; gutter: false; toolbar: false">
289
- encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
290
- attachments['filename.jpg'] = {:mime_type =&gt; 'application/x-gzip',
291
- :encoding =&gt; 'SpecialEncoding',
292
- :content =&gt; encoded_content }
293
- </pre>
294
- </div>
295
- <div class="note"><p>If you specify an encoding, Mail will assume that your content is already encoded and not try to Base64 encode it.</p></div>
296
- <h5 id="making-inline-attachments">2.3.3 Making Inline Attachments</h5>
297
- <p>Action Mailer 3.0 makes inline attachments, which involved a lot of hacking in pre 3.0 versions, much simpler and trivial as they should be.</p>
298
- <ul>
299
- <li>Firstly, to tell Mail to turn an attachment into an inline attachment, you just call <tt>#inline</tt> on the attachments method within your Mailer:</li>
300
- </ul>
301
- <div class="code_container">
302
- <pre class="brush: ruby; gutter: false; toolbar: false">
303
- def welcome
304
- attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
305
- end
306
- </pre>
307
- </div>
308
- <ul>
309
- <li>Then in your view, you can just reference <tt>attachments[]</tt> as a hash and specify which attachment you want to show, calling <tt>url</tt> on it and then passing the result into the <tt>image_tag</tt> method:</li>
310
- </ul>
311
- <div class="code_container">
312
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
313
- &lt;p&gt;Hello there, this is our image&lt;/p&gt;
314
-
315
- &lt;%= image_tag attachments['image.jpg'].url %&gt;
316
- </pre>
317
- </div>
318
- <ul>
319
- <li>As this is a standard call to <tt>image_tag</tt> you can pass in an options hash after the attachment url as you could for any other image:</li>
320
- </ul>
321
- <div class="code_container">
322
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
323
- &lt;p&gt;Hello there, this is our image&lt;/p&gt;
324
-
325
- &lt;%= image_tag attachments['image.jpg'].url, :alt =&gt; 'My Photo',
326
- :class =&gt; 'photos' %&gt;
327
- </pre>
328
- </div>
329
- <h5 id="sending-email-to-multiple-recipients">2.3.4 Sending Email To Multiple Recipients</h5>
330
- <p>It is possible to send email to one or more recipients in one email (for e.g. informing all admins of a new signup) by setting the list of emails to the <tt>:to</tt> key. The list of emails can be an array of email addresses or a single string with the addresses separated by commas.</p>
331
- <div class="code_container">
332
- <pre class="brush: ruby; gutter: false; toolbar: false">
333
- class AdminMailer &lt; ActionMailer::Base
334
- default :to =&gt; Admin.all.map(&amp;:email),
335
- :from =&gt; &quot;notification@example.com&quot;
336
-
337
- def new_registration(user)
338
- @user = user
339
- mail(:subject =&gt; &quot;New User Signup: #{@user.email}&quot;)
340
- end
341
- end
342
- </pre>
343
- </div>
344
- <p>The same format can be used to set carbon copy (Cc:) and blind carbon copy (Bcc:) recipients, by using the <tt>:cc</tt> and <tt>:bcc</tt> keys respectively.</p>
345
- <h5 id="sending-email-with-name">2.3.5 Sending Email With Name</h5>
346
- <p>Sometimes you wish to show the name of the person instead of just their email address when they receive the email. The trick to doing that is
347
- to format the email address in the format <tt>&#8220;Name &lt;email&gt;&#8221;</tt>.</p>
348
- <div class="code_container">
349
- <pre class="brush: ruby; gutter: false; toolbar: false">
350
- def welcome_email(user)
351
- @user = user
352
- email_with_name = &quot;#{@user.name} &lt;#{@user.email}&gt;&quot;
353
- mail(:to =&gt; email_with_name, :subject =&gt; &quot;Welcome to My Awesome Site&quot;)
354
- end
355
- </pre>
356
- </div>
357
- <h4 id="mailer-views">2.4 Mailer Views</h4>
358
- <p>Mailer views are located in the <tt>app/views/name_of_mailer_class</tt> directory. The specific mailer view is known to the class because its name is the same as the mailer method. In our example from above, our mailer view for the <tt>welcome_email</tt> method will be in <tt>app/views/user_mailer/welcome_email.html.erb</tt> for the <span class="caps">HTML</span> version and <tt>welcome_email.text.erb</tt> for the plain text version.</p>
359
- <p>To change the default mailer view for your action you do something like:</p>
360
- <div class="code_container">
361
- <pre class="brush: ruby; gutter: false; toolbar: false">
362
- class UserMailer &lt; ActionMailer::Base
363
- default :from =&gt; &quot;notifications@example.com&quot;
364
-
365
- def welcome_email(user)
366
- @user = user
367
- @url = &quot;http://example.com/login&quot;
368
- mail(:to =&gt; user.email,
369
- :subject =&gt; &quot;Welcome to My Awesome Site&quot;,
370
- :template_path =&gt; 'notifications',
371
- :template_name =&gt; 'another')
372
- end
373
- end
374
- </pre>
375
- </div>
376
- <p>In this case it will look for templates at <tt>app/views/notifications</tt> with name <tt>another</tt>.</p>
377
- <p>If you want more flexibility you can also pass a block and render specific templates or even render inline or text without using a template file:</p>
378
- <div class="code_container">
379
- <pre class="brush: ruby; gutter: false; toolbar: false">
380
- class UserMailer &lt; ActionMailer::Base
381
- default :from =&gt; &quot;notifications@example.com&quot;
382
-
383
- def welcome_email(user)
384
- @user = user
385
- @url = &quot;http://example.com/login&quot;
386
- mail(:to =&gt; user.email,
387
- :subject =&gt; &quot;Welcome to My Awesome Site&quot;) do |format|
388
- format.html { render 'another_template' }
389
- format.text { render :text =&gt; 'Render text' }
390
- end
391
- end
392
-
393
- end
394
- </pre>
395
- </div>
396
- <p>This will render the template &#8216;another_template.html.erb&#8217; for the <span class="caps">HTML</span> part and use the rendered text for the text part. The render command is the same one used inside of Action Controller, so you can use all the same options, such as <tt>:text</tt>, <tt>:inline</tt> etc.</p>
397
- <h4 id="action-mailer-layouts">2.5 Action Mailer Layouts</h4>
398
- <p>Just like controller views, you can also have mailer layouts. The layout name needs to be the same as your mailer, such as <tt>user_mailer.html.erb</tt> and <tt>user_mailer.text.erb</tt> to be automatically recognized by your mailer as a layout.</p>
399
- <p>In order to use a different file just use:</p>
400
- <div class="code_container">
401
- <pre class="brush: ruby; gutter: false; toolbar: false">
402
- class UserMailer &lt; ActionMailer::Base
403
- layout 'awesome' # use awesome.(html|text).erb as the layout
404
- end
405
- </pre>
406
- </div>
407
- <p>Just like with controller views, use <tt>yield</tt> to render the view inside the layout.</p>
408
- <p>You can also pass in a <tt>:layout =&gt; &#8216;layout_name&#8217;</tt> option to the render call inside the format block to specify different layouts for different actions:</p>
409
- <div class="code_container">
410
- <pre class="brush: ruby; gutter: false; toolbar: false">
411
- class UserMailer &lt; ActionMailer::Base
412
- def welcome_email(user)
413
- mail(:to =&gt; user.email) do |format|
414
- format.html { render :layout =&gt; 'my_layout' }
415
- format.text
416
- end
417
- end
418
- end
419
- </pre>
420
- </div>
421
- <p>Will render the <span class="caps">HTML</span> part using the <tt>my_layout.html.erb</tt> file and the text part with the usual <tt>user_mailer.text.erb</tt> file if it exists.</p>
422
- <h4 id="generating-urls-in-action-mailer-views">2.6 Generating URLs in Action Mailer Views</h4>
423
- <p>URLs can be generated in mailer views using <tt>url_for</tt> or named routes.</p>
424
- <p>Unlike controllers, the mailer instance doesn&#8217;t have any context about the incoming request so you&#8217;ll need to provide the <tt>:host</tt>, <tt>:controller</tt>, and <tt>:action</tt>:</p>
425
- <div class="code_container">
426
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
427
- &lt;%= url_for(:host =&gt; &quot;example.com&quot;,
428
- :controller =&gt; &quot;welcome&quot;,
429
- :action =&gt; &quot;greeting&quot;) %&gt;
430
- </pre>
431
- </div>
432
- <p>When using named routes you only need to supply the <tt>:host</tt>:</p>
433
- <div class="code_container">
434
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
435
- &lt;%= user_url(@user, :host =&gt; &quot;example.com&quot;) %&gt;
436
- </pre>
437
- </div>
438
- <p>Email clients have no web context and so paths have no base <span class="caps">URL</span> to form complete web addresses. Thus, when using named routes only the &#8220;_url&#8221; variant makes sense.</p>
439
- <p>It is also possible to set a default host that will be used in all mailers by setting the <tt>:host</tt> option in the <tt>ActionMailer::Base.default_url_options</tt> hash as follows:</p>
440
- <div class="code_container">
441
- <pre class="brush: ruby; gutter: false; toolbar: false">
442
- class UserMailer &lt; ActionMailer::Base
443
- default_url_options[:host] = &quot;example.com&quot;
444
-
445
- def welcome_email(user)
446
- @user = user
447
- @url = user_url(@user)
448
- mail(:to =&gt; user.email,
449
- :subject =&gt; &quot;Welcome to My Awesome Site&quot;)
450
- end
451
- end
452
- </pre>
453
- </div>
454
- <h4 id="sending-multipart-emails">2.7 Sending Multipart Emails</h4>
455
- <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 <tt>welcome_email.text.erb</tt> and <tt>welcome_email.html.erb</tt> in <tt>app/views/user_mailer</tt>, Action Mailer will automatically send a multipart email with the <span class="caps">HTML</span> and text versions setup as different parts.</p>
456
- <p>The order of the parts getting inserted is determined by the <tt>:parts_order</tt> inside of the <tt>ActionMailer::Base.default</tt> method. If you want to explicitly alter the order, you can either change the <tt>:parts_order</tt> or explicitly render the parts in a different order:</p>
457
- <div class="code_container">
458
- <pre class="brush: ruby; gutter: false; toolbar: false">
459
- class UserMailer &lt; ActionMailer::Base
460
- def welcome_email(user)
461
- @user = user
462
- @url = user_url(@user)
463
- mail(:to =&gt; user.email,
464
- :subject =&gt; &quot;Welcome to My Awesome Site&quot;) do |format|
465
- format.html
466
- format.text
467
- end
468
- end
469
- end
470
- </pre>
471
- </div>
472
- <p>Will put the <span class="caps">HTML</span> part first, and the plain text part second.</p>
473
- <h4 id="sending-emails-with-attachments">2.8 Sending Emails with Attachments</h4>
474
- <p>Attachments can be added by using the <tt>attachments</tt> method:</p>
475
- <div class="code_container">
476
- <pre class="brush: ruby; gutter: false; toolbar: false">
477
- class UserMailer &lt; ActionMailer::Base
478
- def welcome_email(user)
479
- @user = user
480
- @url = user_url(@user)
481
- attachments['terms.pdf'] = File.read('/path/terms.pdf')
482
- mail(:to =&gt; user.email,
483
- :subject =&gt; &quot;Please see the Terms and Conditions attached&quot;)
484
- end
485
- end
486
- </pre>
487
- </div>
488
- <p>The above will send a multipart email with an attachment, properly nested with the top level being <tt>multipart/mixed</tt> and the first part being a <tt>multipart/alternative</tt> containing the plain text and <span class="caps">HTML</span> email messages.</p>
489
- <h3 id="receiving-emails">3 Receiving Emails</h3>
490
- <p>Receiving and parsing emails with Action Mailer can be a rather complex endeavor. 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 to:</p>
491
- <ul>
492
- <li>Implement a <tt>receive</tt> method in your mailer.</li>
493
- </ul>
494
- <ul>
495
- <li>Configure your email server to forward emails from the address(es) you would like your app to receive to <tt>/path/to/app/script/rails runner 'UserMailer.receive(STDIN.read)'</tt>.</li>
496
- </ul>
497
- <p>Once a method called <tt>receive</tt> 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 <tt>receive</tt> instance method. Here&#8217;s an example:</p>
498
- <div class="code_container">
499
- <pre class="brush: ruby; gutter: false; toolbar: false">
500
- class UserMailer &lt; ActionMailer::Base
501
- def receive(email)
502
- page = Page.find_by_address(email.to.first)
503
- page.emails.create(
504
- :subject =&gt; email.subject,
505
- :body =&gt; email.body
506
- )
507
-
508
- if email.has_attachments?
509
- email.attachments.each do |attachment|
510
- page.attachments.create({
511
- :file =&gt; attachment,
512
- :description =&gt; email.subject
513
- })
514
- end
515
- end
516
- end
517
- end
518
- </pre>
519
- </div>
520
- <h3 id="using-action-mailer-helpers">4 Using Action Mailer Helpers</h3>
521
- <p>Action Mailer now just inherits from Abstract Controller, so you have access to the same generic helpers as you do in Action Controller.</p>
522
- <h3 id="action-mailer-configuration">5 Action Mailer Configuration</h3>
523
- <p>The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc&#8230;)</p>
524
- <table>
525
- <tr>
526
- <td><tt>template_root</tt></td>
527
- <td>Determines the base from which template references will be made.</td>
528
- </tr>
529
- <tr>
530
- <td><tt>logger</tt></td>
531
- <td>Generates information on the mailing run if available. Can be set to <tt>nil</tt> for no logging. Compatible with both Ruby&#8217;s own <tt>Logger</tt> and <tt>Log4r</tt> loggers.</td>
532
- </tr>
533
- <tr>
534
- <td><tt>smtp_settings</tt></td>
535
- <td>Allows detailed configuration for <tt>:smtp</tt> delivery method:<ul><li><tt>:address</tt> &#8211; Allows you to use a remote mail server. Just change it from its default &#8220;localhost&#8221; setting.</li><li><tt>:port</tt> &#8211; On the off chance that your mail server doesn&#8217;t run on port 25, you can change it.</li><li><tt>:domain</tt> &#8211; If you need to specify a <span class="caps">HELO</span> domain, you can do it here.</li><li><tt>:user_name</tt> &#8211; If your mail server requires authentication, set the username in this setting.</li><li><tt>:password</tt> &#8211; If your mail server requires authentication, set the password in this setting.</li><li><tt>:authentication</tt> &#8211; If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.</li></ul></td>
536
- </tr>
537
- <tr>
538
- <td><tt>sendmail_settings</tt></td>
539
- <td>Allows you to override options for the <tt>:sendmail</tt> delivery method.<ul><li><tt>:location</tt> &#8211; The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.</li><li><tt>:arguments</tt> &#8211; The command line arguments to be passed to sendmail. Defaults to <tt>-i -t</tt>.</li></ul></td>
540
- </tr>
541
- <tr>
542
- <td><tt>raise_delivery_errors</tt></td>
543
- <td>Whether or not errors should be raised if the email fails to be delivered.</td>
544
- </tr>
545
- <tr>
546
- <td><tt>delivery_method</tt></td>
547
- <td>Defines a delivery method. Possible values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, <tt>:file</tt> and <tt>:test</tt>.</td>
548
- </tr>
549
- <tr>
550
- <td><tt>perform_deliveries</tt></td>
551
- <td>Determines whether deliveries are actually carried out when the <tt>deliver</tt> method is invoked on the Mail message. By default they are, but this can be turned off to help functional testing.</td>
552
- </tr>
553
- <tr>
554
- <td><tt>deliveries</tt></td>
555
- <td>Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.</td>
556
- </tr>
557
- </table>
558
- <h4 id="example-action-mailer-configuration">5.1 Example Action Mailer Configuration</h4>
559
- <p>An example would be adding the following to your appropriate <tt>config/environments/$RAILS_ENV.rb</tt> file:</p>
560
- <div class="code_container">
561
- <pre class="brush: ruby; gutter: false; toolbar: false">
562
- config.action_mailer.delivery_method = :sendmail
563
- # Defaults to:
564
- # config.action_mailer.sendmail_settings = {
565
- # :location =&gt; '/usr/sbin/sendmail',
566
- # :arguments =&gt; '-i -t'
567
- # }
568
- config.action_mailer.perform_deliveries = true
569
- config.action_mailer.raise_delivery_errors = true
570
- </pre>
571
- </div>
572
- <h4 id="action-mailer-configuration-for-gmail">5.2 Action Mailer Configuration for GMail</h4>
573
- <p>As Action Mailer now uses the Mail gem, this becomes as simple as adding to your <tt>config/environments/$RAILS_ENV.rb</tt> file:</p>
574
- <div class="code_container">
575
- <pre class="brush: ruby; gutter: false; toolbar: false">
576
- config.action_mailer.delivery_method = :smtp
577
- config.action_mailer.smtp_settings = {
578
- :address =&gt; &quot;smtp.gmail.com&quot;,
579
- :port =&gt; 587,
580
- :domain =&gt; 'baci.lindsaar.net',
581
- :user_name =&gt; '&lt;username&gt;',
582
- :password =&gt; '&lt;password&gt;',
583
- :authentication =&gt; 'plain',
584
- :enable_starttls_auto =&gt; true }
585
- </pre>
586
- </div>
587
- <h3 id="mailer-testing">6 Mailer Testing</h3>
588
- <p>By default Action Mailer does not send emails in the test environment. They are just added to the <tt>ActionMailer::Base.deliveries</tt> array.</p>
589
- <p>Testing mailers normally involves two things: One is that the mail was queued, and the other one that the email is correct. With that in mind, we could test our example mailer from above like so:</p>
590
- <div class="code_container">
591
- <pre class="brush: ruby; gutter: false; toolbar: false">
592
- class UserMailerTest &lt; ActionMailer::TestCase
593
- def test_welcome_email
594
- user = users(:some_user_in_your_fixtures)
595
-
596
- # Send the email, then test that it got queued
597
- email = UserMailer.welcome_email(user).deliver
598
- assert !ActionMailer::Base.deliveries.empty?
599
-
600
- # Test the body of the sent email contains what we expect it to
601
- assert_equal [user.email], email.to
602
- assert_equal &quot;Welcome to My Awesome Site&quot;, email.subject
603
- assert_match(/&lt;h1&gt;Welcome to example.com, #{user.name}&lt;\/h1&gt;/, email.encoded)
604
- assert_match(/Welcome to example.com, #{user.name}/, email.encoded)
605
- end
606
- end
607
- </pre>
608
- </div>
609
- <p>In the test we send the email and store the returned object in the <tt>email</tt> variable. We then ensure that it was sent (the first assert), then, in the second batch of assertions, we ensure that the email does indeed contain what we expect.</p>
610
-
611
- <h3>Feedback</h3>
612
- <p>
613
- You're encouraged to help improve the quality of this guide.
614
- </p>
615
- <p>
616
- If you see any typos or factual errors you are confident to
617
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
618
- and push the change yourself. That branch of Rails has public write access.
619
- Commits are still reviewed, but that happens after you've submitted your
620
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
621
- cross-merged with master periodically.
622
- </p>
623
- <p>
624
- You may also find incomplete content, or stuff that is not up to date.
625
- Please do add any missing documentation for master. Check the
626
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
627
- for style and conventions.
628
- </p>
629
- <p>
630
- If for whatever reason you spot something to fix but cannot patch it yourself, please
631
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
632
- </p>
633
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
634
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
635
- </p>
636
- </div>
637
- </div>
638
- </div>
639
-
640
- <hr class="hide" />
641
- <div id="footer">
642
- <div class="wrapper">
643
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
644
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
645
- </div>
646
- </div>
647
-
648
- <script type="text/javascript" src="javascripts/guides.js"></script>
649
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
650
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
651
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
652
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
653
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
654
- <script type="text/javascript">
655
- SyntaxHighlighter.all()
656
- </script>
657
- </body>
658
- </html>