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