rails 3.2.22.5 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +77 -0
  3. data/guides/CHANGELOG.md +9 -0
  4. data/guides/Rakefile +77 -0
  5. data/guides/assets/images/belongs_to.png +0 -0
  6. data/guides/assets/images/book_icon.gif +0 -0
  7. data/guides/assets/images/bullet.gif +0 -0
  8. data/guides/assets/images/challenge.png +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/confirm_dialog.png +0 -0
  19. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  20. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  21. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  22. data/guides/assets/images/getting_started/new_post.png +0 -0
  23. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  24. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  25. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  26. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  27. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  28. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  29. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  30. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  31. data/guides/assets/images/grey_bullet.gif +0 -0
  32. data/guides/assets/images/habtm.png +0 -0
  33. data/guides/assets/images/has_many.png +0 -0
  34. data/guides/assets/images/has_many_through.png +0 -0
  35. data/guides/assets/images/has_one.png +0 -0
  36. data/guides/assets/images/has_one_through.png +0 -0
  37. data/guides/assets/images/header_backdrop.png +0 -0
  38. data/guides/assets/images/header_tile.gif +0 -0
  39. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  40. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  41. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  42. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  43. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  44. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  45. data/guides/assets/images/icons/README +5 -0
  46. data/guides/assets/images/icons/callouts/1.png +0 -0
  47. data/guides/assets/images/icons/callouts/10.png +0 -0
  48. data/guides/assets/images/icons/callouts/11.png +0 -0
  49. data/guides/assets/images/icons/callouts/12.png +0 -0
  50. data/guides/assets/images/icons/callouts/13.png +0 -0
  51. data/guides/assets/images/icons/callouts/14.png +0 -0
  52. data/guides/assets/images/icons/callouts/15.png +0 -0
  53. data/guides/assets/images/icons/callouts/2.png +0 -0
  54. data/guides/assets/images/icons/callouts/3.png +0 -0
  55. data/guides/assets/images/icons/callouts/4.png +0 -0
  56. data/guides/assets/images/icons/callouts/5.png +0 -0
  57. data/guides/assets/images/icons/callouts/6.png +0 -0
  58. data/guides/assets/images/icons/callouts/7.png +0 -0
  59. data/guides/assets/images/icons/callouts/8.png +0 -0
  60. data/guides/assets/images/icons/callouts/9.png +0 -0
  61. data/guides/assets/images/icons/caution.png +0 -0
  62. data/guides/assets/images/icons/example.png +0 -0
  63. data/guides/assets/images/icons/home.png +0 -0
  64. data/guides/assets/images/icons/important.png +0 -0
  65. data/guides/assets/images/icons/next.png +0 -0
  66. data/guides/assets/images/icons/note.png +0 -0
  67. data/guides/assets/images/icons/prev.png +0 -0
  68. data/guides/assets/images/icons/tip.png +0 -0
  69. data/guides/assets/images/icons/up.png +0 -0
  70. data/guides/assets/images/icons/warning.png +0 -0
  71. data/guides/assets/images/jaimeiniesta.jpg +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/rails_welcome.png +0 -0
  81. data/guides/assets/images/session_fixation.png +0 -0
  82. data/guides/assets/images/tab_grey.gif +0 -0
  83. data/guides/assets/images/tab_info.gif +0 -0
  84. data/guides/assets/images/tab_note.gif +0 -0
  85. data/guides/assets/images/tab_red.gif +0 -0
  86. data/guides/assets/images/tab_yellow.gif +0 -0
  87. data/guides/assets/images/tab_yellow.png +0 -0
  88. data/guides/assets/images/vijaydev.jpg +0 -0
  89. data/guides/assets/javascripts/guides.js +57 -0
  90. data/guides/assets/javascripts/jquery.min.js +4 -0
  91. data/guides/assets/javascripts/responsive-tables.js +43 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  116. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  117. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  118. data/guides/assets/stylesheets/fixes.css +16 -0
  119. data/guides/assets/stylesheets/kindle.css +11 -0
  120. data/guides/assets/stylesheets/main.css +709 -0
  121. data/guides/assets/stylesheets/print.css +52 -0
  122. data/guides/assets/stylesheets/reset.css +43 -0
  123. data/guides/assets/stylesheets/responsive-tables.css +50 -0
  124. data/guides/assets/stylesheets/style.css +13 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  133. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  142. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  143. data/guides/code/getting_started/Gemfile +38 -0
  144. data/guides/code/getting_started/Gemfile.lock +150 -0
  145. data/guides/code/getting_started/README.rdoc +28 -0
  146. data/guides/code/getting_started/Rakefile +6 -0
  147. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  148. data/guides/code/getting_started/app/assets/javascripts/application.js +16 -0
  149. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  150. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  151. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +3 -0
  152. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  153. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  154. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  155. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +3 -0
  156. data/guides/code/getting_started/app/controllers/application_controller.rb +5 -0
  157. data/guides/code/getting_started/app/controllers/comments_controller.rb +17 -0
  158. data/guides/code/getting_started/app/controllers/posts_controller.rb +47 -0
  159. data/guides/code/getting_started/app/controllers/welcome_controller.rb +4 -0
  160. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  161. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  162. data/guides/code/getting_started/app/helpers/posts_helper.rb +2 -0
  163. data/guides/code/getting_started/app/helpers/welcome_helper.rb +2 -0
  164. data/guides/code/getting_started/app/models/comment.rb +3 -0
  165. data/guides/code/getting_started/app/models/post.rb +7 -0
  166. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  167. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  168. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  169. data/guides/code/getting_started/app/views/posts/_form.html.erb +27 -0
  170. data/guides/code/getting_started/app/views/posts/edit.html.erb +5 -0
  171. data/guides/code/getting_started/app/views/posts/index.html.erb +21 -0
  172. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  173. data/guides/code/getting_started/app/views/posts/show.html.erb +18 -0
  174. data/guides/code/getting_started/app/views/welcome/index.html.erb +3 -0
  175. data/guides/code/getting_started/bin/bundle +4 -0
  176. data/guides/code/getting_started/bin/rails +4 -0
  177. data/guides/code/getting_started/bin/rake +4 -0
  178. data/guides/code/getting_started/config.ru +4 -0
  179. data/guides/code/getting_started/config/application.rb +17 -0
  180. data/guides/code/getting_started/config/boot.rb +4 -0
  181. data/guides/code/getting_started/config/database.yml +25 -0
  182. data/guides/code/getting_started/config/environment.rb +5 -0
  183. data/guides/code/getting_started/config/environments/development.rb +30 -0
  184. data/guides/code/getting_started/config/environments/production.rb +80 -0
  185. data/guides/code/getting_started/config/environments/test.rb +36 -0
  186. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  187. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +4 -0
  188. data/guides/code/getting_started/config/initializers/inflections.rb +16 -0
  189. data/guides/code/getting_started/config/initializers/locale.rb +9 -0
  190. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  191. data/guides/code/getting_started/config/initializers/secret_token.rb +12 -0
  192. data/guides/code/getting_started/config/initializers/session_store.rb +3 -0
  193. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  194. data/guides/code/getting_started/config/locales/en.yml +23 -0
  195. data/guides/code/getting_started/config/routes.rb +7 -0
  196. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +10 -0
  197. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +11 -0
  198. data/guides/code/getting_started/db/schema.rb +33 -0
  199. data/guides/code/getting_started/db/seeds.rb +7 -0
  200. data/guides/code/getting_started/public/404.html +27 -0
  201. data/guides/code/getting_started/public/422.html +26 -0
  202. data/guides/code/getting_started/public/500.html +26 -0
  203. data/guides/code/getting_started/public/favicon.ico +0 -0
  204. data/guides/code/getting_started/public/robots.txt +5 -0
  205. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +7 -0
  206. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +7 -0
  207. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +9 -0
  208. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  209. data/guides/code/getting_started/test/fixtures/posts.yml +9 -0
  210. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +4 -0
  211. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +4 -0
  212. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +4 -0
  213. data/guides/code/getting_started/test/models/comment_test.rb +7 -0
  214. data/guides/code/getting_started/test/models/post_test.rb +7 -0
  215. data/guides/code/getting_started/test/test_helper.rb +15 -0
  216. data/guides/rails_guides.rb +44 -0
  217. data/guides/rails_guides/generator.rb +248 -0
  218. data/guides/rails_guides/helpers.rb +51 -0
  219. data/guides/rails_guides/indexer.rb +68 -0
  220. data/guides/rails_guides/kindle.rb +119 -0
  221. data/guides/rails_guides/levenshtein.rb +31 -0
  222. data/guides/rails_guides/markdown.rb +163 -0
  223. data/guides/rails_guides/markdown/renderer.rb +82 -0
  224. data/guides/source/2_2_release_notes.md +435 -0
  225. data/guides/source/2_3_release_notes.md +621 -0
  226. data/guides/source/3_0_release_notes.md +614 -0
  227. data/guides/source/3_1_release_notes.md +556 -0
  228. data/guides/source/3_2_release_notes.md +565 -0
  229. data/guides/source/4_0_release_notes.md +228 -0
  230. data/guides/source/_license.html.erb +2 -0
  231. data/guides/source/_welcome.html.erb +19 -0
  232. data/guides/source/action_controller_overview.md +872 -0
  233. data/guides/source/action_mailer_basics.md +599 -0
  234. data/guides/source/action_view_overview.md +1565 -0
  235. data/guides/source/active_model_basics.md +200 -0
  236. data/guides/source/active_record_basics.md +370 -0
  237. data/guides/source/active_record_callbacks.md +358 -0
  238. data/guides/source/active_record_querying.md +1621 -0
  239. data/guides/source/active_record_validations.md +1128 -0
  240. data/guides/source/active_support_core_extensions.md +3791 -0
  241. data/guides/source/active_support_instrumentation.md +487 -0
  242. data/guides/source/api_documentation_guidelines.md +209 -0
  243. data/guides/source/asset_pipeline.md +832 -0
  244. data/guides/source/association_basics.md +2129 -0
  245. data/guides/source/caching_with_rails.md +350 -0
  246. data/guides/source/command_line.md +594 -0
  247. data/guides/source/configuring.md +736 -0
  248. data/guides/source/contributing_to_ruby_on_rails.md +455 -0
  249. data/guides/source/credits.html.erb +76 -0
  250. data/guides/source/debugging_rails_applications.md +675 -0
  251. data/guides/source/development_dependencies_install.md +195 -0
  252. data/guides/source/documents.yaml +179 -0
  253. data/guides/source/engines.md +961 -0
  254. data/guides/source/form_helpers.md +955 -0
  255. data/guides/source/generators.md +644 -0
  256. data/guides/source/getting_started.md +1775 -0
  257. data/guides/source/i18n.md +983 -0
  258. data/guides/source/index.html.erb +27 -0
  259. data/guides/source/initialization.md +562 -0
  260. data/guides/source/kindle/KINDLE.md +26 -0
  261. data/guides/source/kindle/copyright.html.erb +1 -0
  262. data/guides/source/kindle/layout.html.erb +27 -0
  263. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  264. data/guides/source/kindle/toc.html.erb +24 -0
  265. data/guides/source/kindle/toc.ncx.erb +64 -0
  266. data/guides/source/kindle/welcome.html.erb +5 -0
  267. data/guides/source/layout.html.erb +148 -0
  268. data/guides/source/layouts_and_rendering.md +1132 -0
  269. data/guides/source/migrations.md +1059 -0
  270. data/guides/source/nested_model_forms.md +225 -0
  271. data/guides/source/plugins.md +435 -0
  272. data/guides/source/rails_application_templates.md +229 -0
  273. data/guides/source/rails_on_rack.md +342 -0
  274. data/guides/source/routing.md +1088 -0
  275. data/guides/source/ruby_on_rails_guides_guidelines.md +124 -0
  276. data/guides/source/security.md +973 -0
  277. data/guides/source/testing.md +981 -0
  278. data/guides/source/upgrading_ruby_on_rails.md +286 -0
  279. data/guides/source/working_with_javascript_in_rails.md +396 -0
  280. data/guides/w3c_validator.rb +95 -0
  281. metadata +315 -31
