Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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