rails_test 4.2.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +83 -0
  3. data/guides/CHANGELOG.md +27 -0
  4. data/guides/Rakefile +77 -0
  5. data/guides/assets/images/akshaysurve.jpg +0 -0
  6. data/guides/assets/images/belongs_to.png +0 -0
  7. data/guides/assets/images/book_icon.gif +0 -0
  8. data/guides/assets/images/bullet.gif +0 -0
  9. data/guides/assets/images/chapters_icon.gif +0 -0
  10. data/guides/assets/images/check_bullet.gif +0 -0
  11. data/guides/assets/images/credits_pic_blank.gif +0 -0
  12. data/guides/assets/images/csrf.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/favicon.ico +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/getting_started/article_with_comments.png +0 -0
  19. data/guides/assets/images/getting_started/challenge.png +0 -0
  20. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  21. data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  22. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  23. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  24. data/guides/assets/images/getting_started/new_article.png +0 -0
  25. data/guides/assets/images/getting_started/rails_welcome.png +0 -0
  26. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  27. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  28. data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
  29. data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
  30. data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
  31. data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
  32. data/guides/assets/images/grey_bullet.gif +0 -0
  33. data/guides/assets/images/habtm.png +0 -0
  34. data/guides/assets/images/has_many.png +0 -0
  35. data/guides/assets/images/has_many_through.png +0 -0
  36. data/guides/assets/images/has_one.png +0 -0
  37. data/guides/assets/images/has_one_through.png +0 -0
  38. data/guides/assets/images/header_backdrop.png +0 -0
  39. data/guides/assets/images/header_tile.gif +0 -0
  40. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  41. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  42. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  43. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  44. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  45. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  46. data/guides/assets/images/icons/README +5 -0
  47. data/guides/assets/images/icons/callouts/1.png +0 -0
  48. data/guides/assets/images/icons/callouts/10.png +0 -0
  49. data/guides/assets/images/icons/callouts/11.png +0 -0
  50. data/guides/assets/images/icons/callouts/12.png +0 -0
  51. data/guides/assets/images/icons/callouts/13.png +0 -0
  52. data/guides/assets/images/icons/callouts/14.png +0 -0
  53. data/guides/assets/images/icons/callouts/15.png +0 -0
  54. data/guides/assets/images/icons/callouts/2.png +0 -0
  55. data/guides/assets/images/icons/callouts/3.png +0 -0
  56. data/guides/assets/images/icons/callouts/4.png +0 -0
  57. data/guides/assets/images/icons/callouts/5.png +0 -0
  58. data/guides/assets/images/icons/callouts/6.png +0 -0
  59. data/guides/assets/images/icons/callouts/7.png +0 -0
  60. data/guides/assets/images/icons/callouts/8.png +0 -0
  61. data/guides/assets/images/icons/callouts/9.png +0 -0
  62. data/guides/assets/images/icons/caution.png +0 -0
  63. data/guides/assets/images/icons/example.png +0 -0
  64. data/guides/assets/images/icons/home.png +0 -0
  65. data/guides/assets/images/icons/important.png +0 -0
  66. data/guides/assets/images/icons/next.png +0 -0
  67. data/guides/assets/images/icons/note.png +0 -0
  68. data/guides/assets/images/icons/prev.png +0 -0
  69. data/guides/assets/images/icons/tip.png +0 -0
  70. data/guides/assets/images/icons/up.png +0 -0
  71. data/guides/assets/images/icons/warning.png +0 -0
  72. data/guides/assets/images/nav_arrow.gif +0 -0
  73. data/guides/assets/images/oscardelben.jpg +0 -0
  74. data/guides/assets/images/polymorphic.png +0 -0
  75. data/guides/assets/images/radar.png +0 -0
  76. data/guides/assets/images/rails4_features.png +0 -0
  77. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  78. data/guides/assets/images/rails_guides_logo.gif +0 -0
  79. data/guides/assets/images/rails_logo_remix.gif +0 -0
  80. data/guides/assets/images/session_fixation.png +0 -0
  81. data/guides/assets/images/tab_grey.gif +0 -0
  82. data/guides/assets/images/tab_info.gif +0 -0
  83. data/guides/assets/images/tab_note.gif +0 -0
  84. data/guides/assets/images/tab_red.gif +0 -0
  85. data/guides/assets/images/tab_yellow.gif +0 -0
  86. data/guides/assets/images/tab_yellow.png +0 -0
  87. data/guides/assets/images/vijaydev.jpg +0 -0
  88. data/guides/assets/javascripts/guides.js +59 -0
  89. data/guides/assets/javascripts/jquery.min.js +4 -0
  90. data/guides/assets/javascripts/responsive-tables.js +43 -0
  91. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  116. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  117. data/guides/assets/stylesheets/fixes.css +16 -0
  118. data/guides/assets/stylesheets/kindle.css +11 -0
  119. data/guides/assets/stylesheets/main.css +713 -0
  120. data/guides/assets/stylesheets/print.css +52 -0
  121. data/guides/assets/stylesheets/reset.css +43 -0
  122. data/guides/assets/stylesheets/responsive-tables.css +50 -0
  123. data/guides/assets/stylesheets/style.css +13 -0
  124. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  133. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  142. data/guides/bug_report_templates/action_controller_gem.rb +47 -0
  143. data/guides/bug_report_templates/action_controller_master.rb +56 -0
  144. data/guides/bug_report_templates/active_record_gem.rb +40 -0
  145. data/guides/bug_report_templates/active_record_master.rb +51 -0
  146. data/guides/rails_guides.rb +63 -0
  147. data/guides/rails_guides/generator.rb +248 -0
  148. data/guides/rails_guides/helpers.rb +53 -0
  149. data/guides/rails_guides/indexer.rb +68 -0
  150. data/guides/rails_guides/kindle.rb +119 -0
  151. data/guides/rails_guides/levenshtein.rb +39 -0
  152. data/guides/rails_guides/markdown.rb +167 -0
  153. data/guides/rails_guides/markdown/renderer.rb +82 -0
  154. data/guides/source/2_2_release_notes.md +435 -0
  155. data/guides/source/2_3_release_notes.md +621 -0
  156. data/guides/source/3_0_release_notes.md +611 -0
  157. data/guides/source/3_1_release_notes.md +559 -0
  158. data/guides/source/3_2_release_notes.md +568 -0
  159. data/guides/source/4_0_release_notes.md +279 -0
  160. data/guides/source/4_1_release_notes.md +730 -0
  161. data/guides/source/4_2_release_notes.md +736 -0
  162. data/guides/source/_license.html.erb +2 -0
  163. data/guides/source/_welcome.html.erb +19 -0
  164. data/guides/source/action_controller_overview.md +1247 -0
  165. data/guides/source/action_mailer_basics.md +738 -0
  166. data/guides/source/action_view_overview.md +1621 -0
  167. data/guides/source/active_job_basics.md +285 -0
  168. data/guides/source/active_model_basics.md +223 -0
  169. data/guides/source/active_record_basics.md +374 -0
  170. data/guides/source/active_record_callbacks.md +413 -0
  171. data/guides/source/active_record_migrations.md +1016 -0
  172. data/guides/source/active_record_postgresql.md +438 -0
  173. data/guides/source/active_record_querying.md +1785 -0
  174. data/guides/source/active_record_validations.md +1169 -0
  175. data/guides/source/active_support_core_extensions.md +3924 -0
  176. data/guides/source/active_support_instrumentation.md +497 -0
  177. data/guides/source/api_documentation_guidelines.md +361 -0
  178. data/guides/source/asset_pipeline.md +1361 -0
  179. data/guides/source/association_basics.md +2238 -0
  180. data/guides/source/caching_with_rails.md +379 -0
  181. data/guides/source/command_line.md +634 -0
  182. data/guides/source/configuring.md +1019 -0
  183. data/guides/source/contributing_to_ruby_on_rails.md +607 -0
  184. data/guides/source/credits.html.erb +80 -0
  185. data/guides/source/debugging_rails_applications.md +861 -0
  186. data/guides/source/development_dependencies_install.md +289 -0
  187. data/guides/source/documents.yaml +197 -0
  188. data/guides/source/engines.md +1401 -0
  189. data/guides/source/form_helpers.md +1010 -0
  190. data/guides/source/generators.md +676 -0
  191. data/guides/source/getting_started.md +2085 -0
  192. data/guides/source/i18n.md +1073 -0
  193. data/guides/source/index.html.erb +28 -0
  194. data/guides/source/initialization.md +704 -0
  195. data/guides/source/kindle/copyright.html.erb +1 -0
  196. data/guides/source/kindle/layout.html.erb +27 -0
  197. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  198. data/guides/source/kindle/toc.html.erb +24 -0
  199. data/guides/source/kindle/toc.ncx.erb +64 -0
  200. data/guides/source/kindle/welcome.html.erb +5 -0
  201. data/guides/source/layout.html.erb +143 -0
  202. data/guides/source/layouts_and_rendering.md +1227 -0
  203. data/guides/source/maintenance_policy.md +75 -0
  204. data/guides/source/nested_model_forms.md +228 -0
  205. data/guides/source/plugins.md +444 -0
  206. data/guides/source/rails_application_templates.md +266 -0
  207. data/guides/source/rails_on_rack.md +332 -0
  208. data/guides/source/routing.md +1141 -0
  209. data/guides/source/ruby_on_rails_guides_guidelines.md +126 -0
  210. data/guides/source/security.md +1024 -0
  211. data/guides/source/testing.md +1055 -0
  212. data/guides/source/upgrading_ruby_on_rails.md +1046 -0
  213. data/guides/source/working_with_javascript_in_rails.md +407 -0
  214. data/guides/w3c_validator.rb +97 -0
  215. metadata +404 -0