@@ -0,0 +1,599 @@
1
+ Action Mailer Basics
2
+ ====================
3
+
4
+ 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.
5
+
6
+ After reading this guide, you will know:
7
+
8
+ * How to send and receive email within a Rails application.
9
+ * How to generate and edit an Action Mailer class and mailer view.
10
+ * How to configure Action Mailer for your environment.
11
+ * How to test your Action Mailer classes.
12
+ --------------------------------------------------------------------------------
13
+
14
+ Introduction
15
+ ------------
16
+
17
+ 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 `ActionMailer::Base` and live in `app/mailers`. Those mailers have associated views that appear alongside controller views in `app/views`.
18
+
19
+ Sending Emails
20
+ --------------
21
+
22
+ This section will provide a step-by-step guide to creating a mailer and its views.
23
+
24
+ ### Walkthrough to Generating a Mailer
25
+
26
+ #### Create the Mailer
27
+
28
+ ```bash
29
+ $ rails generate mailer UserMailer
30
+ create app/mailers/user_mailer.rb
31
+ invoke erb
32
+ create app/views/user_mailer
33
+ invoke test_unit
34
+ create test/mailers/user_mailer_test.rb
35
+ ```
36
+
37
+ So we got the mailer, the views, and the tests.
38
+
39
+ #### Edit the Mailer
40
+
41
+ `app/mailers/user_mailer.rb` contains an empty mailer:
42
+
43
+ ```ruby
44
+ class UserMailer < ActionMailer::Base
45
+ default from: 'from@example.com'
46
+ end
47
+ ```
48
+
49
+ Let's add a method called `welcome_email`, that will send an email to the user's registered email address:
50
+
51
+ ```ruby
52
+ class UserMailer < ActionMailer::Base
53
+ default from: 'notifications@example.com'
54
+
55
+ def welcome_email(user)
56
+ @user = user
57
+ @url = 'http://example.com/login'
58
+ mail(to: user.email, subject: 'Welcome to My Awesome Site')
59
+ end
60
+ end
61
+ ```
62
+
63
+ 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.
64
+
65
+ * `default Hash` - This is a hash of default values for any email you send, in this case we are setting the `:from` header to a value for all messages in this class, this can be overridden on a per email basis
66
+ * `mail` - The actual email message, we are passing the `:to` and `:subject` headers in.
67
+
68
+ Just like controllers, any instance variables we define in the method become available for use in the views.
69
+
70
+ #### Create a Mailer View
71
+
72
+ Create a file called `welcome_email.html.erb` in `app/views/user_mailer/`. This will be the template used for the email, formatted in HTML:
73
+
74
+ ```html+erb
75
+ <!DOCTYPE html>
76
+ <html>
77
+ <head>
78
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
79
+ </head>
80
+ <body>
81
+ <h1>Welcome to example.com, <%= @user.name %></h1>
82
+ <p>
83
+ You have successfully signed up to example.com,
84
+ your username is: <%= @user.login %>.<br/>
85
+ </p>
86
+ <p>
87
+ To login to the site, just follow this link: <%= @url %>.
88
+ </p>
89
+ <p>Thanks for joining and have a great day!</p>
90
+ </body>
91
+ </html>
92
+ ```
93
+
94
+ It is also a good idea to make a text part for this email. To do this, create a file called `welcome_email.text.erb` in `app/views/user_mailer/`:
95
+
96
+ ```erb
97
+ Welcome to example.com, <%= @user.name %>
98
+ ===============================================
99
+
100
+ You have successfully signed up to example.com,
101
+ your username is: <%= @user.login %>.
102
+
103
+ To login to the site, just follow this link: <%= @url %>.
104
+
105
+ Thanks for joining and have a great day!
106
+ ```
107
+
108
+ When you call the `mail` method now, Action Mailer will detect the two templates (text and HTML) and automatically generate a `multipart/alternative` email.
109
+
110
+ #### Wire It Up So That the System Sends the Email When a User Signs Up
111
+
112
+ 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, mailers are really just another way to render a view. Instead of rendering a view and sending out the HTTP 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.
113
+
114
+ Setting this up is painfully simple.
115
+
116
+ First off, we need to create a simple `User` scaffold:
117
+
118
+ ```bash
119
+ $ rails generate scaffold user name:string email:string login:string
120
+ $ rake db:migrate
121
+ ```
122
+
123
+ Now that we have a user model to play with, we will just edit the `app/controllers/users_controller.rb` make it instruct the UserMailer to deliver an email to the newly created user by editing the create action and inserting a call to `UserMailer.welcome_email` right after the user is successfully saved:
124
+
125
+ ```ruby
126
+ class UsersController < ApplicationController
127
+ # POST /users
128
+ # POST /users.json
129
+ def create
130
+ @user = User.new(params[:user])
131
+
132
+ respond_to do |format|
133
+ if @user.save
134
+ # Tell the UserMailer to send a welcome Email after save
135
+ UserMailer.welcome_email(@user).deliver
136
+
137
+ format.html { redirect_to(@user, notice: 'User was successfully created.') }
138
+ format.json { render json: @user, status: :created, location: @user }
139
+ else
140
+ format.html { render action: 'new' }
141
+ format.json { render json: @user.errors, status: :unprocessable_entity }
142
+ end
143
+ end
144
+ end
145
+ end
146
+ ```
147
+
148
+ This provides a much simpler implementation that does not require the registering of observers and the like.
149
+
150
+ The method `welcome_email` returns a `Mail::Message` object which can then just be told `deliver` to send itself out.
151
+
152
+ ### Auto encoding header values
153
+
154
+ Action Mailer now handles the auto encoding of multibyte characters inside of headers and bodies.
155
+
156
+ If you are using UTF-8 as your character set, you do not have to do anything special, just go ahead and send in UTF-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-ASCII.
157
+
158
+ For more complex examples such as defining alternate character sets or self-encoding text first, please refer to the Mail library.
159
+
160
+ ### Complete List of Action Mailer Methods
161
+
162
+ There are just three methods that you need to send pretty much any email message:
163
+
164
+ * `headers` - Specifies any header on the email you want. You can pass a hash of header field names and value pairs, or you can call `headers[:field_name] = 'value'`.
165
+ * `attachments` - Allows you to add attachments to your email. For example, `attachments['file-name.jpg'] = File.read('file-name.jpg')`.
166
+ * `mail` - 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.
167
+
168
+ #### Custom Headers
169
+
170
+ Defining custom headers are simple, you can do it one of three ways:
171
+
172
+ * Defining a header field as a parameter to the `mail` method:
173
+
174
+ ```ruby
175
+ mail('X-Spam' => value)
176
+ ```
177
+
178
+ * Passing in a key value assignment to the `headers` method:
179
+
180
+ ```ruby
181
+ headers['X-Spam'] = value
182
+ ```
183
+
184
+ * Passing a hash of key value pairs to the `headers` method:
185
+
186
+ ```ruby
187
+ headers {'X-Spam' => value, 'X-Special' => another_value}
188
+ ```
189
+
190
+ TIP: All `X-Value` headers per the RFC2822 can appear more than once. If you want to delete an `X-Value` header, you need to assign it a value of `nil`.
191
+
192
+ #### Adding Attachments
193
+
194
+ Adding attachments has been simplified in Action Mailer 3.0.
195
+
196
+ * 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.
197
+
198
+ ```ruby
199
+ attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
200
+ ```
201
+
202
+ NOTE: 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 `Hash` to the `attachments` method.
203
+
204
+ * Pass the file name and specify headers and content and Action Mailer and Mail will use the settings you pass in.
205
+
206
+ ```ruby
207
+ encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
208
+ attachments['filename.jpg'] = {mime_type: 'application/x-gzip',
209
+ encoding: 'SpecialEncoding',
210
+ content: encoded_content }
211
+ ```
212
+
213
+ NOTE: If you specify an encoding, Mail will assume that your content is already encoded and not try to Base64 encode it.
214
+
215
+ #### Making Inline Attachments
216
+
217
+ 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.
218
+
219
+ * Firstly, to tell Mail to turn an attachment into an inline attachment, you just call `#inline` on the attachments method within your Mailer:
220
+
221
+ ```ruby
222
+ def welcome
223
+ attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
224
+ end
225
+ ```
226
+
227
+ * Then in your view, you can just reference `attachments[]` as a hash and specify which attachment you want to show, calling `url` on it and then passing the result into the `image_tag` method:
228
+
229
+ ```html+erb
230
+ <p>Hello there, this is our image</p>
231
+
232
+ <%= image_tag attachments['image.jpg'].url %>
233
+ ```
234
+
235
+ * As this is a standard call to `image_tag` you can pass in an options hash after the attachment URL as you could for any other image:
236
+
237
+ ```html+erb
238
+ <p>Hello there, this is our image</p>
239
+
240
+ <%= image_tag attachments['image.jpg'].url, alt: 'My Photo',
241
+ class: 'photos' %>
242
+ ```
243
+
244
+ #### Sending Email To Multiple Recipients
245
+
246
+ It is possible to send email to one or more recipients in one email (e.g., informing all admins of a new signup) by setting the list of emails to the `:to` key. The list of emails can be an array of email addresses or a single string with the addresses separated by commas.
247
+
248
+ ```ruby
249
+ class AdminMailer < ActionMailer::Base
250
+ default to: Proc.new { Admin.pluck(:email) },
251
+ from: 'notification@example.com'
252
+
253
+ def new_registration(user)
254
+ @user = user
255
+ mail(subject: "New User Signup: #{@user.email}")
256
+ end
257
+ end
258
+ ```
259
+
260
+ The same format can be used to set carbon copy (Cc:) and blind carbon copy (Bcc:) recipients, by using the `:cc` and `:bcc` keys respectively.
261
+
262
+ #### Sending Email With Name
263
+
264
+ 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
265
+ to format the email address in the format `"Name <email>"`.
266
+
267
+ ```ruby
268
+ def welcome_email(user)
269
+ @user = user
270
+ email_with_name = "#{@user.name} <#{@user.email}>"
271
+ mail(to: email_with_name, subject: 'Welcome to My Awesome Site')
272
+ end
273
+ ```
274
+
275
+ ### Mailer Views
276
+
277
+ Mailer views are located in the `app/views/name_of_mailer_class` 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 `welcome_email` method will be in `app/views/user_mailer/welcome_email.html.erb` for the HTML version and `welcome_email.text.erb` for the plain text version.
278
+
279
+ To change the default mailer view for your action you do something like:
280
+
281
+ ```ruby
282
+ class UserMailer < ActionMailer::Base
283
+ default from: 'notifications@example.com'
284
+
285
+ def welcome_email(user)
286
+ @user = user
287
+ @url = 'http://example.com/login'
288
+ mail(to: user.email,
289
+ subject: 'Welcome to My Awesome Site',
290
+ template_path: 'notifications',
291
+ template_name: 'another')
292
+ end
293
+ end
294
+ ```
295
+
296
+ In this case it will look for templates at `app/views/notifications` with name `another`. You can also specify an array of paths for `template_path`, and they will be searched in order.
297
+
298
+ 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:
299
+
300
+ ```ruby
301
+ class UserMailer < ActionMailer::Base
302
+ default from: 'notifications@example.com'
303
+
304
+ def welcome_email(user)
305
+ @user = user
306
+ @url = 'http://example.com/login'
307
+ mail(to: user.email,
308
+ subject: 'Welcome to My Awesome Site') do |format|
309
+ format.html { render 'another_template' }
310
+ format.text { render text: 'Render text' }
311
+ end
312
+ end
313
+
314
+ end
315
+ ```
316
+
317
+ This will render the template 'another_template.html.erb' for the HTML 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 `:text`, `:inline` etc.
318
+
319
+ ### Action Mailer Layouts
320
+
321
+ Just like controller views, you can also have mailer layouts. The layout name needs to be the same as your mailer, such as `user_mailer.html.erb` and `user_mailer.text.erb` to be automatically recognized by your mailer as a layout.
322
+
323
+ In order to use a different file just use:
324
+
325
+ ```ruby
326
+ class UserMailer < ActionMailer::Base
327
+ layout 'awesome' # use awesome.(html|text).erb as the layout
328
+ end
329
+ ```
330
+
331
+ Just like with controller views, use `yield` to render the view inside the layout.
332
+
333
+ You can also pass in a `layout: 'layout_name'` option to the render call inside the format block to specify different layouts for different actions:
334
+
335
+ ```ruby
336
+ class UserMailer < ActionMailer::Base
337
+ def welcome_email(user)
338
+ mail(to: user.email) do |format|
339
+ format.html { render layout: 'my_layout' }
340
+ format.text
341
+ end
342
+ end
343
+ end
344
+ ```
345
+
346
+ Will render the HTML part using the `my_layout.html.erb` file and the text part with the usual `user_mailer.text.erb` file if it exists.
347
+
348
+ ### Generating URLs in Action Mailer Views
349
+
350
+ URLs can be generated in mailer views using `url_for` or named routes.
351
+
352
+ Unlike controllers, the mailer instance doesn't have any context about the incoming request so you'll need to provide the `:host`, `:controller`, and `:action`:
353
+
354
+ ```erb
355
+ <%= url_for(host: 'example.com',
356
+ controller: 'welcome',
357
+ action: 'greeting') %>
358
+ ```
359
+
360
+ When using named routes you only need to supply the `:host`:
361
+
362
+ ```erb
363
+ <%= user_url(@user, host: 'example.com') %>
364
+ ```
365
+
366
+ Email clients have no web context and so paths have no base URL to form complete web addresses. Thus, when using named routes only the "_url" variant makes sense.
367
+
368
+ It is also possible to set a default host that will be used in all mailers by setting the `:host` option as a configuration option in `config/application.rb`:
369
+
370
+ ```ruby
371
+ config.action_mailer.default_url_options = { host: 'example.com' }
372
+ ```
373
+
374
+ If you use this setting, you should pass the `only_path: false` option when using `url_for`. This will ensure that absolute URLs are generated because the `url_for` view helper will, by default, generate relative URLs when a `:host` option isn't explicitly provided.
375
+
376
+ ### Sending Multipart Emails
377
+
378
+ Action Mailer will automatically send multipart emails if you have different templates for the same action. So, for our UserMailer example, if you have `welcome_email.text.erb` and `welcome_email.html.erb` in `app/views/user_mailer`, Action Mailer will automatically send a multipart email with the HTML and text versions setup as different parts.
379
+
380
+ The order of the parts getting inserted is determined by the `:parts_order` inside of the `ActionMailer::Base.default` method.
381
+
382
+ ### Sending Emails with Attachments
383
+
384
+ Attachments can be added by using the `attachments` method:
385
+
386
+ ```ruby
387
+ class UserMailer < ActionMailer::Base
388
+ def welcome_email(user)
389
+ @user = user
390
+ @url = user_url(@user)
391
+ attachments['terms.pdf'] = File.read('/path/terms.pdf')
392
+ mail(to: user.email,
393
+ subject: 'Please see the Terms and Conditions attached')
394
+ end
395
+ end
396
+ ```
397
+
398
+ The above will send a multipart email with an attachment, properly nested with the top level being `multipart/mixed` and the first part being a `multipart/alternative` containing the plain text and HTML email messages.
399
+
400
+ ### Sending Emails with Dynamic Delivery Options
401
+
402
+ If you wish to override the default delivery options (e.g. SMTP credentials) while delivering emails, you can do this using `delivery_method_options` in the mailer action.
403
+
404
+ ```ruby
405
+ class UserMailer < ActionMailer::Base
406
+ def welcome_email(user,company)
407
+ @user = user
408
+ @url = user_url(@user)
409
+ delivery_options = { user_name: company.smtp_user, password: company.smtp_password, address: company.smtp_host }
410
+ mail(to: user.email, subject: "Please see the Terms and Conditions attached", delivery_method_options: delivery_options)
411
+ end
412
+ end
413
+ ```
414
+
415
+ Receiving Emails
416
+ ----------------
417
+
418
+ 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'll need to:
419
+
420
+ * Implement a `receive` method in your mailer.
421
+
422
+ * Configure your email server to forward emails from the address(es) you would like your app to receive to `/path/to/app/bin/rails runner 'UserMailer.receive(STDIN.read)'`.
423
+
424
+ Once a method called `receive` is defined in any mailer, Action Mailer will parse the raw incoming email into an email object, decode it, instantiate a new mailer, and pass the email object to the mailer `receive` instance method. Here's an example:
425
+
426
+ ```ruby
427
+ class UserMailer < ActionMailer::Base
428
+ def receive(email)
429
+ page = Page.find_by_address(email.to.first)
430
+ page.emails.create(
431
+ subject: email.subject,
432
+ body: email.body
433
+ )
434
+
435
+ if email.has_attachments?
436
+ email.attachments.each do |attachment|
437
+ page.attachments.create({
438
+ file: attachment,
439
+ description: email.subject
440
+ })
441
+ end
442
+ end
443
+ end
444
+ end
445
+ ```
446
+
447
+ Action Mailer Callbacks
448
+ ---------------------------
449
+
450
+ Action Mailer allows for you to specify a `before_action`, `after_action` and 'around_action'.
451
+
452
+ * Filters can be specified with a block or a symbol to a method in the mailer class similar to controllers.
453
+
454
+ * You could use a `before_action` to prepopulate the mail object with defaults, delivery_method_options or insert default headers and attachments.
455
+
456
+ * You could use an `after_action` to do similar setup as a `before_action` but using instance variables set in your mailer action.
457
+
458
+ ```ruby
459
+ class UserMailer < ActionMailer::Base
460
+ after_action :set_delivery_options, :prevent_delivery_to_guests, :set_business_headers
461
+
462
+ def feedback_message(business, user)
463
+ @business = business
464
+ @user = user
465
+ mail
466
+ end
467
+
468
+ def campaign_message(business, user)
469
+ @business = business
470
+ @user = user
471
+ end
472
+
473
+ private
474
+
475
+ def set_delivery_options
476
+ # You have access to the mail instance and @business and @user instance variables here
477
+ if @business && @business.has_smtp_settings?
478
+ mail.delivery_method.settings.merge!(@business.smtp_settings)
479
+ end
480
+ end
481
+
482
+ def prevent_delivery_to_guests
483
+ if @user && @user.guest?
484
+ mail.perform_deliveries = false
485
+ end
486
+ end
487
+
488
+ def set_business_headers
489
+ if @business
490
+ headers["X-SMTPAPI-CATEGORY"] = @business.code
491
+ end
492
+ end
493
+ end
494
+ ```
495
+
496
+ * Mailer Filters abort further processing if body is set to a non-nil value.
497
+
498
+ Using Action Mailer Helpers
499
+ ---------------------------
500
+
501
+ Action Mailer now just inherits from Abstract Controller, so you have access to the same generic helpers as you do in Action Controller.
502
+
503
+ Action Mailer Configuration
504
+ ---------------------------
505
+
506
+ The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc...)
507
+
508
+ | Configuration | Description |
509
+ |---------------|-------------|
510
+ |`template_root`|Determines the base from which template references will be made.|
511
+ |`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.|
512
+ |`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>|
513
+ |`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>|
514
+ |`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.|
515
+ |`delivery_method`|Defines a delivery method. Possible values are `:smtp` (default), `:sendmail`, `:file` and `:test`.|
516
+ |`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.|
517
+ |`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.|
518
+ |`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).|
519
+
520
+ ### Example Action Mailer Configuration
521
+
522
+ An example would be adding the following to your appropriate `config/environments/$RAILS_ENV.rb` file:
523
+
524
+ ```ruby
525
+ config.action_mailer.delivery_method = :sendmail
526
+ # Defaults to:
527
+ # config.action_mailer.sendmail_settings = {
528
+ # location: '/usr/sbin/sendmail',
529
+ # arguments: '-i -t'
530
+ # }
531
+ config.action_mailer.perform_deliveries = true
532
+ config.action_mailer.raise_delivery_errors = true
533
+ config.action_mailer.default_options = {from: 'no-replay@example.org'}
534
+ ```
535
+
536
+ ### Action Mailer Configuration for GMail
537
+
538
+ As Action Mailer now uses the Mail gem, this becomes as simple as adding to your `config/environments/$RAILS_ENV.rb` file:
539
+
540
+ ```ruby
541
+ config.action_mailer.delivery_method = :smtp
542
+ config.action_mailer.smtp_settings = {
543
+ address: 'smtp.gmail.com',
544
+ port: 587,
545
+ domain: 'baci.lindsaar.net',
546
+ user_name: '<username>',
547
+ password: '<password>',
548
+ authentication: 'plain',
549
+ enable_starttls_auto: true }
550
+ ```
551
+
552
+ Mailer Testing
553
+ --------------
554
+
555
+ By default Action Mailer does not send emails in the test environment. They are just added to the `ActionMailer::Base.deliveries` array.
556
+
557
+ 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:
558
+
559
+ ```ruby
560
+ class UserMailerTest < ActionMailer::TestCase
561
+ def test_welcome_email
562
+ user = users(:some_user_in_your_fixtures)
563
+
564
+ # Send the email, then test that it got queued
565
+ email = UserMailer.welcome_email(user).deliver
566
+ assert !ActionMailer::Base.deliveries.empty?
567
+
568
+ # Test the body of the sent email contains what we expect it to
569
+ assert_equal [user.email], email.to
570
+ assert_equal 'Welcome to My Awesome Site', email.subject
571
+ assert_match "<h1>Welcome to example.com, #{user.name}</h1>", email.body.to_s
572
+ assert_match 'you have joined to example.com community', email.body.to_s
573
+ end
574
+ end
575
+ ```
576
+
577
+ In the test we send the email and store the returned object in the `email` 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.
578
+
579
+ NOTE: The `ActionMailer::Base.deliveries` array is only reset automatically in `ActionMailer::TestCase` tests. If you want to have a clean slate outside Action Mailer tests, you can reset it manually with: `ActionMailer::Base.deliveries.clear`
580
+
581
+ Intercepting Emails
582
+ -------------------
583
+ There are situations where you need to edit an email before it's delivered. Fortunately Action Mailer provides hooks to intercept every email. You can register an interceptor to make modifications to mail messages right before they are handed to the delivery agents.
584
+
585
+ ```ruby
586
+ class SandboxEmailInterceptor
587
+ def self.delivering_email(message)
588
+ message.to = ['sandbox@example.com']
589
+ end
590
+ end
591
+ ```
592
+
593
+ Before the interceptor can do its job you need to register it with the Action Mailer framework. You can do this in an initializer file `config/initializers/sandbox_email_interceptor.rb`
594
+
595
+ ```ruby
596
+ ActionMailer::Base.register_interceptor(SandboxEmailInterceptor) if Rails.env.staging?
597
+ ```
598
+
599
+ NOTE: The example above uses a custom environment called "staging" for a production like server but for testing purposes. You can read [Creating Rails environments](./configuring.html#creating-rails-environments) for more information about custom Rails environments.