Skip to content
Newer
Older
100644 618 lines (546 sloc) 24.3 KB
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
1 require 'action_mailer/adv_attr_accessor'
2 require 'action_mailer/part'
5ddffc8 @jamis Allow for nested parts in multipart mails #1570 [Flurin Egger]
jamis authored
3 require 'action_mailer/part_container'
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored
4 require 'action_mailer/utils'
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
5 require 'tmail/net'
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
6
c4590d5 @dhh Fixed docs
dhh authored
7 module ActionMailer #:nodoc:
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
8 # ActionMailer allows you to send email from your application using a mailer model and views.
db045db @dhh Initial
dhh authored
9 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
10 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
11 # = Mailer Models
20bd4bd @dhh Updated docs and otherwise
dhh authored
12 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
13 # To use ActionMailer, you need to create a mailer model.
14 #
15 # $ script/generate mailer Notifier
16 #
17 # The generated model inherits from ActionMailer::Base. Emails are defined by creating methods within the model which are then
18 # used to set variables to be used in the mail template, to change options on the mail, or
19 # to add attachments.
20 #
21 # Examples:
22 #
23 # class Notifier < ActionMailer::Base
24 # def signup_notification(recipient)
25 # recipients recipient.email_address_with_name
26 # from "system@example.com"
27 # subject "New account information"
20bd4bd @dhh Updated docs and otherwise
dhh authored
28 # body :account => recipient
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
29 # end
30 # end
31 #
32 # Mailer methods have the following configuration methods available.
33 #
34 # * <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.
35 # * <tt>subject</tt> - The subject of your email. Sets the <tt>Subject:</tt> header.
36 # * <tt>from</tt> - Who the email you are sending is from. Sets the <tt>From:</tt> header.
37 # * <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.
38 # * <tt>bcc</tt> - Takes one or more email address. These addresses will receive a blind carbon copy of your email. Sets the <tt>Bcc</tt> header.
39 # * <tt>sent_on</tt> - The date on which the message was sent. If not set, the header wil be set by the delivery agent.
40 # * <tt>content_type</tt> - Specify the content type of the message. Defaults to <tt>text/plain</tt>.
41 # * <tt>headers</tt> - Specify additional headers to be set for the message, e.g. <tt>headers 'X-Mail-Count' => 107370</tt>.
42 #
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
43 # When a <tt>headers 'return-path'</tt> is specified, that value will be used as the 'envelope from'
44 # address. Setting this is useful when you want delivery notifications sent to a different address than
45 # the one in <tt>from</tt>.
46 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
47 # The <tt>body</tt> method has special behavior. It takes a hash which generates an instance variable
48 # named after each key in the hash containing the value that that key points to.
49 #
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
50 # So, for example, <tt>body :account => recipient</tt> would result
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
51 # in an instance variable <tt>@account</tt> with the value of <tt>recipient</tt> being accessible in the
52 # view.
53 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
54 #
55 # = Mailer views
56 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
57 # Like ActionController, each mailer class has a corresponding view directory
58 # in which each method of the class looks for a template with its name.
21187c0 @dhh Apply the rest of Chads patch
dhh authored
59 # To define a template to be used with a mailing, create an <tt>.erb</tt> file with the same name as the method
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
60 # in your mailer model. For example, in the mailer defined above, the template at
21187c0 @dhh Apply the rest of Chads patch
dhh authored
61 # <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
62 #
63 # Variables defined in the model are accessible as instance variables in the view.
64 #
65 # Emails by default are sent in plain text, so a sample view for our model example might look like this:
66 #
67 # Hi <%= @account.name %>,
68 # Thanks for joining our service! Please check back often.
69 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
70 # You can even use Action Pack helpers in these views. For example:
71 #
72 # You got a new note!
73 # <%= truncate(note.body, 25) %>
74 #
75 #
76 # = Generating URLs for mailer views
77 #
78 # If your view includes URLs from the application, you need to use url_for in the mailing method instead of the view.
79 # Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request. That's
80 # why you need to jump this little hoop and supply all the details needed for the URL. Example:
81 #
82 # def signup_notification(recipient)
83 # recipients recipient.email_address_with_name
84 # from "system@example.com"
85 # subject "New account information"
86 # body :account => recipient,
87 # :home_page => url_for(:host => "example.com", :controller => "welcome", :action => "greeting")
88 # end
89 #
90 # You can now access @home_page in the template and get http://example.com/welcome/greeting.
91 #
92 # = Sending mail
93 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
94 # Once a mailer action and template are defined, you can deliver your message or create it and save it
95 # for delivery later:
96 #
97 # Notifier.deliver_signup_notification(david) # sends the email
98 # mail = Notifier.create_signup_notification(david) # => a tmail object
99 # Notifier.deliver(mail)
100 #
101 # You never instantiate your mailer class. Rather, your delivery instance
102 # methods are automatically wrapped in class methods that start with the word
103 # <tt>deliver_</tt> followed by the name of the mailer method that you would
104 # like to deliver. The <tt>signup_notification</tt> method defined above is
105 # delivered by invoking <tt>Notifier.deliver_signup_notification</tt>.
106 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
107 #
108 # = HTML email
109 #
21187c0 @dhh Apply the rest of Chads patch
dhh authored
110 # 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
111 # set the content type to html.
112 #
113 # class MyMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
114 # def signup_notification(recipient)
115 # recipients recipient.email_address_with_name
116 # subject "New account information"
b305ef3 Correct spurious documentation example code which results in a Syntax…
Marcel Molina authored
117 # body "account" => recipient
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
118 # from "system@example.com"
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
119 # content_type "text/html" # Here's where the magic happens
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
120 # end
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
121 # end
122 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
123 #
124 # = Multipart email
125 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
126 # You can explicitly specify multipart messages:
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
127 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
128 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
129 # def signup_notification(recipient)
130 # recipients recipient.email_address_with_name
131 # subject "New account information"
132 # from "system@example.com"
133 #
134 # part :content_type => "text/html",
135 # :body => render_message("signup-as-html", :account => recipient)
136 #
137 # part "text/plain" do |p|
138 # p.body = render_message("signup-as-plain", :account => recipient)
139 # p.transfer_encoding = "base64"
140 # end
141 # end
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
142 # end
143 #
144 # Multipart messages can also be used implicitly because ActionMailer will automatically
145 # detect and use multipart templates, where each template is named after the name of the action, followed
146 # by the content type. Each such detected template will be added as separate part to the message.
147 #
148 # For example, if the following templates existed:
21187c0 @dhh Apply the rest of Chads patch
dhh authored
149 # * signup_notification.text.plain.erb
150 # * signup_notification.text.html.erb
151 # * signup_notification.text.xml.builder
152 # * signup_notification.text.x-yaml.erb
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
153 #
154 # Each would be rendered and added as a separate part to the message,
155 # with the corresponding content type. The same body hash is passed to
156 # each template.
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
157 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
158 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
159 # = Attachments
20bd4bd @dhh Updated docs and otherwise
dhh authored
160 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
161 # Attachments can be added by using the +attachment+ method.
162 #
163 # Example:
164 #
165 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
166 # # attachments
167 # def signup_notification(recipient)
168 # recipients recipient.email_address_with_name
169 # subject "New account information"
170 # from "system@example.com"
171 #
172 # attachment :content_type => "image/jpeg",
173 # :body => File.read("an-image.jpg")
174 #
175 # attachment "application/pdf" do |a|
176 # a.body = generate_your_pdf_here()
177 # end
db045db @dhh Initial
dhh authored
178 # end
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
179 # end
c927aa0 @dhh Updated documentation
dhh authored
180 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
181 #
c927aa0 @dhh Updated documentation
dhh authored
182 # = Configuration options
183 #
184 # These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
185 #
186 # * <tt>template_root</tt> - template root determines the base from which template references will be made.
187 #
188 # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
189 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
190 #
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to all…
NZKoz authored
191 # * <tt>smtp_settings</tt> - Allows detailed configuration for :smtp delivery method:
475bd74 @dhh Fix docs (closes #2598)
dhh authored
192 # * <tt>:address</tt> Allows you to use a remote mail server. Just change it from its default "localhost" setting.
193 # * <tt>:port</tt> On the off chance that your mail server doesn't run on port 25, you can change it.
c927aa0 @dhh Updated documentation
dhh authored
194 # * <tt>:domain</tt> If you need to specify a HELO domain, you can do it here.
475bd74 @dhh Fix docs (closes #2598)
dhh authored
195 # * <tt>:user_name</tt> If your mail server requires authentication, set the username in this setting.
196 # * <tt>:password</tt> If your mail server requires authentication, set the password in this setting.
c927aa0 @dhh Updated documentation
dhh authored
197 # * <tt>:authentication</tt> If your mail server requires authentication, you need to specify the authentication type here.
198 # This is a symbol and one of :plain, :login, :cram_md5
199 #
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to all…
NZKoz authored
200 # * <tt>sendmail_settings</tt> - Allows you to override options for the :sendmail delivery method
201 # * <tt>:location</tt> The location of the sendmail executable, defaults to "/usr/sbin/sendmail"
202 # * <tt>:arguments</tt> The command line arguments
c927aa0 @dhh Updated documentation
dhh authored
203 # * <tt>raise_delivery_errors</tt> - whether or not errors should be raised if the email fails to be delivered.
204 #
205 # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.
206 #
207 # * <tt>perform_deliveries</tt> - Determines whether deliver_* methods are actually carried out. By default they are,
208 # but this can be turned off to help functional testing.
209 #
210 # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful
211 # for unit and functional testing.
af56c80 @dhh Updated documentation
dhh authored
212 #
213 # * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
214 # pick a different charset from inside a method with <tt>@charset</tt>.
475bd74 @dhh Fix docs (closes #2598)
dhh authored
215 # * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
216 # can also pick a different content type from inside a method with <tt>@content_type</tt>.
45b5555 @jamis Make mime version default to 1.0. closes #2323
jamis authored
217 # * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to "1.0". You
218 # can also pick a different value from inside a method with <tt>@mime_version</tt>.
475bd74 @dhh Fix docs (closes #2598)
dhh authored
219 # * <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
220 # which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
221 # ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client
222 # and appear last in the mime encoded message. You can also pick a different order from inside a method with
223 # <tt>@implicit_parts_order</tt>.
db045db @dhh Initial
dhh authored
224 class Base
0fe8e3d @dhh Added a config example in README #1626 [courtenay]
dhh authored
225 include AdvAttrAccessor, PartContainer
4b08caf @technoweenie Only load ActionController::UrlWriter if ActionController is present …
technoweenie authored
226 include ActionController::UrlWriter if Object.const_defined?(:ActionController)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
227
c927aa0 @dhh Updated documentation
dhh authored
228 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored
229
5446ecd @jeremy Mailer template root applies to a class and its subclasses rather tha…
jeremy authored
230 class_inheritable_accessor :template_root
db045db @dhh Initial
dhh authored
231 cattr_accessor :logger
232
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
233 cattr_accessor :template_extensions
234 @@template_extensions = ['erb', 'builder', 'rhtml', 'rxml']
235
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to all…
NZKoz authored
236 @@smtp_settings = {
db045db @dhh Initial
dhh authored
237 :address => "localhost",
238 :port => 25,
239 :domain => 'localhost.localdomain',
240 :user_name => nil,
241 :password => nil,
242 :authentication => nil
243 }
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to all…
NZKoz authored
244 cattr_accessor :smtp_settings
245
246 @@sendmail_settings = {
247 :location => '/usr/sbin/sendmail',
248 :arguments => '-i -t'
249 }
250 cattr_accessor :sendmail_settings
db045db @dhh Initial
dhh authored
251
252 @@raise_delivery_errors = true
253 cattr_accessor :raise_delivery_errors
254
57cde63 @dhh Allow ActionMailer subclasses to individually set their delivery meth…
dhh authored
255 superclass_delegating_accessor :delivery_method
256 self.delivery_method = :smtp
db045db @dhh Initial
dhh authored
257
258 @@perform_deliveries = true
259 cattr_accessor :perform_deliveries
260
261 @@deliveries = []
262 cattr_accessor :deliveries
263
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
264 @@default_charset = "utf-8"
265 cattr_accessor :default_charset
266
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
267 @@default_content_type = "text/plain"
268 cattr_accessor :default_content_type
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
269
45b5555 @jamis Make mime version default to 1.0. closes #2323
jamis authored
270 @@default_mime_version = "1.0"
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
271 cattr_accessor :default_mime_version
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
272
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
273 @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
274 cattr_accessor :default_implicit_parts_order
275
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
276 # Specify the BCC addresses for the message
277 adv_attr_accessor :bcc
278
279 # Define the body of the message. This is either a Hash (in which case it
280 # specifies the variables to pass to the template when it is rendered),
281 # or a string, in which case it specifies the actual text of the message.
282 adv_attr_accessor :body
283
284 # Specify the CC addresses for the message.
285 adv_attr_accessor :cc
286
287 # Specify the charset to use for the message. This defaults to the
288 # +default_charset+ specified for ActionMailer::Base.
289 adv_attr_accessor :charset
290
291 # Specify the content type for the message. This defaults to <tt>text/plain</tt>
292 # in most cases, but can be automatically set in some situations.
293 adv_attr_accessor :content_type
294
295 # Specify the from address for the message.
296 adv_attr_accessor :from
297
298 # Specify additional headers to be added to the message.
299 adv_attr_accessor :headers
300
301 # Specify the order in which parts should be sorted, based on content-type.
302 # This defaults to the value for the +default_implicit_parts_order+.
303 adv_attr_accessor :implicit_parts_order
304
305 # Defaults to "1.0", but may be explicitly given if needed.
306 adv_attr_accessor :mime_version
307
308 # The recipient addresses for the message, either as a string (for a single
309 # address) or an array (for multiple addresses).
310 adv_attr_accessor :recipients
311
312 # The date on which the message was sent. If not set (the default), the
313 # header will be set by the delivery agent.
314 adv_attr_accessor :sent_on
315
316 # Specify the subject of the message.
317 adv_attr_accessor :subject
318
319 # Specify the template name to use for current message. This is the "base"
320 # template name, without the extension or directory, and may be used to
321 # have multiple mailer methods share the same template.
322 adv_attr_accessor :template
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
323
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
324 # Override the mailer name, which defaults to an inflected version of the
325 # mailer's class name. If you want to use a template in a non-standard
326 # location, you can use this to specify that location.
327 def mailer_name(value = nil)
328 if value
329 self.mailer_name = value
330 else
331 self.class.mailer_name
332 end
333 end
334
335 def mailer_name=(value)
336 self.class.mailer_name = value
337 end
338
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
339 # The mail object instance referenced by this mailer.
340 attr_reader :mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
341
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
342 class << self
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
343 attr_writer :mailer_name
344
345 def mailer_name
346 @mailer_name ||= name.underscore
347 end
348
349 # for ActionView compatibility
350 alias_method :controller_name, :mailer_name
351 alias_method :controller_path, :mailer_name
352
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
353 def method_missing(method_symbol, *parameters)#:nodoc:
354 case method_symbol.id2name
355 when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
356 when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
357 when "new" then nil
358 else super
359 end
360 end
361
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
362 # Receives a raw email, parses it into an email object, decodes it,
363 # instantiates a new mailer, and passes the email object to the mailer
364 # object's #receive method. If you want your mailer to be able to
365 # process incoming messages, you'll need to implement a #receive
366 # method that accepts the email object as a parameter:
367 #
368 # class MyMailer < ActionMailer::Base
369 # def receive(mail)
370 # ...
371 # end
372 # end
373 def receive(raw_email)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
374 logger.info "Received mail:\n #{raw_email}" unless logger.nil?
375 mail = TMail::Mail.parse(raw_email)
376 mail.base64_decode
377 new.receive(mail)
378 end
379
380 # Deliver the given mail object directly. This can be used to deliver
381 # a preconstructed mail object, like:
382 #
383 # email = MyMailer.create_some_mail(parameters)
384 # email.set_some_obscure_header "frobnicate"
385 # MyMailer.deliver(email)
386 def deliver(mail)
387 new.deliver!(mail)
388 end
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
389
390 # Register a template extension so mailer templates written in a
391 # templating language other than rhtml or rxml are supported.
392 # To use this, include in your template-language plugin's init
393 # code or on a per-application basis, this can be invoked from
394 # config/environment.rb:
395 #
396 # ActionMailer::Base.register_template_extension('haml')
397 def register_template_extension(extension)
398 template_extensions << extension
399 end
61c90a4 @NZKoz Reapply the TemplateFinder first applied in [8669] then reverted in […
NZKoz authored
400
401 def template_root=(root)
402 write_inheritable_attribute(:template_root, root)
403 ActionView::TemplateFinder.process_view_paths(root)
404 end
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
405 end
406
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
407 # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
408 # will be initialized according to the named method. If not, the mailer will
409 # remain uninitialized (useful when you only need to invoke the "receive"
410 # method, for instance).
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
411 def initialize(method_name=nil, *parameters) #:nodoc:
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
412 create!(method_name, *parameters) if method_name
413 end
414
415 # Initialize the mailer via the given +method_name+. The body will be
416 # rendered and a new TMail::Mail object created.
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
417 def create!(method_name, *parameters) #:nodoc:
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
418 initialize_defaults(method_name)
c3ff04b @jeremy Allow mailer actions named send by using __send__ internally. Closes #…
jeremy authored
419 __send__(method_name, *parameters)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
420
421 # If an explicit, textual body has not been set, we check assumptions.
422 unless String === @body
423 # First, we look to see if there are any likely templates that match,
424 # which include the content-type in their file name (i.e.,
21187c0 @dhh Apply the rest of Chads patch
dhh authored
425 # "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
426 # have not already been specified manually.
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
427 if @parts.empty?
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored
428 templates = Dir.glob("#{template_path}/#{@template}.*")
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
429 templates.each do |path|
797de4d @jamis Use the full template file name for implicitly selected templates, in…
jamis authored
430 basename = File.basename(path)
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
431 template_regex = Regexp.new("^([^\\\.]+)\\\.([^\\\.]+\\\.[^\\\.]+)\\\.(" + template_extensions.join('|') + ")$")
432 next unless md = template_regex.match(basename)
797de4d @jamis Use the full template file name for implicitly selected templates, in…
jamis authored
433 template_name = basename
f2ee215 @jeremy Stricter matching for implicitly multipart filenames excludes files e…
jeremy authored
434 content_type = md.captures[1].gsub('.', '/')
435 @parts << Part.new(:content_type => content_type,
053cb22 @jamis Use the real charset for parts generated by a template, instead of th…
jamis authored
436 :disposition => "inline", :charset => charset,
f3aa7c1 @jeremy r4730@asus: jeremy | 2006-06-29 13:13:38 -0700
jeremy authored
437 :body => render_message(template_name, @body))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
438 end
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
439 unless @parts.empty?
440 @content_type = "multipart/alternative"
441 @parts = sort_parts(@parts, @implicit_parts_order)
442 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
443 end
db045db @dhh Initial
dhh authored
444
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
445 # Then, if there were such templates, we check to see if we ought to
446 # also render a "normal" template (without the content type). If a
447 # normal template exists (or if there were no implicit parts) we render
448 # it.
449 template_exists = @parts.empty?
34df9be @jamis Template paths with dot chars in them no longer mess up implicit temp…
jamis authored
450 template_exists ||= Dir.glob("#{template_path}/#{@template}.*").any? { |i| File.basename(i).split(".").length == 2 }
f3aa7c1 @jeremy r4730@asus: jeremy | 2006-06-29 13:13:38 -0700
jeremy authored
451 @body = render_message(@template, @body) if template_exists
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
452
453 # Finally, if there are other message parts and a textual body exists,
454 # we shift it onto the front of the parts and set the body to nil (so
455 # that create_mail doesn't try to render it in addition to the parts).
456 if !@parts.empty? && String === @body
457 @parts.unshift Part.new(:charset => charset, :body => @body)
458 @body = nil
425aa50 @dhh Added receiver method for incoming email
dhh authored
459 end
db045db @dhh Initial
dhh authored
460 end
461
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
462 # If this is a multipart e-mail add the mime_version if it is not
463 # already set.
464 @mime_version ||= "1.0" if !@parts.empty?
465
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
466 # build the mail object itself
467 @mail = create_mail
468 end
db045db @dhh Initial
dhh authored
469
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
470 # Delivers a TMail::Mail object. By default, it delivers the cached mail
471 # object (from the #create! method). If no cached mail object exists, and
472 # no alternate has been given as the parameter, this will fail.
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
473 def deliver!(mail = @mail)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
474 raise "no mail object available for delivery!" unless mail
fbd3eb7 @jeremy Less verbose mail logging: just recipients for :info log level; the w…
jeremy authored
475 unless logger.nil?
be36455 @jeremy Ruby 1.9 compat: use Array() instead of #to_a.
jeremy authored
476 logger.info "Sent mail to #{Array(recipients).join(', ')}"
fbd3eb7 @jeremy Less verbose mail logging: just recipients for :info log level; the w…
jeremy authored
477 logger.debug "\n#{mail.encoded}"
478 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
479
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
480 begin
c3ff04b @jeremy Allow mailer actions named send by using __send__ internally. Closes #…
jeremy authored
481 __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
12ff554 @jeremy Tighten rescue clauses. Closes #5985.
jeremy authored
482 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
483 raise e if raise_delivery_errors
484 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
485
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
486 return mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
487 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
488
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
489 private
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
490 # Set up the default values for the various instance variables of this
491 # mailer. Subclasses may override this method to provide different
492 # defaults.
493 def initialize_defaults(method_name)
95e8740 @dhh Allow Mailers to have custom initialize methods that set default inst…
dhh authored
494 @charset ||= @@default_charset.dup
495 @content_type ||= @@default_content_type.dup
496 @implicit_parts_order ||= @@default_implicit_parts_order.dup
497 @template ||= method_name
498 @mailer_name ||= Inflector.underscore(self.class.name)
499 @parts ||= []
500 @headers ||= {}
501 @body ||= {}
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
502 @mime_version = @@default_mime_version.dup if @@default_mime_version
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
503 end
504
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
505 def render_message(method_name, body)
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
506 render :file => method_name, :body => body
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
507 end
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
508
509 def render(opts)
510 body = opts.delete(:body)
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
511 if opts[:file] && opts[:file] !~ /\//
512 opts[:file] = "#{mailer_name}/#{opts[:file]}"
513 end
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
514 initialize_template_class(body).render(opts)
515 end
516
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
517 def template_path
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
518 "#{template_root}/#{mailer_name}"
db045db @dhh Initial
dhh authored
519 end
520
5ec990a @jamis Helper support for ActionMailer
jamis authored
521 def initialize_template_class(assigns)
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
522 ActionView::Base.new([template_root], assigns, self)
5ec990a @jamis Helper support for ActionMailer
jamis authored
523 end
524
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
525 def sort_parts(parts, order = [])
526 order = order.collect { |s| s.downcase }
527
528 parts = parts.sort do |a, b|
529 a_ct = a.content_type.downcase
530 b_ct = b.content_type.downcase
531
532 a_in = order.include? a_ct
533 b_in = order.include? b_ct
534
535 s = case
536 when a_in && b_in
537 order.index(a_ct) <=> order.index(b_ct)
538 when a_in
539 -1
540 when b_in
541 1
542 else
543 a_ct <=> b_ct
544 end
545
546 # reverse the ordering because parts that come last are displayed
547 # first in mail clients
548 (s * -1)
549 end
550
551 parts
552 end
553
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
554 def create_mail
555 m = TMail::Mail.new
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored
556
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
557 m.subject, = quote_any_if_necessary(charset, subject)
558 m.to, m.from = quote_any_address_if_necessary(charset, recipients, from)
559 m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
560 m.cc = quote_address_if_necessary(cc, charset) unless cc.nil?
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored
561
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored
562 m.mime_version = mime_version unless mime_version.nil?
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
563 m.date = sent_on.to_time rescue sent_on if sent_on
564 headers.each { |k, v| m[k] = v }
db045db @dhh Initial
dhh authored
565
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
566 real_content_type, ctype_attrs = parse_content_type
567
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
568 if @parts.empty?
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
569 m.set_content_type(real_content_type, nil, ctype_attrs)
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored
570 m.body = Utils.normalize_new_lines(body)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
571 else
572 if String === body
573 part = TMail::Mail.new
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored
574 part.body = Utils.normalize_new_lines(body)
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
575 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
576 part.set_content_disposition "inline"
577 m.parts << part
578 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
579
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
580 @parts.each do |p|
581 part = (TMail::Mail === p ? p : p.to_mail(self))
582 m.parts << part
583 end
3b4eb7a @jamis Allow specific "multipart/xxx" content-type to be set on multipart me…
jamis authored
584
49efa02 @jamis Nil charset caused subject line to be improperly quoted in implicitly…
jamis authored
585 if real_content_type =~ /multipart/
586 ctype_attrs.delete "charset"
587 m.set_content_type(real_content_type, nil, ctype_attrs)
588 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
589 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
590
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
591 @mail = m
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
592 end
593
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
594 def perform_delivery_smtp(mail)
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
595 destinations = mail.destinations
596 mail.ready_to_send
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
597 sender = mail['return-path'] || mail.from
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
598
416091d @NZKoz Fix missed smtp_session changes to base.rb.
NZKoz authored
599 Net::SMTP.start(smtp_settings[:address], smtp_settings[:port], smtp_settings[:domain],
600 smtp_settings[:user_name], smtp_settings[:password], smtp_settings[:authentication]) do |smtp|
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
601 smtp.sendmail(mail.encoded, sender, destinations)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
602 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
603 end
604
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
605 def perform_delivery_sendmail(mail)
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
606 sendmail_settings[:arguments] += " -f \"#{mail['return-path']}\"" if mail['return-path']
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to all…
NZKoz authored
607 IO.popen("#{sendmail_settings[:location]} #{sendmail_settings[:arguments]}","w+") do |sm|
bb62568 @dhh Fix problem with sendmail delivery where headers should be delimited …
dhh authored
608 sm.print(mail.encoded.gsub(/\r/, ''))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
609 sm.flush
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
610 end
611 end
612
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
613 def perform_delivery_test(mail)
614 deliveries << mail
615 end
db045db @dhh Initial
dhh authored
616 end
617 end
Something went wrong with that request. Please try again.