Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 798 lines (749 sloc) 34.211 kb
616ebb8 @josevalim Remove unused code paths and require mail only when it's needed.
josevalim authored
1 require 'mail'
c985a0e Add some tests to collector with templates and any.
José Valim and Mikel Lindsaar authored
2 require 'action_mailer/collector'
284ca81 @joshk remove AM delegating register_observer and register_interceptor to Ma…
joshk authored
3 require 'active_support/core_ext/string/inflections'
0f8a6eb @sikachu Fix missing requires in Action Mailer
sikachu authored
4 require 'active_support/core_ext/hash/except'
c17d200 @lest mailer can be anonymous
lest authored
5 require 'active_support/core_ext/module/anonymous'
6788db8 @josevalim Move Rails::LogSubscriber to ActiveSupport::LogSubscriber, allowing f…
josevalim authored
6 require 'action_mailer/log_subscriber'
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
7
c4590d5 @dhh Fixed docs
dhh authored
8 module ActionMailer #:nodoc:
98dc582 @lifo Merge docrails.
lifo authored
9 # Action Mailer allows you to send email from your application using a mailer model and views.
db045db @dhh Initial
dhh authored
10 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
11 # = Mailer Models
20bd4bd @dhh Updated docs and otherwise
dhh authored
12 #
98dc582 @lifo Merge docrails.
lifo authored
13 # To use Action Mailer, you need to create a mailer model.
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
14 #
f44a0b1 @romdi fix usage examples and more to use new invocations
romdi authored
15 # $ rails generate mailer Notifier
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
16 #
c291195 @seebq Updates ActionMailer Base summary and fixes space in code example.
seebq authored
17 # The generated model inherits from <tt>ActionMailer::Base</tt>. A mailer model defines methods
18 # used to generate an email message. In these methods, you can setup variables to be used in
19 # the mailer views, options on the mail itself such as the <tt>:from</tt> address, and attachments.
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
20 #
21 # Examples:
22 #
0f0213f @siong1987 minor spacing fix in code example in ActionMailer::Base.
siong1987 authored
23 # class Notifier < ActionMailer::Base
24 # default :from => 'no-reply@example.com',
c291195 @seebq Updates ActionMailer Base summary and fixes space in code example.
seebq authored
25 # :return_path => 'system@example.com'
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
26 #
0f0213f @siong1987 minor spacing fix in code example in ActionMailer::Base.
siong1987 authored
27 # def welcome(recipient)
28 # @account = recipient
29 # mail(:to => recipient.email_address_with_name,
30 # :bcc => ["bcc@example.com", "Order Watcher <watcher@example.com>"])
31 # end
32 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
33 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
34 # Within the mailer method, you have access to the following methods:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
35 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
36 # * <tt>attachments[]=</tt> - Allows you to add attachments to your email in an intuitive
37 # manner; <tt>attachments['filename.png'] = File.read('path/to/filename.png')</tt>
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
38 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
39 # * <tt>attachments.inline[]=</tt> - Allows you to add an inline attachment to your email
40 # in the same manner as <tt>attachments[]=</tt>
41 #
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
42 # * <tt>headers[]=</tt> - Allows you to specify any header field in your email such
daad445 @neerajdotname itsy bitsy changes to ActionMailer documentation
neerajdotname authored
43 # as <tt>headers['X-No-Spam'] = 'True'</tt>. Note, while most fields like <tt>To:</tt>
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
44 # <tt>From:</tt> can only appear once in an email header, other fields like <tt>X-Anything</tt>
45 # can appear multiple times. If you want to change a field that can appear multiple times,
daad445 @neerajdotname itsy bitsy changes to ActionMailer documentation
neerajdotname authored
46 # you need to set it to nil first so that Mail knows you are replacing it and not adding
47 # another field of the same name.
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
48 #
9520166 @mikel Fixed up being able to pass random headers in with headers, or mail. …
mikel authored
49 # * <tt>headers(hash)</tt> - Allows you to specify multiple headers in your email such
50 # as <tt>headers({'X-No-Spam' => 'True', 'In-Reply-To' => '1234@message.id'})</tt>
51 #
daad445 @neerajdotname itsy bitsy changes to ActionMailer documentation
neerajdotname authored
52 # * <tt>mail</tt> - Allows you to specify email to be sent.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
53 #
9520166 @mikel Fixed up being able to pass random headers in with headers, or mail. …
mikel authored
54 # The hash passed to the mail method allows you to specify any header that a Mail::Message
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
55 # will accept (any valid Email header including optional fields).
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
56 #
57 # The mail method, if not passed a block, will inspect your views and send all the views with
b62c549 @jasonfox The name for the plain text template in Rails3 appears to be method.t…
jasonfox authored
58 # the same name as the method, so the above action would send the +welcome.text.erb+ view
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
59 # file as well as the +welcome.text.html.erb+ view file in a +multipart/alternative+ email.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
60 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
61 # If you want to explicitly render only certain templates, pass a block:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
62 #
c125116 @dchelimsky docfix (email instead of emai)
dchelimsky authored
63 # mail(:to => user.email) do |format|
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
64 # format.text
65 # format.html
66 # end
20bd4bd @dhh Updated docs and otherwise
dhh authored
67 #
daad445 @neerajdotname itsy bitsy changes to ActionMailer documentation
neerajdotname authored
68 # The block syntax is also useful in providing information specific to a part:
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
69 #
c125116 @dchelimsky docfix (email instead of emai)
dchelimsky authored
70 # mail(:to => user.email) do |format|
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
71 # format.text(:content_transfer_encoding => "base64")
72 # format.html
73 # end
74 #
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
75 # Or even to render a special view:
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
76 #
c125116 @dchelimsky docfix (email instead of emai)
dchelimsky authored
77 # mail(:to => user.email) do |format|
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
78 # format.text
79 # format.html { render "some_other_template" }
80 # end
81 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
82 # = Mailer views
83 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
84 # Like Action Controller, each mailer class has a corresponding view directory in which each
85 # method of the class looks for a template with its name.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
86 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
87 # To define a template to be used with a mailing, create an <tt>.erb</tt> file with the same
88 # name as the method in your mailer model. For example, in the mailer defined above, the template at
b62c549 @jasonfox The name for the plain text template in Rails3 appears to be method.t…
jasonfox authored
89 # <tt>app/views/notifier/welcome.text.erb</tt> would be used to generate the email.
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
90 #
91 # Variables defined in the model are accessible as instance variables in the view.
92 #
93 # Emails by default are sent in plain text, so a sample view for our model example might look like this:
94 #
95 # Hi <%= @account.name %>,
96 # Thanks for joining our service! Please check back often.
97 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
98 # You can even use Action Pack helpers in these views. For example:
99 #
100 # You got a new note!
80f1d40 @avakhov update ActionMailer example using last truncate helper method signature
avakhov authored
101 # <%= truncate(@note.body, :length => 25) %>
ee70d1b @josevalim adv_attr_accessors in ActionMailer are not sent to the views, use the…
josevalim authored
102 #
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
103 # If you need to access the subject, from or the recipients in the view, you can do that through message object:
ee70d1b @josevalim adv_attr_accessors in ActionMailer are not sent to the views, use the…
josevalim authored
104 #
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
105 # You got a new note from <%= message.from %>!
80f1d40 @avakhov update ActionMailer example using last truncate helper method signature
avakhov authored
106 # <%= truncate(@note.body, :length => 25) %>
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
107 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
108 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
109 # = Generating URLs
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
110 #
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
111 # URLs can be generated in mailer views using <tt>url_for</tt> or named routes. Unlike controllers from
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
112 # Action Pack, the mailer instance doesn't have any context about the incoming request, so you'll need
113 # to provide all of the details needed to generate a URL.
20bd4bd @dhh Updated docs and otherwise
dhh authored
114 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
115 # When using <tt>url_for</tt> you'll need to provide the <tt>:host</tt>, <tt>:controller</tt>, and <tt>:action</tt>:
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
116 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
117 # <%= url_for(:host => "example.com", :controller => "welcome", :action => "greeting") %>
20bd4bd @dhh Updated docs and otherwise
dhh authored
118 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
119 # When using named routes you only need to supply the <tt>:host</tt>:
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
120 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
121 # <%= users_url(:host => "example.com") %>
122 #
3438f6c @rafaelfranca Array.wrap is no longer needed here
rafaelfranca authored
123 # You should use the <tt>named_route_url</tt> style (which generates absolute URLs) and avoid using the
124 # <tt>named_route_path</tt> style (which generates relative URLs), since clients reading the mail will
1f4fb6f @mjankowski elaborate details on why to use _url instead of _path in mailers
mjankowski authored
125 # have no concept of a current URL from which to determine a relative path.
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
126 #
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
127 # It is also possible to set a default host that will be used in all mailers by setting the <tt>:host</tt>
ab62aa1 @jeroenvandijk Mention that ActionMailer::Base.default_url_options is now deprecated
jeroenvandijk authored
128 # option as a configuration option in <tt>config/application.rb</tt>:
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
129 #
130 # config.action_mailer.default_url_options = { :host => "example.com" }
20bd4bd @dhh Updated docs and otherwise
dhh authored
131 #
4f433bc @mjankowski clarify why the only_path => false option is needed when default :hos…
mjankowski authored
132 # When you decide to set a default <tt>:host</tt> for your mailers, then you need to make sure to use the
3438f6c @rafaelfranca Array.wrap is no longer needed here
rafaelfranca authored
133 # <tt>:only_path => false</tt> option when using <tt>url_for</tt>. Since the <tt>url_for</tt> view helper
4f433bc @mjankowski clarify why the only_path => false option is needed when default :hos…
mjankowski authored
134 # will generate relative URLs by default when a <tt>:host</tt> option isn't explicitly provided, passing
135 # <tt>:only_path => false</tt> will ensure that absolute URLs are generated.
20bd4bd @dhh Updated docs and otherwise
dhh authored
136 #
137 # = Sending mail
138 #
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
139 # Once a mailer action and template are defined, you can deliver your message or create it and save it
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
140 # for delivery later:
141 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
142 # Notifier.welcome(david).deliver # sends the email
143 # mail = Notifier.welcome(david) # => a Mail::Message object
144 # mail.deliver # sends the email
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
145 #
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
146 # You never instantiate your mailer class. Rather, you just call the method you defined on the class itself.
20bd4bd @dhh Updated docs and otherwise
dhh authored
147 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
148 # = Multipart Emails
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
149 #
3438f6c @rafaelfranca Array.wrap is no longer needed here
rafaelfranca authored
150 # Multipart messages can also be used implicitly because Action Mailer will automatically detect and use
151 # multipart templates, where each template is named after the name of the action, followed by the content
f3db04f @mjankowski grammar (missing "a") and formatting
mjankowski authored
152 # type. Each such detected template will be added as a separate part to the message.
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
153 #
daad445 @neerajdotname itsy bitsy changes to ActionMailer documentation
neerajdotname authored
154 # For example, if the following templates exist:
b62c549 @jasonfox The name for the plain text template in Rails3 appears to be method.t…
jasonfox authored
155 # * signup_notification.text.erb
21187c0 @dhh Apply the rest of Chads patch
dhh authored
156 # * signup_notification.text.html.erb
157 # * signup_notification.text.xml.builder
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
158 # * signup_notification.text.yaml.erb
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
159 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
160 # Each would be rendered and added as a separate part to the message, with the corresponding content
161 # type. The content type for the entire message is automatically set to <tt>multipart/alternative</tt>,
162 # which indicates that the email contains multiple different representations of the same email
163 # body. The same instance variables defined in the action are passed to all email templates.
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
164 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
165 # Implicit template rendering is not performed if any attachments or parts have been added to the email.
166 # This means that you'll have to manually add each part to the email and set the content type of the email
167 # to <tt>multipart/alternative</tt>.
20bd4bd @dhh Updated docs and otherwise
dhh authored
168 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
169 # = Attachments
20bd4bd @dhh Updated docs and otherwise
dhh authored
170 #
daad445 @neerajdotname itsy bitsy changes to ActionMailer documentation
neerajdotname authored
171 # Sending attachment in emails is easy:
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
172 #
173 # class ApplicationMailer < ActionMailer::Base
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
174 # def welcome(recipient)
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
175 # attachments['free_book.pdf'] = File.read('path/to/file.pdf')
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
176 # mail(:to => recipient, :subject => "New account information")
db045db @dhh Initial
dhh authored
177 # end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
178 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
179 #
b62c549 @jasonfox The name for the plain text template in Rails3 appears to be method.t…
jasonfox authored
180 # Which will (if it had both a <tt>welcome.text.erb</tt> and <tt>welcome.text.html.erb</tt>
ccf9577 @dolzenko Fix a bunch of minor spelling mistakes
dolzenko authored
181 # template in the view directory), send a complete <tt>multipart/mixed</tt> email with two parts,
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
182 # the first part being a <tt>multipart/alternative</tt> with the text and HTML email parts inside,
183 # and the second being a <tt>application/pdf</tt> with a Base64 encoded copy of the file.pdf book
184 # with the filename +free_book.pdf+.
c927aa0 @dhh Updated documentation
dhh authored
185 #
45b1045 @dmathieu raise an error if no implicit mailer template could be found
dmathieu authored
186 # If you need to send attachments with no content, you need to create an empty view for it,
187 # or add an empty body parameter like this:
188 #
189 # class ApplicationMailer < ActionMailer::Base
190 # def welcome(recipient)
191 # attachments['free_book.pdf'] = File.read('path/to/file.pdf')
192 # mail(:to => recipient, :subject => "New account information", :body => "")
193 # end
194 # end
195 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
196 # = Inline Attachments
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
197 #
198 # You can also specify that a file should be displayed inline with other HTML. This is useful
daad445 @neerajdotname itsy bitsy changes to ActionMailer documentation
neerajdotname authored
199 # if you want to display a corporate logo or a photo.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
200 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
201 # class ApplicationMailer < ActionMailer::Base
202 # def welcome(recipient)
203 # attachments.inline['photo.png'] = File.read('path/to/photo.png')
204 # mail(:to => recipient, :subject => "Here is what we look like")
205 # end
206 # end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
207 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
208 # And then to reference the image in the view, you create a <tt>welcome.html.erb</tt> file and
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
209 # make a call to +image_tag+ passing in the attachment you want to display and then call
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
210 # +url+ on the attachment to get the relative content id path for the image source:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
211 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
212 # <h1>Please Don't Cringe</h1>
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
213 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
214 # <%= image_tag attachments['photo.png'].url -%>
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
215 #
f17159b @fxn edit pass: the names of Rails components have a space, ie, "Active Re…
fxn authored
216 # As we are using Action View's +image_tag+ method, you can pass in any other options you want:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
217 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
218 # <h1>Please Don't Cringe</h1>
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
219 #
3762362 @mikel Updating readme for ActionMailer::Base
mikel authored
220 # <%= image_tag attachments['photo.png'].url, :alt => 'Our Photo', :class => 'photo' -%>
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
221 #
cefc136 @mikel Adding options to register observers and interceptors through ActionM…
mikel authored
222 # = Observing and Intercepting Mails
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
223 #
5d0d4d8 @smartinez87 Remove extra white spaces on ActionMailer docs.
smartinez87 authored
224 # Action Mailer provides hooks into the Mail observer and interceptor methods. These allow you to
5acf76d @pacoguzman Add action_mailer observer and interceptors configuration options
pacoguzman authored
225 # register classes that are called during the mail delivery life cycle.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
226 #
5acf76d @pacoguzman Add action_mailer observer and interceptors configuration options
pacoguzman authored
227 # An observer class must implement the <tt>:delivered_email(message)</tt> method which will be
cefc136 @mikel Adding options to register observers and interceptors through ActionM…
mikel authored
228 # called once for every email sent after the email has been sent.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
229 #
5acf76d @pacoguzman Add action_mailer observer and interceptors configuration options
pacoguzman authored
230 # An interceptor class must implement the <tt>:delivering_email(message)</tt> method which will be
cefc136 @mikel Adding options to register observers and interceptors through ActionM…
mikel authored
231 # called before the email is sent, allowing you to make modifications to the email before it hits
5acf76d @pacoguzman Add action_mailer observer and interceptors configuration options
pacoguzman authored
232 # the delivery agents. Your class should make any needed modifications directly to the passed
cefc136 @mikel Adding options to register observers and interceptors through ActionM…
mikel authored
233 # in Mail::Message instance.
20bd4bd @dhh Updated docs and otherwise
dhh authored
234 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
235 # = Default Hash
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
236 #
058f7fe @mikel Cleaning up Action Mailer spelling with and without the space
mikel authored
237 # Action Mailer provides some intelligent defaults for your emails, these are usually specified in a
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
238 # default method inside the class definition:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
239 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
240 # class Notifier < ActionMailer::Base
241 # default :sender => 'system@example.com'
242 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
243 #
ad8e1ba @chuyeow Fix missing word in ActionMailer::Base documentation.
chuyeow authored
244 # You can pass in any header value that a <tt>Mail::Message</tt> accepts. Out of the box,
245 # <tt>ActionMailer::Base</tt> sets the following:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
246 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
247 # * <tt>:mime_version => "1.0"</tt>
248 # * <tt>:charset => "UTF-8",</tt>
249 # * <tt>:content_type => "text/plain",</tt>
250 # * <tt>:parts_order => [ "text/plain", "text/enriched", "text/html" ]</tt>
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
251 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
252 # <tt>parts_order</tt> and <tt>charset</tt> are not actually valid <tt>Mail::Message</tt> header fields,
058f7fe @mikel Cleaning up Action Mailer spelling with and without the space
mikel authored
253 # but Action Mailer translates them appropriately and sets the correct values.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
254 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
255 # As you can pass in any header, you need to either quote the header as a string, or pass it in as
b72cd29 @mikel Fixing spelling
mikel authored
256 # an underscored symbol, so the following will work:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
257 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
258 # class Notifier < ActionMailer::Base
259 # default 'Content-Transfer-Encoding' => '7bit',
260 # :content_description => 'This is a description'
261 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
262 #
058f7fe @mikel Cleaning up Action Mailer spelling with and without the space
mikel authored
263 # Finally, Action Mailer also supports passing <tt>Proc</tt> objects into the default hash, so you
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
264 # can define methods that evaluate as the message is being generated:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
265 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
266 # class Notifier < ActionMailer::Base
267 # default 'X-Special-Header' => Proc.new { my_method }
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
268 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
269 # private
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
270 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
271 # def my_method
272 # 'some complex call'
273 # end
274 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
275 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
276 # Note that the proc is evaluated right at the start of the mail message generation, so if you
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
277 # set something in the defaults using a proc, and then set the same thing inside of your
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
278 # mailer method, it will get over written by the mailer method.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
279 #
3009c63 @carlosantoniodasilva Improve docs, changelog and release notes for Action Mailer default_o…
carlosantoniodasilva authored
280 # It is also possible to set these default options that will be used in all mailers through
281 # the <tt>default_options=</tt> configuration in <tt>config/application.rb</tt>:
282 #
283 # config.action_mailer.default_options = { from: "no-reply@example.org" }
284 #
4f28c4f @jsl Add ability to define callbacks in ActionMailer using AbstractControl…
jsl authored
285 # = Callbacks
286 #
287 # You can specify callbacks using before_filter and after_filter for configuring your messages.
288 # This may be useful, for example, when you want to add default inline attachments for all
289 # messages sent out by a certain mailer class:
290 #
291 # class Notifier < ActionMailer::Base
292 # before_filter :add_inline_attachment!
293 #
294 # def welcome
295 # mail
296 # end
297 #
298 # private
299 #
300 # def add_inline_attachment!
301 # attachments.inline["footer.jpg"] = File.read('/path/to/filename.jpg')
302 # end
303 # end
304 #
305 # Callbacks in ActionMailer are implemented using AbstractController::Callbacks, so you
306 # can define and configure callbacks in the same manner that you would use callbacks in
307 # classes that inherit from ActionController::Base.
308 #
309 # Note that unless you have a specific reason to do so, you should prefer using before_filter
310 # rather than after_filter in your ActionMailer classes so that headers are parsed properly.
311 #
c927aa0 @dhh Updated documentation
dhh authored
312 # = Configuration options
313 #
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
314 # These options are specified on the class level, like
e968acc @radar Update ActionMailer documentation to not use deprecated template_root…
radar authored
315 # <tt>ActionMailer::Base.raise_delivery_errors = true</tt>
c927aa0 @dhh Updated documentation
dhh authored
316 #
fb30feb @mikel Adding more docs to ActionMailer
mikel authored
317 # * <tt>default</tt> - You can pass this in at a class level as well as within the class itself as
318 # per the above section.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
319 #
c927aa0 @dhh Updated documentation
dhh authored
320 # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
321 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
322 #
64092de @fxn Improve documentation coverage and markup
fxn authored
323 # * <tt>smtp_settings</tt> - Allows detailed configuration for <tt>:smtp</tt> delivery method:
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
324 # * <tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default
325 # "localhost" setting.
dc4eec1 @lifo Merge docrails:
lifo authored
326 # * <tt>:port</tt> - On the off chance that your mail server doesn't run on port 25, you can change it.
327 # * <tt>:domain</tt> - If you need to specify a HELO domain, you can do it here.
328 # * <tt>:user_name</tt> - If your mail server requires authentication, set the username in this setting.
329 # * <tt>:password</tt> - If your mail server requires authentication, set the password in this setting.
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
330 # * <tt>:authentication</tt> - If your mail server requires authentication, you need to specify the
331 # authentication type here.
5a898e1 Explain actionamailer authentication types
Peter Baker authored
332 # This is a symbol and one of <tt>:plain</tt> (will send the password in the clear), <tt>:login</tt> (will
3457db7 @dalibor Correct spelling for Base64 in action_mailer
dalibor authored
333 # send password Base64 encoded) or <tt>:cram_md5</tt> (combines a Challenge/Response mechanism to exchange
5a898e1 Explain actionamailer authentication types
Peter Baker authored
334 # information and a cryptographic Message Digest 5 algorithm to hash important information)
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
335 # * <tt>:enable_starttls_auto</tt> - When set to true, detects if STARTTLS is enabled in your SMTP server
336 # and starts to use it.
d500ad3 @joshk Removed deprecated ActionMailer API and related tests
joshk authored
337 # * <tt>:openssl_verify_mode</tt> - When using TLS, you can set how OpenSSL checks the certificate. This is
338 # really useful if you need to validate a self-signed and/or a wildcard certificate. You can use the name
339 # of an OpenSSL verify constant ('none', 'peer', 'client_once','fail_if_no_peer_cert') or directly the
a1639ad @flop Undocumented :openssl_verify_mode option for smtp_settings
flop authored
340 # constant (OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER,...).
c927aa0 @dhh Updated documentation
dhh authored
341 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
342 # * <tt>sendmail_settings</tt> - Allows you to override options for the <tt>:sendmail</tt> delivery method.
343 # * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
b72cd29 @mikel Fixing spelling
mikel authored
344 # * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt> with <tt>-f sender@address</tt>
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
345 # added automatically before the message is sent.
dc4eec1 @lifo Merge docrails:
lifo authored
346 #
fbe6c3c @edavis10 Adds a :file delivery_method to save email to a file on disk
edavis10 authored
347 # * <tt>file_settings</tt> - Allows you to override options for the <tt>:file</tt> delivery method.
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
348 # * <tt>:location</tt> - The directory into which emails will be written. Defaults to the application
349 # <tt>tmp/mails</tt>.
fbe6c3c @edavis10 Adds a :file delivery_method to save email to a file on disk
edavis10 authored
350 #
dc4eec1 @lifo Merge docrails:
lifo authored
351 # * <tt>raise_delivery_errors</tt> - Whether or not errors should be raised if the email fails to be delivered.
c927aa0 @dhh Updated documentation
dhh authored
352 #
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
353 # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default),
354 # <tt>:sendmail</tt>, <tt>:test</tt>, and <tt>:file</tt>. Or you may provide a custom delivery method
a4726d3 @dmathieu just specify what's to be done, not what's not to be.
dmathieu authored
355 # object e.g. MyOwnDeliveryMethodClass. See the Mail gem documentation on the interface you need to
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
356 # implement for a custom delivery agent.
c927aa0 @dhh Updated documentation
dhh authored
357 #
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
358 # * <tt>perform_deliveries</tt> - Determines whether emails are actually sent from Action Mailer when you
5d0d4d8 @smartinez87 Remove extra white spaces on ActionMailer docs.
smartinez87 authored
359 # call <tt>.deliver</tt> on an mail message or on an Action Mailer method. This is on by default but can
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
360 # be turned off to aid in functional testing.
c927aa0 @dhh Updated documentation
dhh authored
361 #
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
362 # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with
363 # <tt>delivery_method :test</tt>. Most useful for unit and functional testing.
af56c80 @dhh Updated documentation
dhh authored
364 #
4964d3b @josevalim Make ActionMailer::Base inherit from AbstractController::Base
josevalim authored
365 class Base < AbstractController::Base
dbcf01e @mikel Removing quoting.rb, upgrade to 2.1.3.6, changing all utf-8 reference…
mikel authored
366 include DeliveryMethods
f30d73b Add new class delivery method API.
José Valim and Mikel Lindsaar authored
367 abstract!
368
c03c40b @josevalim Expose a _render_partial hook as thhe _render_template one and make u…
josevalim authored
369 include AbstractController::Logger
83f4d86 @dhh Rename the RenderingController module to just plain Rendering
dhh authored
370 include AbstractController::Rendering
a9751a7 @josevalim Refactor ActionMailer layout and remove legacy one.
josevalim authored
371 include AbstractController::Layouts
684c2dc @josevalim Remove ActionMailer helpers and rely on AbstractController one.
josevalim authored
372 include AbstractController::Helpers
48a3985 @josevalim Also include translation in ActionMailer.
josevalim authored
373 include AbstractController::Translation
08baa34 @dhh Extract assets paths and make them available to Action Mailer as well
dhh authored
374 include AbstractController::AssetPaths
4f28c4f @jsl Add ability to define callbacks in ActionMailer using AbstractControl…
jsl authored
375 include AbstractController::Callbacks
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
376
7d86235 @lest get rid of using instance_variable_names method from AS
lest authored
377 self.protected_instance_variables = [:@_action_has_layout]
d42bb68 @josevalim More updates to ivars list.
josevalim authored
378
972efa1 @josevalim Deprecate the old mailer API that was not deprecated yet.
josevalim authored
379 helper ActionMailer::MailHelper
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
380
c927aa0 @dhh Updated documentation
dhh authored
381 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored
382
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
383 class_attribute :default_params
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
384 self.default_params = {
385 :mime_version => "1.0",
dbcf01e @mikel Removing quoting.rb, upgrade to 2.1.3.6, changing all utf-8 reference…
mikel authored
386 :charset => "UTF-8",
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
387 :content_type => "text/plain",
388 :parts_order => [ "text/plain", "text/enriched", "text/html" ]
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
389 }.freeze
e1c1318 Added delivers_from.
José Valim and Mikel Lindsaar authored
390
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
391 class << self
284ca81 @joshk remove AM delegating register_observer and register_interceptor to Ma…
joshk authored
392 # Register one or more Observers which will be notified when mail is delivered.
393 def register_observers(*observers)
394 observers.flatten.compact.each { |observer| register_observer(observer) }
395 end
396
397 # Register one or more Interceptors which will be called before mail is sent.
398 def register_interceptors(*interceptors)
399 interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) }
400 end
401
402 # Register an Observer which will be notified when mail is delivered.
403 # Either a class or a string can be passed in as the Observer. If a string is passed in
404 # it will be <tt>constantize</tt>d.
405 def register_observer(observer)
cb9e501 @joshk AM register_interceptor and register_observer only constantize if the…
joshk authored
406 delivery_observer = (observer.is_a?(String) ? observer.constantize : observer)
284ca81 @joshk remove AM delegating register_observer and register_interceptor to Ma…
joshk authored
407 Mail.register_observer(delivery_observer)
408 end
409
a467983 @vijaydev fix typo
vijaydev authored
410 # Register an Interceptor which will be called before mail is sent.
9a67a11 @siong1987 minor typo in the rdoc about Interceptor.
siong1987 authored
411 # Either a class or a string can be passed in as the Interceptor. If a string is passed in
284ca81 @joshk remove AM delegating register_observer and register_interceptor to Ma…
joshk authored
412 # it will be <tt>constantize</tt>d.
413 def register_interceptor(interceptor)
cb9e501 @joshk AM register_interceptor and register_observer only constantize if the…
joshk authored
414 delivery_interceptor = (interceptor.is_a?(String) ? interceptor.constantize : interceptor)
284ca81 @joshk remove AM delegating register_observer and register_interceptor to Ma…
joshk authored
415 Mail.register_interceptor(delivery_interceptor)
416 end
417
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
418 def mailer_name
c17d200 @lest mailer can be anonymous
lest authored
419 @mailer_name ||= anonymous? ? "anonymous" : name.underscore
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
420 end
0750304 @mikel Migrated over to Mail doing delivery.
mikel authored
421 attr_writer :mailer_name
422 alias :controller_path :mailer_name
81c416b @josevalim More refactoring on ActionMailer::Base.
josevalim authored
423
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
424 def default(value = nil)
425 self.default_params = default_params.merge(value).freeze if value
426 default_params
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
427 end
3009c63 @carlosantoniodasilva Improve docs, changelog and release notes for Action Mailer default_o…
carlosantoniodasilva authored
428 # Allows to set defaults through app configuration:
429 #
430 # config.action_mailer.default_options = { from: "no-reply@example.org" }
edaa2c4 @paneq Introduce config.action_mailer.default_from=
paneq authored
431 alias :default_options= :default
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
432
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
433 # Receives a raw email, parses it into an email object, decodes it,
434 # instantiates a new mailer, and passes the email object to the mailer
98dc582 @lifo Merge docrails.
lifo authored
435 # object's +receive+ method. If you want your mailer to be able to
436 # process incoming messages, you'll need to implement a +receive+
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
437 # method that accepts the raw email string as a parameter:
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
438 #
439 # class MyMailer < ActionMailer::Base
440 # def receive(mail)
441 # ...
442 # end
443 # end
704daad @josevalim Ensure we just send Ruby Stdlib objects in ActionMailer notifications.
josevalim authored
444 def receive(raw_mail)
731d439 @jaggederest Change event namespace ordering to most-significant first [#4504 stat…
jaggederest authored
445 ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload|
704daad @josevalim Ensure we just send Ruby Stdlib objects in ActionMailer notifications.
josevalim authored
446 mail = Mail.new(raw_mail)
447 set_payload_for_mail(payload, mail)
2a6bc12 @josevalim Add subscriber to ActionMailer.
josevalim authored
448 new.receive(mail)
449 end
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
450 end
451
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
452 # Wraps an email delivery inside of Active Support Notifications instrumentation. This
de65c82 @rizwanreza Changes call backs to callbacks.
rizwanreza authored
453 # method is actually called by the <tt>Mail::Message</tt> object itself through a callback
bb2c7b4 @mikel Updating Action Mailer documentation
mikel authored
454 # when you call <tt>:deliver</tt> on the Mail::Message, calling +deliver_mail+ directly
455 # and passing a Mail::Message will do nothing except tell the logger you sent the email.
e4a989e Added delivery_handler method to mail and implemented in ActionMailer…
José Valim and Mikel Lindsaar authored
456 def deliver_mail(mail) #:nodoc:
731d439 @jaggederest Change event namespace ordering to most-significant first [#4504 stat…
jaggederest authored
457 ActiveSupport::Notifications.instrument("deliver.action_mailer") do |payload|
c176397 @sachin87 some modifications in action_mailer/base.rb
sachin87 authored
458 set_payload_for_mail(payload, mail)
ace7497 Got AM working with Mail yield on delivery_handler and updated tests
José Valim and Mikel Lindsaar authored
459 yield # Let Mail do the delivery actions
258ca14 Delegated ActionMailer::Base.deliveries to Mail.deliveries, added cal…
José Valim and Mikel Lindsaar authored
460 end
461 end
462
ad8dc5a @mikel Should use standard ruby signature for respond_to
mikel authored
463 def respond_to?(method, include_private = false) #:nodoc:
99f960a Handle some TODOs and deprecations.
José Valim and Mikel Lindsaar authored
464 super || action_methods.include?(method.to_s)
465 end
466
a11fcd9 @bcardarella Better documentation for ActionMailer.async
bcardarella authored
467 # Will force ActionMailer to push new messages to the queue defined
e2abd8d @frodsan fix ActionMailer::Async docs and update to follow coding conventions
frodsan authored
468 # in the ActionMailer class when set to true.
aee4eec @bcardarella Better documentation for ActionMailer.async
bcardarella authored
469 #
470 # class WelcomeMailer < ActionMailer::Base
471 # self.async = true
e2abd8d @frodsan fix ActionMailer::Async docs and update to follow coding conventions
frodsan authored
472 # end
dc7fd82 @bcardarella Asynchronous ActionMailer
bcardarella authored
473 def async=(truth)
474 if truth
475 require 'action_mailer/async'
812d1e8 @bcardarella Support for custom queues on the mailer
bcardarella authored
476 extend ActionMailer::Async
dc7fd82 @bcardarella Asynchronous ActionMailer
bcardarella authored
477 end
478 end
479
99f960a Handle some TODOs and deprecations.
José Valim and Mikel Lindsaar authored
480 protected
481
704daad @josevalim Ensure we just send Ruby Stdlib objects in ActionMailer notifications.
josevalim authored
482 def set_payload_for_mail(payload, mail) #:nodoc:
e8c9cac @tenderlove self is implied, so remove self
tenderlove authored
483 payload[:mailer] = name
c905fce @josevalim Add message_id to AM payload and don't send bcc and cc if they were n…
josevalim authored
484 payload[:message_id] = mail.message_id
485 payload[:subject] = mail.subject
486 payload[:to] = mail.to
487 payload[:from] = mail.from
488 payload[:bcc] = mail.bcc if mail.bcc.present?
489 payload[:cc] = mail.cc if mail.cc.present?
490 payload[:date] = mail.date
491 payload[:mail] = mail.encoded
704daad @josevalim Ensure we just send Ruby Stdlib objects in ActionMailer notifications.
josevalim authored
492 end
f30d73b Add new class delivery method API.
José Valim and Mikel Lindsaar authored
493
99f960a Handle some TODOs and deprecations.
José Valim and Mikel Lindsaar authored
494 def method_missing(method, *args) #:nodoc:
4d7b2ee @tenderlove dry up method missing
tenderlove authored
495 return super unless respond_to?(method)
496 new(method, *args).message
f30d73b Add new class delivery method API.
José Valim and Mikel Lindsaar authored
497 end
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
498 end
499
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
500 attr_internal :message
501
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
502 # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
503 # will be initialized according to the named method. If not, the mailer will
504 # remain uninitialized (useful when you only need to invoke the "receive"
505 # method, for instance).
506 def initialize(method_name=nil, *args)
507 super()
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
508 @_message = Mail.new
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
509 process(method_name, *args) if method_name
510 end
511
3fc609e @josevalim Fix ActionMailer test broken in 99d54599215c2a8cea7e57f609e8e578043d71b2
josevalim authored
512 def process(*args) #:nodoc:
513 lookup_context.skip_default_locale!
514 super
515 end
516
972efa1 @josevalim Deprecate the old mailer API that was not deprecated yet.
josevalim authored
517 def mailer_name
518 self.class.mailer_name
519 end
520
8cc4159 @mark-rushakoff Use <tt>Foo::Bar</tt> instead of +Foo::Bar+
mark-rushakoff authored
521 # Allows you to pass random and unusual headers to the new <tt>Mail::Message</tt> object
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
522 # which will add them to itself.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
523 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
524 # headers['X-Special-Domain-Specific-Header'] = "SecretValue"
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
525 #
9520166 @mikel Fixed up being able to pass random headers in with headers, or mail. …
mikel authored
526 # You can also pass a hash into headers of header field names and values, which
527 # will then be set on the Mail::Message object:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
528 #
05c4ad9 Tidy up tests and docs.
José Valim and Mikel Lindsaar authored
529 # headers 'X-Special-Domain-Specific-Header' => "SecretValue",
530 # 'In-Reply-To' => incoming.message_id
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
531 #
ff44c33 @mark-rushakoff Fix it's -> its
mark-rushakoff authored
532 # The resulting Mail::Message will have the following in its header:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
533 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
534 # X-Special-Domain-Specific-Header: SecretValue
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
535 def headers(args=nil)
536 if args
9520166 @mikel Fixed up being able to pass random headers in with headers, or mail. …
mikel authored
537 @_message.headers(args)
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
538 else
539 @_message
540 end
541 end
c34cfcc @mikel Created mail method for new API
mikel authored
542
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
543 # Allows you to add attachments to an email, like so:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
544 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
545 # mail.attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
546 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
547 # If you do this, then Mail will take the file name and work out the mime type
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
548 # set the Content-Type, Content-Disposition, Content-Transfer-Encoding and
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
549 # base64 encode the contents of the attachment all for you.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
550 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
551 # You can also specify overrides if you want by passing a hash instead of a string:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
552 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
553 # mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
554 # :content => File.read('/path/to/filename.jpg')}
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
555 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
556 # If you want to use a different encoding than Base64, you can pass an encoding in,
557 # but then it is up to you to pass in the content pre-encoded, and don't expect
558 # Mail to know how to decode this data:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
559 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
560 # file_content = SpecialEncode(File.read('/path/to/filename.jpg'))
561 # mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
562 # :encoding => 'SpecialEncoding',
563 # :content => file_content }
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
564 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
565 # You can also search for specific attachments:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
566 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
567 # # By Filename
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored
568 # mail.attachments['filename.jpg'] # => Mail::Part object or nil
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
569 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
570 # # or by index
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored
571 # mail.attachments[0] # => Mail::Part (first attachment)
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
572 #
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
573 def attachments
574 @_message.attachments
575 end
dcb9253 Add basic template rendering to new DSL.
José Valim and Mikel Lindsaar authored
576
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
577 # The main method that creates the message and renders the email templates. There are
578 # two ways to call this method, with a block, or without a block.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
579 #
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
580 # Both methods accept a headers hash. This hash allows you to specify the most used headers
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
581 # in an email message, these are:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
582 #
058f7fe @mikel Cleaning up Action Mailer spelling with and without the space
mikel authored
583 # * <tt>:subject</tt> - The subject of the message, if this is omitted, Action Mailer will
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
584 # ask the Rails I18n class for a translated <tt>:subject</tt> in the scope of
66880cf @Odaeus Correct ActionMailer subject i18n lookup scope.
Odaeus authored
585 # <tt>[mailer_scope, action_name]</tt> or if this is missing, will translate the
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
586 # humanized version of the <tt>action_name</tt>
587 # * <tt>:to</tt> - Who the message is destined for, can be a string of addresses, or an array
588 # of addresses.
0b05acd @mikel Implementing class level :defaults hash, instead of delivers_from et al
mikel authored
589 # * <tt>:from</tt> - Who the message is from
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
590 # * <tt>:cc</tt> - Who you would like to Carbon-Copy on this email, can be a string of addresses,
591 # or an array of addresses.
592 # * <tt>:bcc</tt> - Who you would like to Blind-Carbon-Copy on this email, can be a string of
593 # addresses, or an array of addresses.
594 # * <tt>:reply_to</tt> - Who to set the Reply-To header of the email to.
595 # * <tt>:date</tt> - The date to say the email was sent on.
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
596 #
597 # You can set default values for any of the above headers (except :date) by using the <tt>default</tt>
0b05acd @mikel Implementing class level :defaults hash, instead of delivers_from et al
mikel authored
598 # class method:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
599 #
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
600 # class Notifier < ActionMailer::Base
452bb1e @jeremy Rename 'defaults' class method to 'default' to reflect that it's a de…
jeremy authored
601 # self.default :from => 'no-reply@test.lindsaar.net',
602 # :bcc => 'email_logger@test.lindsaar.net',
603 # :reply_to => 'bounces@test.lindsaar.net'
0b05acd @mikel Implementing class level :defaults hash, instead of delivers_from et al
mikel authored
604 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
605 #
52c56f9 @wincent docs: note that mail() accepts arbitrary headers
wincent authored
606 # If you need other headers not listed above, you can either pass them in
607 # as part of the headers hash or use the <tt>headers['name'] = value</tt>
608 # method.
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
609 #
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
610 # When a <tt>:return_path</tt> is specified as header, that value will be used as the 'envelope from'
5d0d4d8 @smartinez87 Remove extra white spaces on ActionMailer docs.
smartinez87 authored
611 # address for the Mail message. Setting this is useful when you want delivery notifications
612 # sent to a different address than the one in <tt>:from</tt>. Mail will actually use the
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
613 # <tt>:return_path</tt> in preference to the <tt>:sender</tt> in preference to the <tt>:from</tt>
614 # field for the 'envelope from' value.
615 #
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
616 # If you do not pass a block to the +mail+ method, it will find all templates in the
be35a15 @josevalim Allow to choose the template path and template name used in implicit …
josevalim authored
617 # view paths using by default the mailer name and the method name that it is being
618 # called from, it will then create parts for each of these templates intelligently,
619 # making educated guesses on correct content type and sequence, and return a fully
620 # prepared Mail::Message ready to call <tt>:deliver</tt> on to send.
621 #
622 # For example:
623 #
624 # class Notifier < ActionMailer::Base
625 # default :from => 'no-reply@test.lindsaar.net',
626 #
627 # def welcome
628 # mail(:to => 'mikel@test.lindsaar.net')
629 # end
630 # end
631 #
45b1045 @dmathieu raise an error if no implicit mailer template could be found
dmathieu authored
632 # Will look for all templates at "app/views/notifier" with name "welcome".
633 # If no welcome template exists, it will raise an ActionView::MissingTemplate error.
634 #
635 # However, those can be customized:
be35a15 @josevalim Allow to choose the template path and template name used in implicit …
josevalim authored
636 #
637 # mail(:template_path => 'notifications', :template_name => 'another')
638 #
639 # And now it will look for all templates at "app/views/notifications" with name "another".
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
640 #
641 # If you do pass a block, you can render specific templates of your choice:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
642 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
643 # mail(:to => 'mikel@test.lindsaar.net') do |format|
644 # format.text
645 # format.html
646 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
647 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
648 # You can even render text directly without using a template:
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
649 #
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
650 # mail(:to => 'mikel@test.lindsaar.net') do |format|
651 # format.text { render :text => "Hello Mikel!" }
652 # format.html { render :text => "<h1>Hello Mikel!</h1>" }
653 # end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
654 #
74a5889 Refactor content type setting, added tests to ensure boundary exists …
José Valim and Mikel Lindsaar authored
655 # Which will render a <tt>multipart/alternative</tt> email with <tt>text/plain</tt> and
4a6eba3 Added initial documentation for the new API
José Valim and Mikel Lindsaar authored
656 # <tt>text/html</tt> parts.
6589976 Remove old files, add some information to docs and improve test suite.
José Valim and Mikel Lindsaar authored
657 #
658 # The block syntax also allows you to customize the part headers if desired:
659 #
660 # mail(:to => 'mikel@test.lindsaar.net') do |format|
661 # format.text(:content_transfer_encoding => "base64")
662 # format.html
663 # end
664 #
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
665 def mail(headers={}, &block)
666 m = @_message
258ca14 Delegated ActionMailer::Base.deliveries to Mail.deliveries, added cal…
José Valim and Mikel Lindsaar authored
667
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
668 # At the beginning, do not consider class default for parts order neither content_type
669 content_type = headers[:content_type]
670 parts_order = headers[:parts_order]
1cd5592 First work on implicit multipart.
José Valim and Mikel Lindsaar authored
671
08b07b6 @mikel Adding ability to pass proc's to the ActionMailer class default method
mikel authored
672 # Call all the procs (if any)
c176397 @sachin87 some modifications in action_mailer/base.rb
sachin87 authored
673 class_default = self.class.default
674 default_values = class_default.merge(class_default) do |k,v|
3fbc042 @lest Use instance_eval instead of Proc#bind
lest authored
675 v.respond_to?(:to_proc) ? instance_eval(&v) : v
08b07b6 @mikel Adding ability to pass proc's to the ActionMailer class default method
mikel authored
676 end
458f571 @wycats Remove the need for a special action_mailer.url_for initializer that …
wycats authored
677
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
678 # Handle defaults
08b07b6 @mikel Adding ability to pass proc's to the ActionMailer class default method
mikel authored
679 headers = headers.reverse_merge(default_values)
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
680 headers[:subject] ||= default_i18n_subject
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
681
682 # Apply charset at the beginning so all fields are properly quoted
683 m.charset = charset = headers[:charset]
684
685 # Set configure delivery behavior
8fc8763 @asanghi Allow delivery method options to be set per mail instance
asanghi authored
686 wrap_delivery_behavior!(headers.delete(:delivery_method),headers.delete(:delivery_method_options))
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
687
688 # Assign all headers except parts_order, content_type and body
b4fd5e6 @josevalim template_name and template_path should not be added to mail headers.
josevalim authored
689 assignable = headers.except(:parts_order, :content_type, :body, :template_name, :template_path)
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
690 assignable.each { |k, v| m[k] = v }
951397b Get implicit multipart and attachments working together.
José Valim and Mikel Lindsaar authored
691
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
692 # Render the templates and blocks
39a1b06 Deprecate old defaults API.
José Valim and Mikel Lindsaar authored
693 responses, explicit_order = collect_responses_and_parts_order(headers, &block)
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
694 create_parts_from_responses(m, responses)
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
695
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
696 # Setup content type, reapply charset and handle parts order
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
697 m.content_type = set_content_type(m, content_type, headers[:content_type])
951397b Get implicit multipart and attachments working together.
José Valim and Mikel Lindsaar authored
698 m.charset = charset
699
e7e4ed4 Set sort order for explicit parts from the collector's template sequence
José Valim and Mikel Lindsaar authored
700 if m.multipart?
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
701 parts_order ||= explicit_order || headers[:parts_order]
702 m.body.set_sort_order(parts_order)
951397b Get implicit multipart and attachments working together.
José Valim and Mikel Lindsaar authored
703 m.body.sort_parts!
704 end
bb9d71f Move class methods to deprecated stuff.
José Valim and Mikel Lindsaar authored
705
c34cfcc @mikel Created mail method for new API
mikel authored
706 m
707 end
708
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
709 protected
710
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
711 def set_content_type(m, user_content_type, class_default)
74a5889 Refactor content type setting, added tests to ensure boundary exists …
José Valim and Mikel Lindsaar authored
712 params = m.content_type_parameters || {}
713 case
714 when user_content_type.present?
715 user_content_type
716 when m.has_attachments?
311d99e @mikel Adding inline attachment support to ActionMailer
mikel authored
717 if m.attachments.detect { |a| a.inline? }
718 ["multipart", "related", params]
719 else
720 ["multipart", "mixed", params]
721 end
74a5889 Refactor content type setting, added tests to ensure boundary exists …
José Valim and Mikel Lindsaar authored
722 when m.multipart?
723 ["multipart", "alternative", params]
724 else
b6b3db6 @mikel Fixed bug on HTML only emails getting set to text/plain
mikel authored
725 m.content_type || class_default
74a5889 Refactor content type setting, added tests to ensure boundary exists …
José Valim and Mikel Lindsaar authored
726 end
727 end
728
11b67b3 @indrekj fix default_i18n_subject comment in ActionMailer::Base
indrekj authored
729 # Translates the +subject+ using Rails I18n class under <tt>[mailer_scope, action_name]</tt> scope.
082e299 @smartinez87 docs for ActionMailer::Base.default_i18n_subject
smartinez87 authored
730 # If it does not find a translation for the +subject+ under the specified scope it will default to a
731 # humanized version of the <tt>action_name</tt>.
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
732 def default_i18n_subject #:nodoc:
2d8396f @jurriaan Updated/changed useless tr/gsubs
jurriaan authored
733 mailer_scope = self.class.mailer_name.tr('/', '.')
30eaecb @josevalim subject is automatically looked up on I18n using mailer_name and acti…
josevalim authored
734 I18n.t(:subject, :scope => [mailer_scope, action_name], :default => action_name.humanize)
5c3ef8c Refactor subject with i18n.
José Valim and Mikel Lindsaar authored
735 end
736
39a1b06 Deprecate old defaults API.
José Valim and Mikel Lindsaar authored
737 def collect_responses_and_parts_order(headers) #:nodoc:
738 responses, parts_order = [], nil
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
739
740 if block_given?
a09e992 @josevalim Ensure details are frozen after @details_keys lookup. The implementat…
josevalim authored
741 collector = ActionMailer::Collector.new(lookup_context) { render(action_name) }
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
742 yield(collector)
39a1b06 Deprecate old defaults API.
José Valim and Mikel Lindsaar authored
743 parts_order = collector.responses.map { |r| r[:content_type] }
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
744 responses = collector.responses
745 elsif headers[:body]
746 responses << {
be35a15 @josevalim Allow to choose the template path and template name used in implicit …
josevalim authored
747 :body => headers.delete(:body),
452bb1e @jeremy Rename 'defaults' class method to 'default' to reflect that it's a de…
jeremy authored
748 :content_type => self.class.default[:content_type] || "text/plain"
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
749 }
750 else
be35a15 @josevalim Allow to choose the template path and template name used in implicit …
josevalim authored
751 templates_path = headers.delete(:template_path) || self.class.mailer_name
752 templates_name = headers.delete(:template_name) || action_name
753
754 each_template(templates_path, templates_name) do |template|
f28d856 @josevalim Improve performance of the rendering stack by freezing formats as a s…
josevalim authored
755 self.formats = template.formats
756
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
757 responses << {
2a12686 @josevalim Allow anything that responds to render to be given as :template and u…
josevalim authored
758 :body => render(:template => template),
167f250 @kennyj Fixes warnings when executing rake test in ActionMailer. Related to 5…
kennyj authored
759 :content_type => template.type.to_s
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
760 }
761 end
762 end
763
39a1b06 Deprecate old defaults API.
José Valim and Mikel Lindsaar authored
764 [responses, parts_order]
7409b73 Some refactoring.
José Valim and Mikel Lindsaar authored
765 end
766
be35a15 @josevalim Allow to choose the template path and template name used in implicit …
josevalim authored
767 def each_template(paths, name, &block) #:nodoc:
3438f6c @rafaelfranca Array.wrap is no longer needed here
rafaelfranca authored
768 templates = lookup_context.find_all(name, Array(paths))
45b1045 @dmathieu raise an error if no implicit mailer template could be found
dmathieu authored
769 if templates.empty?
770 raise ActionView::MissingTemplate.new([paths], name, [paths], false, 'mailer')
771 else
772 templates.uniq { |t| t.formats }.each(&block)
773 end
99f960a Handle some TODOs and deprecations.
José Valim and Mikel Lindsaar authored
774 end
775
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
776 def create_parts_from_responses(m, responses) #:nodoc:
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
777 if responses.size == 1 && !m.has_attachments?
9dd65c3 Make defaults accept a hash.
José Valim and Mikel Lindsaar authored
778 responses[0].each { |k,v| m[k] = v }
74a5889 Refactor content type setting, added tests to ensure boundary exists …
José Valim and Mikel Lindsaar authored
779 elsif responses.size > 1 && m.has_attachments?
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
780 container = Mail::Part.new
74a5889 Refactor content type setting, added tests to ensure boundary exists …
José Valim and Mikel Lindsaar authored
781 container.content_type = "multipart/alternative"
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
782 responses.each { |r| insert_part(container, r, m.charset) }
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
783 m.add_part(container)
784 else
b5f9a9f @josevalim Move set_fields! to the old API module.
josevalim authored
785 responses.each { |r| insert_part(m, r, m.charset) }
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
786 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
787 end
788
6ba9446 Make implicit and explicit templates pass through the same part creat…
José Valim and Mikel Lindsaar authored
789 def insert_part(container, response, charset) #:nodoc:
790 response[:charset] ||= charset
791 part = Mail::Part.new(response)
792 container.add_part(part)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
793 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
794
ef5dada @nex3 Only run load hooks once a file has been fully loaded.
nex3 authored
795 ActiveSupport.run_load_hooks(:action_mailer, self)
db045db @dhh Initial
dhh authored
796 end
edaa2c4 @paneq Introduce config.action_mailer.default_from=
paneq authored
797 end
Something went wrong with that request. Please try again.