Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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