Skip to content

HTTPS clone URL

Subversion checkout URL

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