Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 581 lines (514 sloc) 23.681 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 #
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 @dhh Updated docs and otherwise
dhh authored
50 #
51 # = Mailer views
52 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
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.
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
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina 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) %>
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, …
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 @dhh Updated docs and otherwise
dhh authored
103 #
104 # = HTML email
105 #
21187c0 @dhh Apply the rest of Chads patch
dhh authored
106 # 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
107 # set the content type to html.
108 #
109 # class MyMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
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 Syntax…
Marcel Molina authored
113 # body "account" => recipient
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
114 # from "system@example.com"
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
115 # 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
116 # end
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
117 # end
118 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
119 #
120 # = Multipart email
121 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
122 # You can explicitly specify multipart messages:
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
123 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
124 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
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, …
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:
21187c0 @dhh Apply the rest of Chads patch
dhh authored
145 # * signup_notification.text.plain.erb
146 # * signup_notification.text.html.erb
147 # * signup_notification.text.xml.builder
148 # * signup_notification.text.x-yaml.erb
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
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 A very thorough refactoring, resulting in new mail property setters a…
jamis authored
153 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
154 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
155 # = Attachments
20bd4bd @dhh Updated docs and otherwise
dhh authored
156 #
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
157 # Attachments can be added by using the +attachment+ method.
158 #
159 # Example:
160 #
161 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
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 @dhh Initial
dhh authored
174 # end
0a407bc ActionMailer::Base documentation rewrite. Closes #4991 [Kevin Clark, …
Marcel Molina authored
175 # end
c927aa0 @dhh Updated documentation
dhh authored
176 #
20bd4bd @dhh Updated docs and otherwise
dhh authored
177 #
c927aa0 @dhh 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 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to al…
NZKoz authored
187 # * <tt>smtp_settings</tt> - Allows detailed configuration for :smtp delivery method:
475bd74 @dhh 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 @dhh Updated documentation
dhh authored
190 # * <tt>:domain</tt> If you need to specify a HELO domain, you can do it here.
475bd74 @dhh 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 @dhh 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 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to al…
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 @dhh 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 @dhh 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 Better multipart support with implicit multipart/alternative and sort…
jamis authored
210 # pick a different charset from inside a method with <tt>@charset</tt>.
475bd74 @dhh 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 Better multipart support with implicit multipart/alternative and sort…
jamis authored
212 # 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
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 @dhh 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 Better multipart support with implicit multipart/alternative and sort…
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 @dhh Initial
dhh authored
220 class Base
0fe8e3d @dhh Added a config example in README #1626 [courtenay]
dhh authored
221 include AdvAttrAccessor, PartContainer
4b08caf @technoweenie Only load ActionController::UrlWriter if ActionController is present …
technoweenie authored
222 include ActionController::UrlWriter if Object.const_defined?(:ActionController)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
223
c927aa0 @dhh Updated documentation
dhh authored
224 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored
225
5446ecd @jeremy Mailer template root applies to a class and its subclasses rather tha…
jeremy authored
226 class_inheritable_accessor :template_root
db045db @dhh Initial
dhh authored
227 cattr_accessor :logger
228
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
229 cattr_accessor :template_extensions
230 @@template_extensions = ['erb', 'builder', 'rhtml', 'rxml']
231
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to al…
NZKoz authored
232 @@smtp_settings = {
db045db @dhh Initial
dhh authored
233 :address => "localhost",
234 :port => 25,
235 :domain => 'localhost.localdomain',
236 :user_name => nil,
237 :password => nil,
238 :authentication => nil
239 }
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to al…
NZKoz authored
240 cattr_accessor :smtp_settings
241
242 @@sendmail_settings = {
243 :location => '/usr/sbin/sendmail',
244 :arguments => '-i -t'
245 }
246 cattr_accessor :sendmail_settings
db045db @dhh Initial
dhh authored
247
248 @@raise_delivery_errors = true
249 cattr_accessor :raise_delivery_errors
250
57cde63 @dhh Allow ActionMailer subclasses to individually set their delivery meth…
dhh authored
251 superclass_delegating_accessor :delivery_method
252 self.delivery_method = :smtp
db045db @dhh Initial
dhh authored
253
254 @@perform_deliveries = true
255 cattr_accessor :perform_deliveries
256
257 @@deliveries = []
258 cattr_accessor :deliveries
259
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
260 @@default_charset = "utf-8"
261 cattr_accessor :default_charset
262
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
263 @@default_content_type = "text/plain"
264 cattr_accessor :default_content_type
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
265
45b5555 @jamis Make mime version default to 1.0. closes #2323
jamis authored
266 @@default_mime_version = "1.0"
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
267 cattr_accessor :default_mime_version
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
268
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
269 @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
270 cattr_accessor :default_implicit_parts_order
271
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
272 # Specify the BCC addresses for the message
273 adv_attr_accessor :bcc
274
275 # Define the body of the message. This is either a Hash (in which case it
276 # specifies the variables to pass to the template when it is rendered),
277 # or a string, in which case it specifies the actual text of the message.
278 adv_attr_accessor :body
279
280 # Specify the CC addresses for the message.
281 adv_attr_accessor :cc
282
283 # Specify the charset to use for the message. This defaults to the
284 # +default_charset+ specified for ActionMailer::Base.
285 adv_attr_accessor :charset
286
287 # Specify the content type for the message. This defaults to <tt>text/plain</tt>
288 # in most cases, but can be automatically set in some situations.
289 adv_attr_accessor :content_type
290
291 # Specify the from address for the message.
292 adv_attr_accessor :from
293
294 # Specify additional headers to be added to the message.
295 adv_attr_accessor :headers
296
297 # Specify the order in which parts should be sorted, based on content-type.
298 # This defaults to the value for the +default_implicit_parts_order+.
299 adv_attr_accessor :implicit_parts_order
300
301 # Override the mailer name, which defaults to an inflected version of the
302 # mailer's class name. If you want to use a template in a non-standard
303 # location, you can use this to specify that location.
304 adv_attr_accessor :mailer_name
305
306 # Defaults to "1.0", but may be explicitly given if needed.
307 adv_attr_accessor :mime_version
308
309 # The recipient addresses for the message, either as a string (for a single
310 # address) or an array (for multiple addresses).
311 adv_attr_accessor :recipients
312
313 # The date on which the message was sent. If not set (the default), the
314 # header will be set by the delivery agent.
315 adv_attr_accessor :sent_on
316
317 # Specify the subject of the message.
318 adv_attr_accessor :subject
319
320 # Specify the template name to use for current message. This is the "base"
321 # template name, without the extension or directory, and may be used to
322 # have multiple mailer methods share the same template.
323 adv_attr_accessor :template
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
324
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
325 # The mail object instance referenced by this mailer.
326 attr_reader :mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
327
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
328 class << self
329 def method_missing(method_symbol, *parameters)#:nodoc:
330 case method_symbol.id2name
331 when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
332 when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
333 when "new" then nil
334 else super
335 end
336 end
337
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
338 # Receives a raw email, parses it into an email object, decodes it,
339 # instantiates a new mailer, and passes the email object to the mailer
340 # object's #receive method. If you want your mailer to be able to
341 # process incoming messages, you'll need to implement a #receive
342 # method that accepts the email object as a parameter:
343 #
344 # class MyMailer < ActionMailer::Base
345 # def receive(mail)
346 # ...
347 # end
348 # end
349 def receive(raw_email)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
350 logger.info "Received mail:\n #{raw_email}" unless logger.nil?
351 mail = TMail::Mail.parse(raw_email)
352 mail.base64_decode
353 new.receive(mail)
354 end
355
356 # Deliver the given mail object directly. This can be used to deliver
357 # a preconstructed mail object, like:
358 #
359 # email = MyMailer.create_some_mail(parameters)
360 # email.set_some_obscure_header "frobnicate"
361 # MyMailer.deliver(email)
362 def deliver(mail)
363 new.deliver!(mail)
364 end
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
365
366 # Register a template extension so mailer templates written in a
367 # templating language other than rhtml or rxml are supported.
368 # To use this, include in your template-language plugin's init
369 # code or on a per-application basis, this can be invoked from
370 # config/environment.rb:
371 #
372 # ActionMailer::Base.register_template_extension('haml')
373 def register_template_extension(extension)
374 template_extensions << extension
375 end
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
376 end
377
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
378 # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
379 # will be initialized according to the named method. If not, the mailer will
380 # remain uninitialized (useful when you only need to invoke the "receive"
381 # method, for instance).
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
382 def initialize(method_name=nil, *parameters) #:nodoc:
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
383 create!(method_name, *parameters) if method_name
384 end
385
386 # Initialize the mailer via the given +method_name+. The body will be
387 # rendered and a new TMail::Mail object created.
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
388 def create!(method_name, *parameters) #:nodoc:
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
389 initialize_defaults(method_name)
c3ff04b @jeremy Allow mailer actions named send by using __send__ internally. Closes …
jeremy authored
390 __send__(method_name, *parameters)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
391
392 # If an explicit, textual body has not been set, we check assumptions.
393 unless String === @body
394 # First, we look to see if there are any likely templates that match,
395 # which include the content-type in their file name (i.e.,
21187c0 @dhh Apply the rest of Chads patch
dhh authored
396 # "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
397 # have not already been specified manually.
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
398 if @parts.empty?
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored
399 templates = Dir.glob("#{template_path}/#{@template}.*")
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
400 templates.each do |path|
797de4d @jamis Use the full template file name for implicitly selected templates, in…
jamis authored
401 basename = File.basename(path)
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
402 template_regex = Regexp.new("^([^\\\.]+)\\\.([^\\\.]+\\\.[^\\\.]+)\\\.(" + template_extensions.join('|') + ")$")
403 next unless md = template_regex.match(basename)
797de4d @jamis Use the full template file name for implicitly selected templates, in…
jamis authored
404 template_name = basename
f2ee215 @jeremy Stricter matching for implicitly multipart filenames excludes files e…
jeremy authored
405 content_type = md.captures[1].gsub('.', '/')
406 @parts << Part.new(:content_type => content_type,
053cb22 @jamis Use the real charset for parts generated by a template, instead of th…
jamis authored
407 :disposition => "inline", :charset => charset,
f3aa7c1 @jeremy r4730@asus: jeremy | 2006-06-29 13:13:38 -0700
jeremy authored
408 :body => render_message(template_name, @body))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
409 end
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
410 unless @parts.empty?
411 @content_type = "multipart/alternative"
412 @parts = sort_parts(@parts, @implicit_parts_order)
413 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
414 end
db045db @dhh Initial
dhh authored
415
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
416 # Then, if there were such templates, we check to see if we ought to
417 # also render a "normal" template (without the content type). If a
418 # normal template exists (or if there were no implicit parts) we render
419 # it.
420 template_exists = @parts.empty?
34df9be @jamis Template paths with dot chars in them no longer mess up implicit temp…
jamis authored
421 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
422 @body = render_message(@template, @body) if template_exists
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
423
424 # Finally, if there are other message parts and a textual body exists,
425 # we shift it onto the front of the parts and set the body to nil (so
426 # that create_mail doesn't try to render it in addition to the parts).
427 if !@parts.empty? && String === @body
428 @parts.unshift Part.new(:charset => charset, :body => @body)
429 @body = nil
425aa50 @dhh Added receiver method for incoming email
dhh authored
430 end
db045db @dhh Initial
dhh authored
431 end
432
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
433 # If this is a multipart e-mail add the mime_version if it is not
434 # already set.
435 @mime_version ||= "1.0" if !@parts.empty?
436
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
437 # build the mail object itself
438 @mail = create_mail
439 end
db045db @dhh Initial
dhh authored
440
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
441 # Delivers a TMail::Mail object. By default, it delivers the cached mail
442 # object (from the #create! method). If no cached mail object exists, and
443 # no alternate has been given as the parameter, this will fail.
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
444 def deliver!(mail = @mail)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
445 raise "no mail object available for delivery!" unless mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
446 logger.info "Sent mail:\n #{mail.encoded}" unless logger.nil?
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
447
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
448 begin
c3ff04b @jeremy Allow mailer actions named send by using __send__ internally. Closes …
jeremy authored
449 __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
12ff554 @jeremy Tighten rescue clauses. Closes #5985.
jeremy authored
450 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
451 raise e if raise_delivery_errors
452 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
453
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
454 return mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
455 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
456
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
457 private
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
458 # Set up the default values for the various instance variables of this
459 # mailer. Subclasses may override this method to provide different
460 # defaults.
461 def initialize_defaults(method_name)
95e8740 @dhh Allow Mailers to have custom initialize methods that set default inst…
dhh authored
462 @charset ||= @@default_charset.dup
463 @content_type ||= @@default_content_type.dup
464 @implicit_parts_order ||= @@default_implicit_parts_order.dup
465 @template ||= method_name
466 @mailer_name ||= Inflector.underscore(self.class.name)
467 @parts ||= []
468 @headers ||= {}
469 @body ||= {}
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
470 @mime_version = @@default_mime_version.dup if @@default_mime_version
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
471 end
472
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
473 def render_message(method_name, body)
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
474 render :file => method_name, :body => body
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
475 end
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
476
477 def render(opts)
478 body = opts.delete(:body)
479 initialize_template_class(body).render(opts)
480 end
481
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
482 def template_path
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
483 "#{template_root}/#{mailer_name}"
db045db @dhh Initial
dhh authored
484 end
485
5ec990a @jamis Helper support for ActionMailer
jamis authored
486 def initialize_template_class(assigns)
f3aa7c1 @jeremy r4730@asus: jeremy | 2006-06-29 13:13:38 -0700
jeremy authored
487 ActionView::Base.new(template_path, assigns, self)
5ec990a @jamis Helper support for ActionMailer
jamis authored
488 end
489
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
490 def sort_parts(parts, order = [])
491 order = order.collect { |s| s.downcase }
492
493 parts = parts.sort do |a, b|
494 a_ct = a.content_type.downcase
495 b_ct = b.content_type.downcase
496
497 a_in = order.include? a_ct
498 b_in = order.include? b_ct
499
500 s = case
501 when a_in && b_in
502 order.index(a_ct) <=> order.index(b_ct)
503 when a_in
504 -1
505 when b_in
506 1
507 else
508 a_ct <=> b_ct
509 end
510
511 # reverse the ordering because parts that come last are displayed
512 # first in mail clients
513 (s * -1)
514 end
515
516 parts
517 end
518
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
519 def create_mail
520 m = TMail::Mail.new
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored
521
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
522 m.subject, = quote_any_if_necessary(charset, subject)
523 m.to, m.from = quote_any_address_if_necessary(charset, recipients, from)
524 m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
525 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
526
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
527 m.mime_version = mime_version unless mime_version.nil?
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
528 m.date = sent_on.to_time rescue sent_on if sent_on
529 headers.each { |k, v| m[k] = v }
db045db @dhh Initial
dhh authored
530
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
531 real_content_type, ctype_attrs = parse_content_type
532
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
533 if @parts.empty?
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
534 m.set_content_type(real_content_type, nil, ctype_attrs)
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored
535 m.body = Utils.normalize_new_lines(body)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
536 else
537 if String === body
538 part = TMail::Mail.new
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored
539 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
540 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
541 part.set_content_disposition "inline"
542 m.parts << part
543 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
544
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
545 @parts.each do |p|
546 part = (TMail::Mail === p ? p : p.to_mail(self))
547 m.parts << part
548 end
3b4eb7a @jamis Allow specific "multipart/xxx" content-type to be set on multipart me…
jamis authored
549
49efa02 @jamis Nil charset caused subject line to be improperly quoted in implicitly…
jamis authored
550 if real_content_type =~ /multipart/
551 ctype_attrs.delete "charset"
552 m.set_content_type(real_content_type, nil, ctype_attrs)
553 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
554 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
555
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
556 @mail = m
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
557 end
558
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
559 def perform_delivery_smtp(mail)
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
560 destinations = mail.destinations
561 mail.ready_to_send
562
416091d @NZKoz Fix missed smtp_session changes to base.rb.
NZKoz authored
563 Net::SMTP.start(smtp_settings[:address], smtp_settings[:port], smtp_settings[:domain],
564 smtp_settings[:user_name], smtp_settings[:password], smtp_settings[:authentication]) do |smtp|
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
565 smtp.sendmail(mail.encoded, mail.from, destinations)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
566 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
567 end
568
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
569 def perform_delivery_sendmail(mail)
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to al…
NZKoz authored
570 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
571 sm.print(mail.encoded.gsub(/\r/, ''))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
572 sm.flush
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
573 end
574 end
575
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
576 def perform_delivery_test(mail)
577 deliveries << mail
578 end
db045db @dhh Initial
dhh authored
579 end
580 end
Something went wrong with that request. Please try again.