Skip to content
Newer
Older
100644 729 lines (640 sloc) 28.2 KB
c4590d5 @dhh Fixed docs
dhh authored
1 module ActionMailer #:nodoc:
98dc582 @lifo Merge docrails.
lifo authored
2 # Action Mailer allows you to send email from your application using a mailer model and views.
db045db @dhh Initial
dhh authored
3 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
4 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
5 # = Mailer Models
20bd4bd @dhh Updated docs and otherwise
dhh authored
6 #
98dc582 @lifo Merge docrails.
lifo authored
7 # 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
8 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
9 # $ script/generate mailer Notifier
10 #
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
11 # The generated model inherits from ActionMailer::Base. Emails are defined by creating methods within the model which are then
12 # used to set variables to be used in the mail template, to change options on the mail, or
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
13 # to add attachments.
14 #
15 # Examples:
16 #
17 # class Notifier < ActionMailer::Base
18 # def signup_notification(recipient)
19 # recipients recipient.email_address_with_name
c3f53f4 @lifo Merge docrails
lifo authored
20 # bcc ["bcc@example.com", "Order Watcher <watcher@example.com>"]
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
21 # from "system@example.com"
22 # subject "New account information"
20bd4bd @dhh Updated docs and otherwise
dhh authored
23 # body :account => recipient
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
24 # end
25 # end
26 #
27 # Mailer methods have the following configuration methods available.
28 #
29 # * <tt>recipients</tt> - Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the <tt>To:</tt> header.
30 # * <tt>subject</tt> - The subject of your email. Sets the <tt>Subject:</tt> header.
31 # * <tt>from</tt> - Who the email you are sending is from. Sets the <tt>From:</tt> header.
32 # * <tt>cc</tt> - Takes one or more email addresses. These addresses will receive a carbon copy of your email. Sets the <tt>Cc:</tt> header.
cf6299d Add ActionMailer#reply_to. [#245 state:resolved]
Adam authored
33 # * <tt>bcc</tt> - Takes one or more email addresses. These addresses will receive a blind carbon copy of your email. Sets the <tt>Bcc:</tt> header.
34 # * <tt>reply_to</tt> - Takes one or more email addresses. These addresses will be listed as the default recipients when replying to your email. Sets the <tt>Reply-To:</tt> header.
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
35 # * <tt>sent_on</tt> - The date on which the message was sent. If not set, the header wil be set by the delivery agent.
36 # * <tt>content_type</tt> - Specify the content type of the message. Defaults to <tt>text/plain</tt>.
37 # * <tt>headers</tt> - Specify additional headers to be set for the message, e.g. <tt>headers 'X-Mail-Count' => 107370</tt>.
38 #
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
39 # When a <tt>headers 'return-path'</tt> is specified, that value will be used as the 'envelope from'
40 # address. Setting this is useful when you want delivery notifications sent to a different address than
41 # the one in <tt>from</tt>.
42 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
43 # The <tt>body</tt> method has special behavior. It takes a hash which generates an instance variable
44 # named after each key in the hash containing the value that that key points to.
45 #
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
46 # So, for example, <tt>body :account => recipient</tt> would result
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
47 # in an instance variable <tt>@account</tt> with the value of <tt>recipient</tt> being accessible in the
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
48 # view.
49 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
50 #
51 # = Mailer views
52 #
98dc582 @lifo Merge docrails.
lifo authored
53 # Like Action Controller, each mailer class has a corresponding view directory
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
54 # in which each method of the class looks for a template with its name.
21187c0 @dhh Apply the rest of Chads patch
dhh authored
55 # To define a template to be used with a mailing, create an <tt>.erb</tt> file with the same name as the method
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
56 # in your mailer model. For example, in the mailer defined above, the template at
21187c0 @dhh Apply the rest of Chads patch
dhh authored
57 # <tt>app/views/notifier/signup_notification.erb</tt> would be used to generate the email.
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
58 #
59 # Variables defined in the model are accessible as instance variables in the view.
60 #
61 # Emails by default are sent in plain text, so a sample view for our model example might look like this:
62 #
63 # Hi <%= @account.name %>,
64 # Thanks for joining our service! Please check back often.
65 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
66 # You can even use Action Pack helpers in these views. For example:
67 #
68 # You got a new note!
69 # <%= truncate(note.body, 25) %>
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
70 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
71 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
72 # = Generating URLs
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
73 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
74 # URLs can be generated in mailer views using <tt>url_for</tt> or named routes.
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
75 # Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request,
76 # so you'll need to provide all of the details needed to generate a URL.
20bd4bd @dhh Updated docs and otherwise
dhh authored
77 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
78 # 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
79 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
80 # <%= url_for(:host => "example.com", :controller => "welcome", :action => "greeting") %>
20bd4bd @dhh Updated docs and otherwise
dhh authored
81 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
82 # 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
83 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
84 # <%= users_url(:host => "example.com") %>
85 #
86 # You will want to avoid using the <tt>name_of_route_path</tt> form of named routes because it doesn't make sense to
87 # generate relative URLs in email messages.
88 #
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
89 # It is also possible to set a default host that will be used in all mailers by setting the <tt>:host</tt> option in
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
90 # the <tt>ActionMailer::Base.default_url_options</tt> hash as follows:
91 #
92 # ActionMailer::Base.default_url_options[:host] = "example.com"
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
93 #
dc4eec1 @lifo Merge docrails:
lifo authored
94 # This can also be set as a configuration option in <tt>config/environment.rb</tt>:
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
95 #
96 # config.action_mailer.default_url_options = { :host => "example.com" }
20bd4bd @dhh Updated docs and otherwise
dhh authored
97 #
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
98 # If you do decide to set a default <tt>:host</tt> for your mailers you will want to use the
99 # <tt>:only_path => false</tt> option when using <tt>url_for</tt>. This will ensure that absolute URLs are generated because
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
100 # the <tt>url_for</tt> view helper will, by default, generate relative URLs when a <tt>:host</tt> option isn't
4809dcc * Remove default_url_options from mailer generator
Cody Fauser authored
101 # explicitly provided.
20bd4bd @dhh Updated docs and otherwise
dhh authored
102 #
103 # = Sending mail
104 #
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
105 # 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
106 # for delivery later:
107 #
108 # Notifier.deliver_signup_notification(david) # sends the email
109 # mail = Notifier.create_signup_notification(david) # => a tmail object
110 # Notifier.deliver(mail)
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
111 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
112 # You never instantiate your mailer class. Rather, your delivery instance
113 # methods are automatically wrapped in class methods that start with the word
114 # <tt>deliver_</tt> followed by the name of the mailer method that you would
115 # like to deliver. The <tt>signup_notification</tt> method defined above is
116 # delivered by invoking <tt>Notifier.deliver_signup_notification</tt>.
117 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
118 #
119 # = HTML email
120 #
21187c0 @dhh Apply the rest of Chads patch
dhh authored
121 # To send mail as HTML, make sure your view (the <tt>.erb</tt> file) generates HTML and
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
122 # set the content type to html.
123 #
124 # class MyMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
125 # def signup_notification(recipient)
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
126 # recipients recipient.email_address_with_name
127 # subject "New account information"
128 # from "system@example.com"
129 # body :account => recipient
130 # content_type "text/html"
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
131 # end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
132 # end
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
133 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
134 #
135 # = Multipart email
136 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
137 # You can explicitly specify multipart messages:
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
138 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
139 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
140 # def signup_notification(recipient)
141 # recipients recipient.email_address_with_name
142 # subject "New account information"
143 # from "system@example.com"
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
144 # content_type "multipart/alternative"
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
145 #
146 # part :content_type => "text/html",
147 # :body => render_message("signup-as-html", :account => recipient)
148 #
149 # part "text/plain" do |p|
150 # p.body = render_message("signup-as-plain", :account => recipient)
151 # p.transfer_encoding = "base64"
152 # end
153 # end
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
154 # end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
155 #
98dc582 @lifo Merge docrails.
lifo authored
156 # Multipart messages can also be used implicitly because Action Mailer will automatically
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
157 # detect and use multipart templates, where each template is named after the name of the action, followed
158 # by the content type. Each such detected template will be added as separate part to the message.
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
159 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
160 # For example, if the following templates existed:
21187c0 @dhh Apply the rest of Chads patch
dhh authored
161 # * signup_notification.text.plain.erb
162 # * signup_notification.text.html.erb
163 # * signup_notification.text.xml.builder
164 # * signup_notification.text.x-yaml.erb
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
165 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
166 # Each would be rendered and added as a separate part to the message,
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
167 # with the corresponding content type. The content type for the entire
168 # message is automatically set to <tt>multipart/alternative</tt>, which indicates
169 # that the email contains multiple different representations of the same email
170 # body. The same body hash is passed to each template.
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
171 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
172 # Implicit template rendering is not performed if any attachments or parts have been added to the email.
173 # This means that you'll have to manually add each part to the email and set the content type of the email
174 # to <tt>multipart/alternative</tt>.
20bd4bd @dhh Updated docs and otherwise
dhh authored
175 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
176 # = Attachments
20bd4bd @dhh Updated docs and otherwise
dhh authored
177 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
178 # Attachments can be added by using the +attachment+ method.
179 #
180 # Example:
181 #
182 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
183 # # attachments
184 # def signup_notification(recipient)
185 # recipients recipient.email_address_with_name
186 # subject "New account information"
187 # from "system@example.com"
188 #
189 # attachment :content_type => "image/jpeg",
190 # :body => File.read("an-image.jpg")
191 #
192 # attachment "application/pdf" do |a|
193 # a.body = generate_your_pdf_here()
194 # end
db045db @dhh Initial
dhh authored
195 # end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
196 # end
c927aa0 @dhh Updated documentation
dhh authored
197 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
198 #
c927aa0 @dhh Updated documentation
dhh authored
199 # = Configuration options
200 #
201 # These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
202 #
64092de @fxn Improve documentation coverage and markup
fxn authored
203 # * <tt>template_root</tt> - Determines the base from which template references will be made.
c927aa0 @dhh Updated documentation
dhh authored
204 #
205 # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
206 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
207 #
64092de @fxn Improve documentation coverage and markup
fxn authored
208 # * <tt>smtp_settings</tt> - Allows detailed configuration for <tt>:smtp</tt> delivery method:
dc4eec1 @lifo Merge docrails:
lifo authored
209 # * <tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
210 # * <tt>:port</tt> - On the off chance that your mail server doesn't run on port 25, you can change it.
211 # * <tt>:domain</tt> - If you need to specify a HELO domain, you can do it here.
212 # * <tt>:user_name</tt> - If your mail server requires authentication, set the username in this setting.
213 # * <tt>:password</tt> - If your mail server requires authentication, set the password in this setting.
b2504f8 @josh Tidy up ActionMailer rendering logic to take advantage of view path c…
josh authored
214 # * <tt>:authentication</tt> - If your mail server requires authentication, you need to specify the authentication type here.
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
215 # This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
216 # * <tt>:enable_starttls_auto</tt> - When set to true, detects if STARTTLS is enabled in your SMTP server and starts to use it.
217 # It works only on Ruby >= 1.8.7 and Ruby >= 1.9. Default is true.
c927aa0 @dhh Updated documentation
dhh authored
218 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
219 # * <tt>sendmail_settings</tt> - Allows you to override options for the <tt>:sendmail</tt> delivery method.
220 # * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
221 # * <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
dc4eec1 @lifo Merge docrails:
lifo authored
222 #
223 # * <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
224 #
64092de @fxn Improve documentation coverage and markup
fxn authored
225 # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>.
c927aa0 @dhh Updated documentation
dhh authored
226 #
dc4eec1 @lifo Merge docrails:
lifo authored
227 # * <tt>perform_deliveries</tt> - Determines whether <tt>deliver_*</tt> methods are actually carried out. By default they are,
c927aa0 @dhh Updated documentation
dhh authored
228 # but this can be turned off to help functional testing.
229 #
64092de @fxn Improve documentation coverage and markup
fxn authored
230 # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with <tt>delivery_method :test</tt>. Most useful
c927aa0 @dhh Updated documentation
dhh authored
231 # for unit and functional testing.
af56c80 @dhh Updated documentation
dhh authored
232 #
b2504f8 @josh Tidy up ActionMailer rendering logic to take advantage of view path c…
josh authored
233 # * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
234 # pick a different charset from inside a method with +charset+.
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
235 #
475bd74 @dhh Fix docs (closes #2598)
dhh authored
236 # * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
b2504f8 @josh Tidy up ActionMailer rendering logic to take advantage of view path c…
josh authored
237 # can also pick a different content type from inside a method with +content_type+.
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
238 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
239 # * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to <tt>1.0</tt>. You
240 # can also pick a different value from inside a method with +mime_version+.
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
241 #
475bd74 @dhh Fix docs (closes #2598)
dhh authored
242 # * <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
243 # which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
244 # <tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
245 # and appear last in the mime encoded message. You can also pick a different order from inside a method with
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
246 # +implicit_parts_order+.
db045db @dhh Initial
dhh authored
247 class Base
e201fc7 @josh use autoload instead of explicit requires for ActionMailer
josh authored
248 include AdvAttrAccessor, PartContainer, Quoting, Utils
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
249 if Object.const_defined?(:ActionController)
250 include ActionController::UrlWriter
251 include ActionController::Layout
252 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
253
c927aa0 @dhh Updated documentation
dhh authored
254 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored
255
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
256 class_inheritable_accessor :view_paths
441e4e2 @svenfuchs load plugin view_paths to action_mailer view_paths and make action_ma…
svenfuchs authored
257 self.view_paths = []
258
db045db @dhh Initial
dhh authored
259 cattr_accessor :logger
260
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
261 @@smtp_settings = {
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
262 :address => "localhost",
263 :port => 25,
264 :domain => 'localhost.localdomain',
265 :user_name => nil,
266 :password => nil,
267 :authentication => nil,
268 :enable_starttls_auto => true,
db045db @dhh Initial
dhh authored
269 }
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to all…
NZKoz authored
270 cattr_accessor :smtp_settings
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
271
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to all…
NZKoz authored
272 @@sendmail_settings = {
273 :location => '/usr/sbin/sendmail',
274 :arguments => '-i -t'
275 }
276 cattr_accessor :sendmail_settings
db045db @dhh Initial
dhh authored
277
278 @@raise_delivery_errors = true
279 cattr_accessor :raise_delivery_errors
280
57cde63 @dhh Allow ActionMailer subclasses to individually set their delivery meth…
dhh authored
281 superclass_delegating_accessor :delivery_method
282 self.delivery_method = :smtp
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
283
db045db @dhh Initial
dhh authored
284 @@perform_deliveries = true
285 cattr_accessor :perform_deliveries
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
286
db045db @dhh Initial
dhh authored
287 @@deliveries = []
288 cattr_accessor :deliveries
289
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
290 @@default_charset = "utf-8"
291 cattr_accessor :default_charset
292
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
293 @@default_content_type = "text/plain"
294 cattr_accessor :default_content_type
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
295
45b5555 @jamis Make mime version default to 1.0. closes #2323
jamis authored
296 @@default_mime_version = "1.0"
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
297 cattr_accessor :default_mime_version
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
298
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
299 @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
300 cattr_accessor :default_implicit_parts_order
301
28bf2fa @josh Protect body ivar from being clobbered by the mailer template assigns
josh authored
302 cattr_reader :protected_instance_variables
303 @@protected_instance_variables = %w(@body)
304
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
305 # Specify the BCC addresses for the message
306 adv_attr_accessor :bcc
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
307
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
308 # Define the body of the message. This is either a Hash (in which case it
309 # specifies the variables to pass to the template when it is rendered),
310 # or a string, in which case it specifies the actual text of the message.
311 adv_attr_accessor :body
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
312
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
313 # Specify the CC addresses for the message.
314 adv_attr_accessor :cc
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
315
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
316 # Specify the charset to use for the message. This defaults to the
317 # +default_charset+ specified for ActionMailer::Base.
318 adv_attr_accessor :charset
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
319
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
320 # Specify the content type for the message. This defaults to <tt>text/plain</tt>
321 # in most cases, but can be automatically set in some situations.
322 adv_attr_accessor :content_type
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
323
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
324 # Specify the from address for the message.
325 adv_attr_accessor :from
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
326
cf6299d Add ActionMailer#reply_to. [#245 state:resolved]
Adam authored
327 # Specify the address (if different than the "from" address) to direct
328 # replies to this message.
329 adv_attr_accessor :reply_to
330
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
331 # Specify additional headers to be added to the message.
332 adv_attr_accessor :headers
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
333
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
334 # Specify the order in which parts should be sorted, based on content-type.
335 # This defaults to the value for the +default_implicit_parts_order+.
336 adv_attr_accessor :implicit_parts_order
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
337
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
338 # Defaults to "1.0", but may be explicitly given if needed.
339 adv_attr_accessor :mime_version
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
340
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
341 # The recipient addresses for the message, either as a string (for a single
342 # address) or an array (for multiple addresses).
343 adv_attr_accessor :recipients
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
344
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
345 # The date on which the message was sent. If not set (the default), the
346 # header will be set by the delivery agent.
347 adv_attr_accessor :sent_on
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
348
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
349 # Specify the subject of the message.
350 adv_attr_accessor :subject
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
351
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
352 # Specify the template name to use for current message. This is the "base"
353 # template name, without the extension or directory, and may be used to
354 # have multiple mailer methods share the same template.
355 adv_attr_accessor :template
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
356
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
357 # Override the mailer name, which defaults to an inflected version of the
358 # mailer's class name. If you want to use a template in a non-standard
359 # location, you can use this to specify that location.
360 def mailer_name(value = nil)
361 if value
362 self.mailer_name = value
363 else
364 self.class.mailer_name
365 end
366 end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
367
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
368 def mailer_name=(value)
369 self.class.mailer_name = value
370 end
371
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
372 # The mail object instance referenced by this mailer.
373 attr_reader :mail
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
374 attr_reader :template_name, :default_template_name, :action_name
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
375
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
376 def controller_path
377 self.class.controller_path
378 end
379
380 def formats
381 @template.formats
382 end
383
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
384 class << self
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
385 attr_writer :mailer_name
386
387 def mailer_name
388 @mailer_name ||= name.underscore
389 end
390
391 # for ActionView compatibility
392 alias_method :controller_name, :mailer_name
393 alias_method :controller_path, :mailer_name
394
3cf773b @floehopper ActionMailer should respond_to? to methods handled by method_missing [#…
floehopper authored
395 def respond_to?(method_symbol, include_private = false) #:nodoc:
396 matches_dynamic_method?(method_symbol) || super
397 end
398
399 def method_missing(method_symbol, *parameters) #:nodoc:
a62e9e9 @mocoso Fix for ActionMailer::Base.method_missing so that it raises NoMethodE…
mocoso authored
400 if match = matches_dynamic_method?(method_symbol)
401 case match[1]
402 when 'create' then new(match[2], *parameters).mail
403 when 'deliver' then new(match[2], *parameters).deliver!
404 when 'new' then nil
c65075f @mocoso Fixed method_missing for ActionMailer so it no longer matches methods…
mocoso authored
405 else super
a62e9e9 @mocoso Fix for ActionMailer::Base.method_missing so that it raises NoMethodE…
mocoso authored
406 end
407 else
408 super
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
409 end
410 end
411
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
412 # Receives a raw email, parses it into an email object, decodes it,
413 # instantiates a new mailer, and passes the email object to the mailer
98dc582 @lifo Merge docrails.
lifo authored
414 # object's +receive+ method. If you want your mailer to be able to
415 # process incoming messages, you'll need to implement a +receive+
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
416 # method that accepts the email object as a parameter:
417 #
418 # class MyMailer < ActionMailer::Base
419 # def receive(mail)
420 # ...
421 # end
422 # end
423 def receive(raw_email)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
424 logger.info "Received mail:\n #{raw_email}" unless logger.nil?
425 mail = TMail::Mail.parse(raw_email)
426 mail.base64_decode
427 new.receive(mail)
428 end
429
430 # Deliver the given mail object directly. This can be used to deliver
431 # a preconstructed mail object, like:
432 #
433 # email = MyMailer.create_some_mail(parameters)
434 # email.set_some_obscure_header "frobnicate"
435 # MyMailer.deliver(email)
436 def deliver(mail)
437 new.deliver!(mail)
438 end
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
439
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
440 def template_root
441 self.view_paths && self.view_paths.first
442 end
443
61c90a4 @NZKoz Reapply the TemplateFinder first applied in [8669] then reverted in […
NZKoz authored
444 def template_root=(root)
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
445 self.view_paths = ActionView::Base.process_view_paths(root)
61c90a4 @NZKoz Reapply the TemplateFinder first applied in [8669] then reverted in […
NZKoz authored
446 end
3cf773b @floehopper ActionMailer should respond_to? to methods handled by method_missing [#…
floehopper authored
447
448 private
449 def matches_dynamic_method?(method_name) #:nodoc:
450 method_name = method_name.to_s
c65075f @mocoso Fixed method_missing for ActionMailer so it no longer matches methods…
mocoso authored
451 /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name)
3cf773b @floehopper ActionMailer should respond_to? to methods handled by method_missing [#…
floehopper authored
452 end
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
453 end
454
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
455 # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
456 # will be initialized according to the named method. If not, the mailer will
457 # remain uninitialized (useful when you only need to invoke the "receive"
458 # method, for instance).
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
459 def initialize(method_name=nil, *parameters) #:nodoc:
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
460 create!(method_name, *parameters) if method_name
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
461 end
462
463 # Initialize the mailer via the given +method_name+. The body will be
464 # rendered and a new TMail::Mail object created.
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
465 def create!(method_name, *parameters) #:nodoc:
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
466 initialize_defaults(method_name)
c3ff04b @jeremy Allow mailer actions named send by using __send__ internally. Closes #…
jeremy authored
467 __send__(method_name, *parameters)
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
468
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
469 # If an explicit, textual body has not been set, we check assumptions.
470 unless String === @body
471 # First, we look to see if there are any likely templates that match,
472 # which include the content-type in their file name (i.e.,
21187c0 @dhh Apply the rest of Chads patch
dhh authored
473 # "the_template_file.text.html.erb", etc.). Only do this if parts
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
474 # have not already been specified manually.
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
475 # if @parts.empty?
476 template_root.find_all_by_parts(@template, {}, template_path).each do |template|
b2504f8 @josh Tidy up ActionMailer rendering logic to take advantage of view path c…
josh authored
477 @parts << Part.new(
ab83db9 Fixes ActionMailer to work with the ActionView refactoring
Yehuda Katz + Carl Lerche authored
478 :content_type => template.mime_type ? template.mime_type.to_s : "text/plain",
b2504f8 @josh Tidy up ActionMailer rendering logic to take advantage of view path c…
josh authored
479 :disposition => "inline",
480 :charset => charset,
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
481 :body => render_template(template, @body)
b2504f8 @josh Tidy up ActionMailer rendering logic to take advantage of view path c…
josh authored
482 )
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
483 end
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
484
485 if @parts.size > 1
1dff106 @sdague Allow custom type for multipart emails [#1691 state:resolved]
sdague authored
486 @content_type = "multipart/alternative" if @content_type !~ /^multipart/
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
487 @parts = sort_parts(@parts, @implicit_parts_order)
488 end
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
489 # end
490
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
491 # Then, if there were such templates, we check to see if we ought to
492 # also render a "normal" template (without the content type). If a
493 # normal template exists (or if there were no implicit parts) we render
494 # it.
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
495 # ====
496 # TODO: Revisit this
497 # template_exists = @parts.empty?
498 # template_exists ||= template_root.find_by_parts("#{mailer_name}/#{@template}")
499 # @body = render_message(@template, @body) if template_exists
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
500
501 # Finally, if there are other message parts and a textual body exists,
502 # we shift it onto the front of the parts and set the body to nil (so
503 # that create_mail doesn't try to render it in addition to the parts).
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
504 # ====
505 # TODO: Revisit this
506 # if !@parts.empty? && String === @body
507 # @parts.unshift Part.new(:charset => charset, :body => @body)
508 # @body = nil
509 # end
db045db @dhh Initial
dhh authored
510 end
511
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
512 # If this is a multipart e-mail add the mime_version if it is not
513 # already set.
514 @mime_version ||= "1.0" if !@parts.empty?
515
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
516 # build the mail object itself
517 @mail = create_mail
518 end
db045db @dhh Initial
dhh authored
519
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
520 # Delivers a TMail::Mail object. By default, it delivers the cached mail
98dc582 @lifo Merge docrails.
lifo authored
521 # object (from the <tt>create!</tt> method). If no cached mail object exists, and
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
522 # no alternate has been given as the parameter, this will fail.
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
523 def deliver!(mail = @mail)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
524 raise "no mail object available for delivery!" unless mail
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
525
fbd3eb7 @jeremy Less verbose mail logging: just recipients for :info log level; the w…
jeremy authored
526 unless logger.nil?
be36455 @jeremy Ruby 1.9 compat: use Array() instead of #to_a.
jeremy authored
527 logger.info "Sent mail to #{Array(recipients).join(', ')}"
fbd3eb7 @jeremy Less verbose mail logging: just recipients for :info log level; the w…
jeremy authored
528 logger.debug "\n#{mail.encoded}"
529 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
530
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
531 begin
c3ff04b @jeremy Allow mailer actions named send by using __send__ internally. Closes #…
jeremy authored
532 __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
12ff554 @jeremy Tighten rescue clauses. Closes #5985.
jeremy authored
533 rescue Exception => e # Net::SMTP errors or sendmail pipe errors
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
534 raise e if raise_delivery_errors
535 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
536
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
537 return mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
538 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
539
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
540 private
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
541 # Set up the default values for the various instance variables of this
542 # mailer. Subclasses may override this method to provide different
543 # defaults.
544 def initialize_defaults(method_name)
95e8740 @dhh Allow Mailers to have custom initialize methods that set default inst…
dhh authored
545 @charset ||= @@default_charset.dup
546 @content_type ||= @@default_content_type.dup
547 @implicit_parts_order ||= @@default_implicit_parts_order.dup
548 @template ||= method_name
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
549 @default_template_name = @action_name = @template
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
550 @mailer_name ||= self.class.name.underscore
95e8740 @dhh Allow Mailers to have custom initialize methods that set default inst…
dhh authored
551 @parts ||= []
552 @headers ||= {}
553 @body ||= {}
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
554 @mime_version = @@default_mime_version.dup if @@default_mime_version
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
555 end
556
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
557 def render_template(template, body)
ab83db9 Fixes ActionMailer to work with the ActionView refactoring
Yehuda Katz + Carl Lerche authored
558 if template.respond_to?(:mime_type)
559 @current_template_content_type = template.mime_type && template.mime_type.to_sym.to_s
1d4554d ActionMailer should respect content type when choosing layouts
Colin Curtin authored
560 end
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
561
562 @template = initialize_template_class(body)
ab83db9 Fixes ActionMailer to work with the ActionView refactoring
Yehuda Katz + Carl Lerche authored
563 layout = _pick_layout(layout, true) unless
564 ActionController::Base.exempt_from_layout.include?(template.handler)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
565 @template._render_template_with_layout(template, layout, {})
566 ensure
567 @current_template_content_type = nil
568 end
569
570 def render_message(method_name, body)
7461227 @josh Ruby 1.9: Call join on template_root instead of to_s
josh authored
571 render :file => method_name, :body => body
1d4554d ActionMailer should respect content type when choosing layouts
Colin Curtin authored
572 ensure
573 @current_template_content_type = nil
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
574 end
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
575
576 def render(opts)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
577 layout, file = opts.delete(:layout), opts[:file]
578
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
579 begin
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
580 @template = initialize_template_class(opts.delete(:body))
581
582 if file
583 prefix = mailer_name unless file =~ /\//
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
584 template = view_paths.find_by_parts(file, {:formats => formats}, prefix)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
585 end
586
587 layout = _pick_layout(layout,
ab83db9 Fixes ActionMailer to work with the ActionView refactoring
Yehuda Katz + Carl Lerche authored
588 !template || ActionController::Base.exempt_from_layout.include?(template.handler))
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
589
590 if template
591 @template._render_template_with_layout(template, layout, opts)
592 elsif inline = opts[:inline]
593 @template._render_inline(inline, layout, opts)
594 end
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
595 end
596 end
597
598 def default_template_format
1d4554d ActionMailer should respect content type when choosing layouts
Colin Curtin authored
599 if @current_template_content_type
600 Mime::Type.lookup(@current_template_content_type).to_sym
601 else
602 :html
603 end
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
604 end
605
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
606 def template_root
607 self.class.template_root
608 end
609
610 def template_root=(root)
611 self.class.template_root = root
612 end
613
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
614 def template_path
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
615 "#{mailer_name}"
db045db @dhh Initial
dhh authored
616 end
617
5ec990a @jamis Helper support for ActionMailer
jamis authored
618 def initialize_template_class(assigns)
441e4e2 @svenfuchs load plugin view_paths to action_mailer view_paths and make action_ma…
svenfuchs authored
619 template = ActionView::Base.new(self.class.view_paths, assigns, self)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
620 template.formats = [default_template_format]
1d4554d ActionMailer should respect content type when choosing layouts
Colin Curtin authored
621 template
5ec990a @jamis Helper support for ActionMailer
jamis authored
622 end
623
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
624 def sort_parts(parts, order = [])
625 order = order.collect { |s| s.downcase }
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
626
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
627 parts = parts.sort do |a, b|
628 a_ct = a.content_type.downcase
629 b_ct = b.content_type.downcase
630
631 a_in = order.include? a_ct
632 b_in = order.include? b_ct
633
634 s = case
635 when a_in && b_in
636 order.index(a_ct) <=> order.index(b_ct)
637 when a_in
638 -1
639 when b_in
640 1
641 else
642 a_ct <=> b_ct
643 end
644
645 # reverse the ordering because parts that come last are displayed
646 # first in mail clients
647 (s * -1)
648 end
649
650 parts
651 end
652
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
653 def create_mail
654 m = TMail::Mail.new
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored
655
cf6299d Add ActionMailer#reply_to. [#245 state:resolved]
Adam authored
656 m.subject, = quote_any_if_necessary(charset, subject)
657 m.to, m.from = quote_any_address_if_necessary(charset, recipients, from)
658 m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
659 m.cc = quote_address_if_necessary(cc, charset) unless cc.nil?
660 m.reply_to = quote_address_if_necessary(reply_to, charset) unless reply_to.nil?
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
661 m.mime_version = mime_version unless mime_version.nil?
cf6299d Add ActionMailer#reply_to. [#245 state:resolved]
Adam authored
662 m.date = sent_on.to_time rescue sent_on if sent_on
663
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
664 headers.each { |k, v| m[k] = v }
db045db @dhh Initial
dhh authored
665
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
666 real_content_type, ctype_attrs = parse_content_type
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
667
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
668 if @parts.empty?
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
669 m.set_content_type(real_content_type, nil, ctype_attrs)
e201fc7 @josh use autoload instead of explicit requires for ActionMailer
josh authored
670 m.body = normalize_new_lines(body)
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
671 elsif @parts.size == 1 && @parts.first.parts.empty?
672 m.set_content_type(real_content_type, nil, ctype_attrs)
673 m.body = normalize_new_lines(@parts.first.body)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
674 else
675 if String === body
676 part = TMail::Mail.new
e201fc7 @josh use autoload instead of explicit requires for ActionMailer
josh authored
677 part.body = normalize_new_lines(body)
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
678 part.set_content_type(real_content_type, nil, ctype_attrs)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
679 part.set_content_disposition "inline"
680 m.parts << part
681 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
682
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
683 @parts.each do |p|
684 part = (TMail::Mail === p ? p : p.to_mail(self))
685 m.parts << part
686 end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
687
49efa02 @jamis Nil charset caused subject line to be improperly quoted in implicitly…
jamis authored
688 if real_content_type =~ /multipart/
689 ctype_attrs.delete "charset"
690 m.set_content_type(real_content_type, nil, ctype_attrs)
691 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
692 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
693
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
694 @mail = m
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
695 end
696
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
697 def perform_delivery_smtp(mail)
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
698 destinations = mail.destinations
699 mail.ready_to_send
250dfb1 @al2o3cr Fixed that ActionMailer should send correctly formatted Return-Path i…
al2o3cr authored
700 sender = (mail['return-path'] && mail['return-path'].spec) || mail.from
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
701
732c724 @granth Turn on STARTTLS if it is available in Net::SMTP (added in Ruby 1.8.7…
granth authored
702 smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
703 smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
732c724 @granth Turn on STARTTLS if it is available in Net::SMTP (added in Ruby 1.8.7…
granth authored
704 smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
705 smtp_settings[:authentication]) do |smtp|
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
706 smtp.sendmail(mail.encoded, sender, destinations)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
707 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
708 end
709
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
710 def perform_delivery_sendmail(mail)
77730f7 @NZKoz Avoid modifying the sendmail_settings hash when using the return path…
NZKoz authored
711 sendmail_args = sendmail_settings[:arguments]
712 sendmail_args += " -f \"#{mail['return-path']}\"" if mail['return-path']
713 IO.popen("#{sendmail_settings[:location]} #{sendmail_args}","w+") do |sm|
bb62568 @dhh Fix problem with sendmail delivery where headers should be delimited …
dhh authored
714 sm.print(mail.encoded.gsub(/\r/, ''))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
715 sm.flush
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
716 end
717 end
718
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
719 def perform_delivery_test(mail)
720 deliveries << mail
721 end
db045db @dhh Initial
dhh authored
722 end
e201fc7 @josh use autoload instead of explicit requires for ActionMailer
josh authored
723
724 Base.class_eval do
725 include Helpers
726 helper MailHelper
727 end
db045db @dhh Initial
dhh authored
728 end
Something went wrong with that request. Please try again.