@@ -0,0 +1,738 @@
1
+ Action Mailer Basics
2
+ ====================
3
+
4
+ This guide provides you with all you need to get started in sending and
5
+ receiving emails from and to your application, and many internals of Action
6
+ Mailer. It also covers how to test your mailers.
7
+
8
+ After reading this guide, you will know:
9
+
10
+ * How to send and receive email within a Rails application.
11
+ * How to generate and edit an Action Mailer class and mailer view.
12
+ * How to configure Action Mailer for your environment.
13
+ * How to test your Action Mailer classes.
14
+
15
+ --------------------------------------------------------------------------------
16
+
17
+ Introduction
18
+ ------------
19
+
20
+ Action Mailer allows you to send emails from your application using mailer classes
21
+ and views. Mailers work very similarly to controllers. They inherit from
22
+ `ActionMailer::Base` and live in `app/mailers`, and they have associated views
23
+ that appear in `app/views`.
24
+
25
+ Sending Emails
26
+ --------------
27
+
28
+ This section will provide a step-by-step guide to creating a mailer and its
29
+ views.
30
+
31
+ ### Walkthrough to Generating a Mailer
32
+
33
+ #### Create the Mailer
34
+
35
+ ```bash
36
+ $ bin/rails generate mailer UserMailer
37
+ create app/mailers/user_mailer.rb
38
+ invoke erb
39
+ create app/views/user_mailer
40
+ invoke test_unit
41
+ create test/mailers/user_mailer_test.rb
42
+ ```
43
+
44
+ As you can see, you can generate mailers just like you use other generators with
45
+ Rails. Mailers are conceptually similar to controllers, and so we get a mailer,
46
+ a directory for views, and a test.
47
+
48
+ If you didn't want to use a generator, you could create your own file inside of
49
+ app/mailers, just make sure that it inherits from `ActionMailer::Base`:
50
+
51
+ ```ruby
52
+ class MyMailer < ActionMailer::Base
53
+ end
54
+ ```
55
+
56
+ #### Edit the Mailer
57
+
58
+ Mailers are very similar to Rails controllers. They also have methods called
59
+ "actions" and use views to structure the content. Where a controller generates
60
+ content like HTML to send back to the client, a Mailer creates a message to be
61
+ delivered via email.
62
+
63
+ `app/mailers/user_mailer.rb` contains an empty mailer:
64
+
65
+ ```ruby
66
+ class UserMailer < ActionMailer::Base
67
+ default from: 'from@example.com'
68
+ end
69
+ ```
70
+
71
+ Let's add a method called `welcome_email`, that will send an email to the user's
72
+ registered email address:
73
+
74
+ ```ruby
75
+ class UserMailer < ActionMailer::Base
76
+ default from: 'notifications@example.com'
77
+
78
+ def welcome_email(user)
79
+ @user = user
80
+ @url = 'http://example.com/login'
81
+ mail(to: @user.email, subject: 'Welcome to My Awesome Site')
82
+ end
83
+ end
84
+ ```
85
+
86
+ Here is a quick explanation of the items presented in the preceding method. For
87
+ a full list of all available options, please have a look further down at the
88
+ Complete List of Action Mailer user-settable attributes section.
89
+
90
+ * `default Hash` - This is a hash of default values for any email you send from
91
+ this mailer. In this case we are setting the `:from` header to a value for all
92
+ messages in this class. This can be overridden on a per-email basis.
93
+ * `mail` - The actual email message, we are passing the `:to` and `:subject`
94
+ headers in.
95
+
96
+ Just like controllers, any instance variables we define in the method become
97
+ available for use in the views.
98
+
99
+ #### Create a Mailer View
100
+
101
+ Create a file called `welcome_email.html.erb` in `app/views/user_mailer/`. This
102
+ will be the template used for the email, formatted in HTML:
103
+
104
+ ```html+erb
105
+ <!DOCTYPE html>
106
+ <html>
107
+ <head>
108
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
109
+ </head>
110
+ <body>
111
+ <h1>Welcome to example.com, <%= @user.name %></h1>
112
+ <p>
113
+ You have successfully signed up to example.com,
114
+ your username is: <%= @user.login %>.<br>
115
+ </p>
116
+ <p>
117
+ To login to the site, just follow this link: <%= @url %>.
118
+ </p>
119
+ <p>Thanks for joining and have a great day!</p>
120
+ </body>
121
+ </html>
122
+ ```
123
+
124
+ Let's also make a text part for this email. Not all clients prefer HTML emails,
125
+ and so sending both is best practice. To do this, create a file called
126
+ `welcome_email.text.erb` in `app/views/user_mailer/`:
127
+
128
+ ```erb
129
+ Welcome to example.com, <%= @user.name %>
130
+ ===============================================
131
+
132
+ You have successfully signed up to example.com,
133
+ your username is: <%= @user.login %>.
134
+
135
+ To login to the site, just follow this link: <%= @url %>.
136
+
137
+ Thanks for joining and have a great day!
138
+ ```
139
+
140
+ When you call the `mail` method now, Action Mailer will detect the two templates
141
+ (text and HTML) and automatically generate a `multipart/alternative` email.
142
+
143
+ #### Calling the Mailer
144
+
145
+ Mailers are really just another way to render a view. Instead of rendering a
146
+ view and sending out the HTTP protocol, they are just sending it out through the
147
+ email protocols instead. Due to this, it makes sense to just have your
148
+ controller tell the Mailer to send an email when a user is successfully created.
149
+
150
+ Setting this up is painfully simple.
151
+
152
+ First, let's create a simple `User` scaffold:
153
+
154
+ ```bash
155
+ $ bin/rails generate scaffold user name email login
156
+ $ bin/rake db:migrate
157
+ ```
158
+
159
+ Now that we have a user model to play with, we will just edit the
160
+ `app/controllers/users_controller.rb` make it instruct the `UserMailer` to deliver
161
+ an email to the newly created user by editing the create action and inserting a
162
+ call to `UserMailer.welcome_email` right after the user is successfully saved.
163
+
164
+ Action Mailer is nicely integrated with Active Job so you can send emails outside
165
+ of the request-response cycle, so the user doesn't have to wait on it:
166
+
167
+ ```ruby
168
+ class UsersController < ApplicationController
169
+ # POST /users
170
+ # POST /users.json
171
+ def create
172
+ @user = User.new(params[:user])
173
+
174
+ respond_to do |format|
175
+ if @user.save
176
+ # Tell the UserMailer to send a welcome email after save
177
+ UserMailer.welcome_email(@user).deliver_later
178
+
179
+ format.html { redirect_to(@user, notice: 'User was successfully created.') }
180
+ format.json { render json: @user, status: :created, location: @user }
181
+ else
182
+ format.html { render action: 'new' }
183
+ format.json { render json: @user.errors, status: :unprocessable_entity }
184
+ end
185
+ end
186
+ end
187
+ end
188
+ ```
189
+
190
+ NOTE: Active Job's default behavior is to execute jobs ':inline'. So, you can use
191
+ `deliver_later` now to send emails, and when you later decide to start sending
192
+ them from a background job, you'll only need to set up Active Job to use a queueing
193
+ backend (Sidekiq, Resque, etc).
194
+
195
+ If you want to send emails right away (from a cronjob for example) just call
196
+ `deliver_now`:
197
+
198
+ ```ruby
199
+ class SendWeeklySummary
200
+ def run
201
+ User.find_each do |user|
202
+ UserMailer.weekly_summary(user).deliver_now
203
+ end
204
+ end
205
+ end
206
+ ```
207
+
208
+ The method `welcome_email` returns a `ActionMailer::MessageDelivery` object which
209
+ can then just be told `deliver_now` or `deliver_later` to send itself out. The
210
+ `ActionMailer::MessageDelivery` object is just a wrapper around a `Mail::Message`. If
211
+ you want to inspect, alter or do anything else with the `Mail::Message` object you can
212
+ access it with the `message` method on the `ActionMailer::MessageDelivery` object.
213
+
214
+ ### Auto encoding header values
215
+
216
+ Action Mailer handles the auto encoding of multibyte characters inside of
217
+ headers and bodies.
218
+
219
+ For more complex examples such as defining alternate character sets or
220
+ self-encoding text first, please refer to the
221
+ [Mail](https://github.com/mikel/mail) library.
222
+
223
+ ### Complete List of Action Mailer Methods
224
+
225
+ There are just three methods that you need to send pretty much any email
226
+ message:
227
+
228
+ * `headers` - Specifies any header on the email you want. You can pass a hash of
229
+ header field names and value pairs, or you can call `headers[:field_name] =
230
+ 'value'`.
231
+ * `attachments` - Allows you to add attachments to your email. For example,
232
+ `attachments['file-name.jpg'] = File.read('file-name.jpg')`.
233
+ * `mail` - Sends the actual email itself. You can pass in headers as a hash to
234
+ the mail method as a parameter, mail will then create an email, either plain
235
+ text, or multipart, depending on what email templates you have defined.
236
+
237
+ #### Adding Attachments
238
+
239
+ Action Mailer makes it very easy to add attachments.
240
+
241
+ * Pass the file name and content and Action Mailer and the
242
+ [Mail gem](https://github.com/mikel/mail) will automatically guess the
243
+ mime_type, set the encoding and create the attachment.
244
+
245
+ ```ruby
246
+ attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
247
+ ```
248
+
249
+ When the `mail` method will be triggered, it will send a multipart email with
250
+ an attachment, properly nested with the top level being `multipart/mixed` and
251
+ the first part being a `multipart/alternative` containing the plain text and
252
+ HTML email messages.
253
+
254
+ NOTE: Mail will automatically Base64 encode an attachment. If you want something
255
+ different, encode your content and pass in the encoded content and encoding in a
256
+ `Hash` to the `attachments` method.
257
+
258
+ * Pass the file name and specify headers and content and Action Mailer and Mail
259
+ will use the settings you pass in.
260
+
261
+ ```ruby
262
+ encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
263
+ attachments['filename.jpg'] = {
264
+ mime_type: 'application/x-gzip',
265
+ encoding: 'SpecialEncoding',
266
+ content: encoded_content
267
+ }
268
+ ```
269
+
270
+ NOTE: If you specify an encoding, Mail will assume that your content is already
271
+ encoded and not try to Base64 encode it.
272
+
273
+ #### Making Inline Attachments
274
+
275
+ 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.
276
+
277
+ * First, to tell Mail to turn an attachment into an inline attachment, you just call `#inline` on the attachments method within your Mailer:
278
+
279
+ ```ruby
280
+ def welcome
281
+ attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
282
+ end
283
+ ```
284
+
285
+ * Then in your view, you can just reference `attachments` as a hash and specify
286
+ which attachment you want to show, calling `url` on it and then passing the
287
+ result into the `image_tag` method:
288
+
289
+ ```html+erb
290
+ <p>Hello there, this is our image</p>
291
+
292
+ <%= image_tag attachments['image.jpg'].url %>
293
+ ```
294
+
295
+ * As this is a standard call to `image_tag` you can pass in an options hash
296
+ after the attachment URL as you could for any other image:
297
+
298
+ ```html+erb
299
+ <p>Hello there, this is our image</p>
300
+
301
+ <%= image_tag attachments['image.jpg'].url, alt: 'My Photo',
302
+ class: 'photos' %>
303
+ ```
304
+
305
+ #### Sending Email To Multiple Recipients
306
+
307
+ It is possible to send email to one or more recipients in one email (e.g.,
308
+ informing all admins of a new signup) by setting the list of emails to the `:to`
309
+ key. The list of emails can be an array of email addresses or a single string
310
+ with the addresses separated by commas.
311
+
312
+ ```ruby
313
+ class AdminMailer < ActionMailer::Base
314
+ default to: Proc.new { Admin.pluck(:email) },
315
+ from: 'notification@example.com'
316
+
317
+ def new_registration(user)
318
+ @user = user
319
+ mail(subject: "New User Signup: #{@user.email}")
320
+ end
321
+ end
322
+ ```
323
+
324
+ The same format can be used to set carbon copy (Cc:) and blind carbon copy
325
+ (Bcc:) recipients, by using the `:cc` and `:bcc` keys respectively.
326
+
327
+ #### Sending Email With Name
328
+
329
+ Sometimes you wish to show the name of the person instead of just their email
330
+ address when they receive the email. The trick to doing that is to format the
331
+ email address in the format `"Full Name <email>"`.
332
+
333
+ ```ruby
334
+ def welcome_email(user)
335
+ @user = user
336
+ email_with_name = %("#{@user.name}" <#{@user.email}>)
337
+ mail(to: email_with_name, subject: 'Welcome to My Awesome Site')
338
+ end
339
+ ```
340
+
341
+ ### Mailer Views
342
+
343
+ Mailer views are located in the `app/views/name_of_mailer_class` directory. The
344
+ specific mailer view is known to the class because its name is the same as the
345
+ mailer method. In our example from above, our mailer view for the
346
+ `welcome_email` method will be in `app/views/user_mailer/welcome_email.html.erb`
347
+ for the HTML version and `welcome_email.text.erb` for the plain text version.
348
+
349
+ To change the default mailer view for your action you do something like:
350
+
351
+ ```ruby
352
+ class UserMailer < ActionMailer::Base
353
+ default from: 'notifications@example.com'
354
+
355
+ def welcome_email(user)
356
+ @user = user
357
+ @url = 'http://example.com/login'
358
+ mail(to: @user.email,
359
+ subject: 'Welcome to My Awesome Site',
360
+ template_path: 'notifications',
361
+ template_name: 'another')
362
+ end
363
+ end
364
+ ```
365
+
366
+ In this case it will look for templates at `app/views/notifications` with name
367
+ `another`. You can also specify an array of paths for `template_path`, and they
368
+ will be searched in order.
369
+
370
+ If you want more flexibility you can also pass a block and render specific
371
+ templates or even render inline or text without using a template file:
372
+
373
+ ```ruby
374
+ class UserMailer < ActionMailer::Base
375
+ default from: 'notifications@example.com'
376
+
377
+ def welcome_email(user)
378
+ @user = user
379
+ @url = 'http://example.com/login'
380
+ mail(to: @user.email,
381
+ subject: 'Welcome to My Awesome Site') do |format|
382
+ format.html { render 'another_template' }
383
+ format.text { render text: 'Render text' }
384
+ end
385
+ end
386
+ end
387
+ ```
388
+
389
+ This will render the template 'another_template.html.erb' for the HTML part and
390
+ use the rendered text for the text part. The render command is the same one used
391
+ inside of Action Controller, so you can use all the same options, such as
392
+ `:text`, `:inline` etc.
393
+
394
+ ### Action Mailer Layouts
395
+
396
+ Just like controller views, you can also have mailer layouts. The layout name
397
+ needs to be the same as your mailer, such as `user_mailer.html.erb` and
398
+ `user_mailer.text.erb` to be automatically recognized by your mailer as a
399
+ layout.
400
+
401
+ In order to use a different file, call `layout` in your mailer:
402
+
403
+ ```ruby
404
+ class UserMailer < ActionMailer::Base
405
+ layout 'awesome' # use awesome.(html|text).erb as the layout
406
+ end
407
+ ```
408
+
409
+ Just like with controller views, use `yield` to render the view inside the
410
+ layout.
411
+
412
+ You can also pass in a `layout: 'layout_name'` option to the render call inside
413
+ the format block to specify different layouts for different formats:
414
+
415
+ ```ruby
416
+ class UserMailer < ActionMailer::Base
417
+ def welcome_email(user)
418
+ mail(to: user.email) do |format|
419
+ format.html { render layout: 'my_layout' }
420
+ format.text
421
+ end
422
+ end
423
+ end
424
+ ```
425
+
426
+ Will render the HTML part using the `my_layout.html.erb` file and the text part
427
+ with the usual `user_mailer.text.erb` file if it exists.
428
+
429
+ ### Generating URLs in Action Mailer Views
430
+
431
+ Unlike controllers, the mailer instance doesn't have any context about the
432
+ incoming request so you'll need to provide the `:host` parameter yourself.
433
+
434
+ As the `:host` usually is consistent across the application you can configure it
435
+ globally in `config/application.rb`:
436
+
437
+ ```ruby
438
+ config.action_mailer.default_url_options = { host: 'example.com' }
439
+ ```
440
+
441
+ Because of this behavior you cannot use any of the `*_path` helpers inside of
442
+ an email. Instead you will need to use the associated `*_url` helper. For example
443
+ instead of using
444
+
445
+ ```
446
+ <%= link_to 'welcome', welcome_path %>
447
+ ```
448
+
449
+ You will need to use:
450
+
451
+ ```
452
+ <%= link_to 'welcome', welcome_url %>
453
+ ```
454
+
455
+ By using the full URL, your links will now work in your emails.
456
+
457
+ #### generating URLs with `url_for`
458
+
459
+ You need to pass the `only_path: false` option when using `url_for`. This will
460
+ ensure that absolute URLs are generated because the `url_for` view helper will,
461
+ by default, generate relative URLs when a `:host` option isn't explicitly
462
+ provided.
463
+
464
+ ```erb
465
+ <%= url_for(controller: 'welcome',
466
+ action: 'greeting',
467
+ only_path: false) %>
468
+ ```
469
+
470
+ If you did not configure the `:host` option globally make sure to pass it to
471
+ `url_for`.
472
+
473
+
474
+ ```erb
475
+ <%= url_for(host: 'example.com',
476
+ controller: 'welcome',
477
+ action: 'greeting') %>
478
+ ```
479
+
480
+ NOTE: When you explicitly pass the `:host` Rails will always generate absolute
481
+ URLs, so there is no need to pass `only_path: false`.
482
+
483
+ #### generating URLs with named routes
484
+
485
+ Email clients have no web context and so paths have no base URL to form complete
486
+ web addresses. Thus, you should always use the "_url" variant of named route
487
+ helpers.
488
+
489
+ If you did not configure the `:host` option globally make sure to pass it to the
490
+ url helper.
491
+
492
+ ```erb
493
+ <%= user_url(@user, host: 'example.com') %>
494
+ ```
495
+
496
+ ### Sending Multipart Emails
497
+
498
+ Action Mailer will automatically send multipart emails if you have different
499
+ templates for the same action. So, for our UserMailer example, if you have
500
+ `welcome_email.text.erb` and `welcome_email.html.erb` in
501
+ `app/views/user_mailer`, Action Mailer will automatically send a multipart email
502
+ with the HTML and text versions setup as different parts.
503
+
504
+ The order of the parts getting inserted is determined by the `:parts_order`
505
+ inside of the `ActionMailer::Base.default` method.
506
+
507
+ ### Sending Emails with Dynamic Delivery Options
508
+
509
+ If you wish to override the default delivery options (e.g. SMTP credentials)
510
+ while delivering emails, you can do this using `delivery_method_options` in the
511
+ mailer action.
512
+
513
+ ```ruby
514
+ class UserMailer < ActionMailer::Base
515
+ def welcome_email(user, company)
516
+ @user = user
517
+ @url = user_url(@user)
518
+ delivery_options = { user_name: company.smtp_user,
519
+ password: company.smtp_password,
520
+ address: company.smtp_host }
521
+ mail(to: @user.email,
522
+ subject: "Please see the Terms and Conditions attached",
523
+ delivery_method_options: delivery_options)
524
+ end
525
+ end
526
+ ```
527
+
528
+ ### Sending Emails without Template Rendering
529
+
530
+ There may be cases in which you want to skip the template rendering step and
531
+ supply the email body as a string. You can achieve this using the `:body`
532
+ option. In such cases don't forget to add the `:content_type` option. Rails
533
+ will default to `text/plain` otherwise.
534
+
535
+ ```ruby
536
+ class UserMailer < ActionMailer::Base
537
+ def welcome_email(user, email_body)
538
+ mail(to: user.email,
539
+ body: email_body,
540
+ content_type: "text/html",
541
+ subject: "Already rendered!")
542
+ end
543
+ end
544
+ ```
545
+
546
+ Receiving Emails
547
+ ----------------
548
+
549
+ Receiving and parsing emails with Action Mailer can be a rather complex
550
+ endeavor. Before your email reaches your Rails app, you would have had to
551
+ configure your system to somehow forward emails to your app, which needs to be
552
+ listening for that. So, to receive emails in your Rails app you'll need to:
553
+
554
+ * Implement a `receive` method in your mailer.
555
+
556
+ * Configure your email server to forward emails from the address(es) you would
557
+ like your app to receive to `/path/to/app/bin/rails runner
558
+ 'UserMailer.receive(STDIN.read)'`.
559
+
560
+ Once a method called `receive` is defined in any mailer, Action Mailer will
561
+ parse the raw incoming email into an email object, decode it, instantiate a new
562
+ mailer, and pass the email object to the mailer `receive` instance
563
+ method. Here's an example:
564
+
565
+ ```ruby
566
+ class UserMailer < ActionMailer::Base
567
+ def receive(email)
568
+ page = Page.find_by(address: email.to.first)
569
+ page.emails.create(
570
+ subject: email.subject,
571
+ body: email.body
572
+ )
573
+
574
+ if email.has_attachments?
575
+ email.attachments.each do |attachment|
576
+ page.attachments.create({
577
+ file: attachment,
578
+ description: email.subject
579
+ })
580
+ end
581
+ end
582
+ end
583
+ end
584
+ ```
585
+
586
+ Action Mailer Callbacks
587
+ ---------------------------
588
+
589
+ Action Mailer allows for you to specify a `before_action`, `after_action` and
590
+ `around_action`.
591
+
592
+ * Filters can be specified with a block or a symbol to a method in the mailer
593
+ class similar to controllers.
594
+
595
+ * You could use a `before_action` to populate the mail object with defaults,
596
+ delivery_method_options or insert default headers and attachments.
597
+
598
+ * You could use an `after_action` to do similar setup as a `before_action` but
599
+ using instance variables set in your mailer action.
600
+
601
+ ```ruby
602
+ class UserMailer < ActionMailer::Base
603
+ after_action :set_delivery_options,
604
+ :prevent_delivery_to_guests,
605
+ :set_business_headers
606
+
607
+ def feedback_message(business, user)
608
+ @business = business
609
+ @user = user
610
+ mail
611
+ end
612
+
613
+ def campaign_message(business, user)
614
+ @business = business
615
+ @user = user
616
+ end
617
+
618
+ private
619
+
620
+ def set_delivery_options
621
+ # You have access to the mail instance,
622
+ # @business and @user instance variables here
623
+ if @business && @business.has_smtp_settings?
624
+ mail.delivery_method.settings.merge!(@business.smtp_settings)
625
+ end
626
+ end
627
+
628
+ def prevent_delivery_to_guests
629
+ if @user && @user.guest?
630
+ mail.perform_deliveries = false
631
+ end
632
+ end
633
+
634
+ def set_business_headers
635
+ if @business
636
+ headers["X-SMTPAPI-CATEGORY"] = @business.code
637
+ end
638
+ end
639
+ end
640
+ ```
641
+
642
+ * Mailer Filters abort further processing if body is set to a non-nil value.
643
+
644
+ Using Action Mailer Helpers
645
+ ---------------------------
646
+
647
+ Action Mailer now just inherits from `AbstractController`, so you have access to
648
+ the same generic helpers as you do in Action Controller.
649
+
650
+ Action Mailer Configuration
651
+ ---------------------------
652
+
653
+ The following configuration options are best made in one of the environment
654
+ files (environment.rb, production.rb, etc...)
655
+
656
+ | Configuration | Description |
657
+ |---------------|-------------|
658
+ |`logger`|Generates information on the mailing run if available. Can be set to `nil` for no logging. Compatible with both Ruby's own `Logger` and `Log4r` loggers.|
659
+ |`smtp_settings`|Allows detailed configuration for `:smtp` delivery method:<ul><li>`:address` - Allows you to use a remote mail server. Just change it from its default `"localhost"` setting.</li><li>`:port` - On the off chance that your mail server doesn't run on port 25, you can change it.</li><li>`:domain` - If you need to specify a HELO domain, you can do it here.</li><li>`:user_name` - If your mail server requires authentication, set the username in this setting.</li><li>`:password` - If your mail server requires authentication, set the password in this setting.</li><li>`:authentication` - 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`.</li><li>`:enable_starttls_auto` - Set this to `false` if there is a problem with your server certificate that you cannot resolve.</li></ul>|
660
+ |`sendmail_settings`|Allows you to override options for the `:sendmail` delivery method.<ul><li>`:location` - The location of the sendmail executable. Defaults to `/usr/sbin/sendmail`.</li><li>`:arguments` - The command line arguments to be passed to sendmail. Defaults to `-i -t`.</li></ul>|
661
+ |`raise_delivery_errors`|Whether or not errors should be raised if the email fails to be delivered. This only works if the external email server is configured for immediate delivery.|
662
+ |`delivery_method`|Defines a delivery method. Possible values are:<ul><li>`:smtp` (default), can be configured by using `config.action_mailer.smtp_settings`.</li><li>`:sendmail`, can be configured by using `config.action_mailer.sendmail_settings`.</li><li>`:file`: save emails to files; can be configured by using `config.action_mailer.file_settings`.</li><li>`:test`: save emails to `ActionMailer::Base.deliveries` array.</li></ul>See [API docs](http://api.rubyonrails.org/classes/ActionMailer/Base.html) for more info.|
663
+ |`perform_deliveries`|Determines whether deliveries are actually carried out when the `deliver` method is invoked on the Mail message. By default they are, but this can be turned off to help functional testing.|
664
+ |`deliveries`|Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.|
665
+ |`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).|
666
+
667
+ For a complete writeup of possible configurations see the
668
+ [Configuring Action Mailer](configuring.html#configuring-action-mailer) in
669
+ our Configuring Rails Applications guide.
670
+
671
+ ### Example Action Mailer Configuration
672
+
673
+ An example would be adding the following to your appropriate
674
+ `config/environments/$RAILS_ENV.rb` file:
675
+
676
+ ```ruby
677
+ config.action_mailer.delivery_method = :sendmail
678
+ # Defaults to:
679
+ # config.action_mailer.sendmail_settings = {
680
+ # location: '/usr/sbin/sendmail',
681
+ # arguments: '-i -t'
682
+ # }
683
+ config.action_mailer.perform_deliveries = true
684
+ config.action_mailer.raise_delivery_errors = true
685
+ config.action_mailer.default_options = {from: 'no-reply@example.com'}
686
+ ```
687
+
688
+ ### Action Mailer Configuration for Gmail
689
+
690
+ As Action Mailer now uses the [Mail gem](https://github.com/mikel/mail), this
691
+ becomes as simple as adding to your `config/environments/$RAILS_ENV.rb` file:
692
+
693
+ ```ruby
694
+ config.action_mailer.delivery_method = :smtp
695
+ config.action_mailer.smtp_settings = {
696
+ address: 'smtp.gmail.com',
697
+ port: 587,
698
+ domain: 'example.com',
699
+ user_name: '<username>',
700
+ password: '<password>',
701
+ authentication: 'plain',
702
+ enable_starttls_auto: true }
703
+ ```
704
+
705
+ Mailer Testing
706
+ --------------
707
+
708
+ You can find detailed instructions on how to test your mailers in the
709
+ [testing guide](testing.html#testing-your-mailers).
710
+
711
+ Intercepting Emails
712
+ -------------------
713
+
714
+ There are situations where you need to edit an email before it's
715
+ delivered. Fortunately Action Mailer provides hooks to intercept every
716
+ email. You can register an interceptor to make modifications to mail messages
717
+ right before they are handed to the delivery agents.
718
+
719
+ ```ruby
720
+ class SandboxEmailInterceptor
721
+ def self.delivering_email(message)
722
+ message.to = ['sandbox@example.com']
723
+ end
724
+ end
725
+ ```
726
+
727
+ Before the interceptor can do its job you need to register it with the Action
728
+ Mailer framework. You can do this in an initializer file
729
+ `config/initializers/sandbox_email_interceptor.rb`
730
+
731
+ ```ruby
732
+ ActionMailer::Base.register_interceptor(SandboxEmailInterceptor) if Rails.env.staging?
733
+ ```
734
+
735
+ NOTE: The example above uses a custom environment called "staging" for a
736
+ production like server but for testing purposes. You can read
737
+ [Creating Rails environments](configuring.html#creating-rails-environments)
738
+ for more information about custom Rails environments.