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