Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 762 lines (665 sloc) 30.04 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
09f798b @josevalim Instrument create and deliver in ActionMailer.
josevalim authored
259 extend AbstractController::RenderingController
260
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
261 if Object.const_defined?(:ActionController)
262 include ActionController::UrlWriter
5314abe Keep ActionMailer using the old layouts code until it gets refactored.
Yehuda Katz + Carl Lerche authored
263 include ActionController::Layout
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
264 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
265
c927aa0 @dhh Updated documentation
dhh authored
266 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored
267
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
268 class_inheritable_accessor :view_paths
441e4e2 @svenfuchs load plugin view_paths to action_mailer view_paths and make action_ma…
svenfuchs authored
269 self.view_paths = []
270
db045db @dhh Initial
dhh authored
271 cattr_accessor :logger
272
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
273 @@smtp_settings = {
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
274 :address => "localhost",
275 :port => 25,
276 :domain => 'localhost.localdomain',
277 :user_name => nil,
278 :password => nil,
279 :authentication => nil,
280 :enable_starttls_auto => true,
db045db @dhh Initial
dhh authored
281 }
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to al…
NZKoz authored
282 cattr_accessor :smtp_settings
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
283
27bb903 @NZKoz Rename server_settings to smtp_settings, add sendmail_settings to al…
NZKoz authored
284 @@sendmail_settings = {
285 :location => '/usr/sbin/sendmail',
286 :arguments => '-i -t'
287 }
288 cattr_accessor :sendmail_settings
db045db @dhh Initial
dhh authored
289
fbe6c3c @edavis10 Adds a :file delivery_method to save email to a file on disk
edavis10 authored
290 @@file_settings = {
291 :location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
292 }
293
294 cattr_accessor :file_settings
295
db045db @dhh Initial
dhh authored
296 @@raise_delivery_errors = true
297 cattr_accessor :raise_delivery_errors
298
57cde63 @dhh Allow ActionMailer subclasses to individually set their delivery meth…
dhh authored
299 superclass_delegating_accessor :delivery_method
300 self.delivery_method = :smtp
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
301
db045db @dhh Initial
dhh authored
302 @@perform_deliveries = true
303 cattr_accessor :perform_deliveries
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
304
db045db @dhh Initial
dhh authored
305 @@deliveries = []
306 cattr_accessor :deliveries
307
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
308 @@default_charset = "utf-8"
309 cattr_accessor :default_charset
310
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
311 @@default_content_type = "text/plain"
312 cattr_accessor :default_content_type
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
313
45b5555 @jamis Make mime version default to 1.0. closes #2323
jamis authored
314 @@default_mime_version = "1.0"
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
315 cattr_accessor :default_mime_version
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
316
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
317 @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
318 cattr_accessor :default_implicit_parts_order
319
28bf2fa @josh Protect body ivar from being clobbered by the mailer template assigns
josh authored
320 cattr_reader :protected_instance_variables
321 @@protected_instance_variables = %w(@body)
322
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
323 # Specify the BCC addresses for the message
324 adv_attr_accessor :bcc
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
325
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
326 # Define the body of the message. This is either a Hash (in which case it
327 # specifies the variables to pass to the template when it is rendered),
328 # or a string, in which case it specifies the actual text of the message.
329 adv_attr_accessor :body
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
330
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
331 # Specify the CC addresses for the message.
332 adv_attr_accessor :cc
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
333
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
334 # Specify the charset to use for the message. This defaults to the
335 # +default_charset+ specified for ActionMailer::Base.
336 adv_attr_accessor :charset
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
337
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
338 # Specify the content type for the message. This defaults to <tt>text/plain</tt>
339 # in most cases, but can be automatically set in some situations.
340 adv_attr_accessor :content_type
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
341
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
342 # Specify the from address for the message.
343 adv_attr_accessor :from
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
344
cf6299d Add ActionMailer#reply_to. [#245 state:resolved]
Adam authored
345 # Specify the address (if different than the "from" address) to direct
346 # replies to this message.
347 adv_attr_accessor :reply_to
348
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
349 # Specify additional headers to be added to the message.
350 adv_attr_accessor :headers
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
351
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
352 # Specify the order in which parts should be sorted, based on content-type.
353 # This defaults to the value for the +default_implicit_parts_order+.
354 adv_attr_accessor :implicit_parts_order
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
355
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
356 # Defaults to "1.0", but may be explicitly given if needed.
357 adv_attr_accessor :mime_version
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
358
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
359 # The recipient addresses for the message, either as a string (for a single
360 # address) or an array (for multiple addresses).
361 adv_attr_accessor :recipients
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
362
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
363 # The date on which the message was sent. If not set (the default), the
364 # header will be set by the delivery agent.
365 adv_attr_accessor :sent_on
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
366
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
367 # Specify the subject of the message.
368 adv_attr_accessor :subject
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
369
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
370 # Specify the template name to use for current message. This is the "base"
371 # template name, without the extension or directory, and may be used to
372 # have multiple mailer methods share the same template.
373 adv_attr_accessor :template
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
374
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
375 # Override the mailer name, which defaults to an inflected version of the
376 # mailer's class name. If you want to use a template in a non-standard
377 # location, you can use this to specify that location.
378 def mailer_name(value = nil)
379 if value
380 self.mailer_name = value
381 else
382 self.class.mailer_name
383 end
384 end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
385
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
386 def mailer_name=(value)
387 self.class.mailer_name = value
388 end
389
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
390 # The mail object instance referenced by this mailer.
391 attr_reader :mail
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
392 attr_reader :template_name, :default_template_name, :action_name
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
393
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
394 def controller_path
395 self.class.controller_path
396 end
397
398 def formats
399 @template.formats
400 end
401
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
402 class << self
bd5ed65 @technoweenie Update ActionMailer so it treats ActionView the same way that ActionC…
technoweenie authored
403 attr_writer :mailer_name
404
405 def mailer_name
406 @mailer_name ||= name.underscore
407 end
408
409 # for ActionView compatibility
410 alias_method :controller_name, :mailer_name
411 alias_method :controller_path, :mailer_name
412
3cf773b @floehopper ActionMailer should respond_to? to methods handled by method_missing …
floehopper authored
413 def respond_to?(method_symbol, include_private = false) #:nodoc:
414 matches_dynamic_method?(method_symbol) || super
415 end
416
417 def method_missing(method_symbol, *parameters) #:nodoc:
a62e9e9 @mocoso Fix for ActionMailer::Base.method_missing so that it raises NoMethodE…
mocoso authored
418 if match = matches_dynamic_method?(method_symbol)
419 case match[1]
420 when 'create' then new(match[2], *parameters).mail
421 when 'deliver' then new(match[2], *parameters).deliver!
422 when 'new' then nil
c65075f @mocoso Fixed method_missing for ActionMailer so it no longer matches methods…
mocoso authored
423 else super
a62e9e9 @mocoso Fix for ActionMailer::Base.method_missing so that it raises NoMethodE…
mocoso authored
424 end
425 else
426 super
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
427 end
428 end
429
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
430 # Receives a raw email, parses it into an email object, decodes it,
431 # instantiates a new mailer, and passes the email object to the mailer
98dc582 @lifo Merge docrails.
lifo authored
432 # object's +receive+ method. If you want your mailer to be able to
433 # process incoming messages, you'll need to implement a +receive+
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
434 # method that accepts the email object as a parameter:
435 #
436 # class MyMailer < ActionMailer::Base
437 # def receive(mail)
438 # ...
439 # end
440 # end
441 def receive(raw_email)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
442 logger.info "Received mail:\n #{raw_email}" unless logger.nil?
443 mail = TMail::Mail.parse(raw_email)
444 mail.base64_decode
445 new.receive(mail)
446 end
447
448 # Deliver the given mail object directly. This can be used to deliver
449 # a preconstructed mail object, like:
450 #
451 # email = MyMailer.create_some_mail(parameters)
452 # email.set_some_obscure_header "frobnicate"
453 # MyMailer.deliver(email)
454 def deliver(mail)
455 new.deliver!(mail)
456 end
271404e @jeremy Register alternative template engines using ActionMailer::Base.regist…
jeremy authored
457
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
458 def template_root
459 self.view_paths && self.view_paths.first
460 end
461
61c90a4 @NZKoz Reapply the TemplateFinder first applied in [8669] then reverted in […
NZKoz authored
462 def template_root=(root)
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
463 self.view_paths = ActionView::Base.process_view_paths(root)
61c90a4 @NZKoz Reapply the TemplateFinder first applied in [8669] then reverted in […
NZKoz authored
464 end
3cf773b @floehopper ActionMailer should respond_to? to methods handled by method_missing …
floehopper authored
465
466 private
467 def matches_dynamic_method?(method_name) #:nodoc:
468 method_name = method_name.to_s
c65075f @mocoso Fixed method_missing for ActionMailer so it no longer matches methods…
mocoso authored
469 /^(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
470 end
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
471 end
472
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
473 # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
474 # will be initialized according to the named method. If not, the mailer will
475 # remain uninitialized (useful when you only need to invoke the "receive"
476 # method, for instance).
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
477 def initialize(method_name=nil, *parameters) #:nodoc:
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
478 create!(method_name, *parameters) if method_name
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
479 end
480
481 # Initialize the mailer via the given +method_name+. The body will be
482 # rendered and a new TMail::Mail object created.
2789b5d @dhh Tuned documentation for release (AM)
dhh authored
483 def create!(method_name, *parameters) #:nodoc:
09f798b @josevalim Instrument create and deliver in ActionMailer.
josevalim authored
484 ActiveSupport::Orchestra.instrument(:create_mail, :name => method_name) do
485 initialize_defaults(method_name)
486 __send__(method_name, *parameters)
487
488 # If an explicit, textual body has not been set, we check assumptions.
489 unless String === @body
490 # First, we look to see if there are any likely templates that match,
491 # which include the content-type in their file name (i.e.,
492 # "the_template_file.text.html.erb", etc.). Only do this if parts
493 # have not already been specified manually.
494 # if @parts.empty?
495 template_root.find_all(@template, {}, template_path).each do |template|
496 @parts << Part.new(
497 :content_type => template.mime_type ? template.mime_type.to_s : "text/plain",
498 :disposition => "inline",
499 :charset => charset,
500 :body => render_template(template, @body)
501 )
502 end
503
504 if @parts.size > 1
505 @content_type = "multipart/alternative" if @content_type !~ /^multipart/
506 @parts = sort_parts(@parts, @implicit_parts_order)
507 end
508 # end
509
510 # Then, if there were such templates, we check to see if we ought to
511 # also render a "normal" template (without the content type). If a
512 # normal template exists (or if there were no implicit parts) we render
513 # it.
514 # ====
515 # TODO: Revisit this
516 # template_exists = @parts.empty?
517 # template_exists ||= template_root.find("#{mailer_name}/#{@template}")
518 # @body = render_message(@template, @body) if template_exists
519
520 # Finally, if there are other message parts and a textual body exists,
521 # we shift it onto the front of the parts and set the body to nil (so
522 # that create_mail doesn't try to render it in addition to the parts).
523 # ====
524 # TODO: Revisit this
525 # if !@parts.empty? && String === @body
526 # @parts.unshift Part.new(:charset => charset, :body => @body)
527 # @body = nil
528 # end
529 end
db045db @dhh Initial
dhh authored
530
09f798b @josevalim Instrument create and deliver in ActionMailer.
josevalim authored
531 # If this is a multipart e-mail add the mime_version if it is not
532 # already set.
533 @mime_version ||= "1.0" if !@parts.empty?
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
534
09f798b @josevalim Instrument create and deliver in ActionMailer.
josevalim authored
535 # build the mail object itself
536 @mail = create_mail
537 end
538
539 @mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
540 end
db045db @dhh Initial
dhh authored
541
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
542 # Delivers a TMail::Mail object. By default, it delivers the cached mail
98dc582 @lifo Merge docrails.
lifo authored
543 # 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
544 # no alternate has been given as the parameter, this will fail.
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored
545 def deliver!(mail = @mail)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored
546 raise "no mail object available for delivery!" unless mail
09f798b @josevalim Instrument create and deliver in ActionMailer.
josevalim authored
547
fbd3eb7 @jeremy Less verbose mail logging: just recipients for :info log level; the w…
jeremy authored
548 unless logger.nil?
be36455 @jeremy Ruby 1.9 compat: use Array() instead of #to_a.
jeremy authored
549 logger.info "Sent mail to #{Array(recipients).join(', ')}"
fbd3eb7 @jeremy Less verbose mail logging: just recipients for :info log level; the w…
jeremy authored
550 logger.debug "\n#{mail.encoded}"
551 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
552
09f798b @josevalim Instrument create and deliver in ActionMailer.
josevalim authored
553 ActiveSupport::Orchestra.instrument(:deliver_mail, :mail => @mail) do
554 begin
555 __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
556 rescue Exception => e # Net::SMTP errors or sendmail pipe errors
557 raise e if raise_delivery_errors
558 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
559 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
560
09f798b @josevalim Instrument create and deliver in ActionMailer.
josevalim authored
561 mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
562 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
563
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
564 private
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
565 # Set up the default values for the various instance variables of this
566 # mailer. Subclasses may override this method to provide different
567 # defaults.
568 def initialize_defaults(method_name)
95e8740 @dhh Allow Mailers to have custom initialize methods that set default inst…
dhh authored
569 @charset ||= @@default_charset.dup
570 @content_type ||= @@default_content_type.dup
571 @implicit_parts_order ||= @@default_implicit_parts_order.dup
572 @template ||= method_name
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
573 @default_template_name = @action_name = @template
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
574 @mailer_name ||= self.class.name.underscore
95e8740 @dhh Allow Mailers to have custom initialize methods that set default inst…
dhh authored
575 @parts ||= []
576 @headers ||= {}
577 @body ||= {}
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003 …
jamis authored
578 @mime_version = @@default_mime_version.dup if @@default_mime_version
5fdc33c @mrduncan Default sent_on time to now in ActionMailer
mrduncan authored
579 @sent_on ||= Time.now
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored
580 end
581
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
582 def render_template(template, body)
ab83db9 Fixes ActionMailer to work with the ActionView refactoring
Yehuda Katz + Carl Lerche authored
583 if template.respond_to?(:mime_type)
584 @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
585 end
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
586
587 @template = initialize_template_class(body)
ab83db9 Fixes ActionMailer to work with the ActionView refactoring
Yehuda Katz + Carl Lerche authored
588 layout = _pick_layout(layout, true) unless
589 ActionController::Base.exempt_from_layout.include?(template.handler)
0612fd0 @wycats Replace _render_template_with_layout with _render_template since the …
wycats authored
590 @template._render_template(template, layout, {})
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
591 ensure
592 @current_template_content_type = nil
593 end
594
595 def render_message(method_name, body)
7461227 @josh Ruby 1.9: Call join on template_root instead of to_s
josh authored
596 render :file => method_name, :body => body
1d4554d ActionMailer should respect content type when choosing layouts
Colin Curtin authored
597 ensure
598 @current_template_content_type = nil
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
599 end
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored
600
601 def render(opts)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
602 layout, file = opts.delete(:layout), opts[:file]
603
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
604 begin
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
605 @template = initialize_template_class(opts.delete(:body))
606
607 if file
608 prefix = mailer_name unless file =~ /\//
010a0c9 @wycats Rename find_by_parts and find_by_parts? to find and exists?
wycats authored
609 template = view_paths.find(file, {:formats => formats}, prefix)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
610 end
611
612 layout = _pick_layout(layout,
ab83db9 Fixes ActionMailer to work with the ActionView refactoring
Yehuda Katz + Carl Lerche authored
613 !template || ActionController::Base.exempt_from_layout.include?(template.handler))
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
614
615 if template
0612fd0 @wycats Replace _render_template_with_layout with _render_template since the …
wycats authored
616 @template._render_template(template, layout, opts)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
617 elsif inline = opts[:inline]
618 @template._render_inline(inline, layout, opts)
619 end
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
620 end
621 end
622
623 def default_template_format
1d4554d ActionMailer should respect content type when choosing layouts
Colin Curtin authored
624 if @current_template_content_type
625 Mime::Type.lookup(@current_template_content_type).to_sym
626 else
627 :html
628 end
6228220 @dhh Revert "Revert "Add layout functionality to mailers.""
dhh authored
629 end
630
cb21db1 @josh Treat ActionMailer template_root as a view path set internally to avo…
josh authored
631 def template_root
632 self.class.template_root
633 end
634
635 def template_root=(root)
636 self.class.template_root = root
637 end
638
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
639 def template_path
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
640 "#{mailer_name}"
db045db @dhh Initial
dhh authored
641 end
642
5ec990a @jamis Helper support for ActionMailer
jamis authored
643 def initialize_template_class(assigns)
441e4e2 @svenfuchs load plugin view_paths to action_mailer view_paths and make action_ma…
svenfuchs authored
644 template = ActionView::Base.new(self.class.view_paths, assigns, self)
eb9af20 @wycats Begin unifying the interface between ActionController and ActionView
wycats authored
645 template.formats = [default_template_format]
1d4554d ActionMailer should respect content type when choosing layouts
Colin Curtin authored
646 template
5ec990a @jamis Helper support for ActionMailer
jamis authored
647 end
648
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
649 def sort_parts(parts, order = [])
650 order = order.collect { |s| s.downcase }
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
651
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored
652 parts = parts.sort do |a, b|
653 a_ct = a.content_type.downcase
654 b_ct = b.content_type.downcase
655
656 a_in = order.include? a_ct
657 b_in = order.include? b_ct
658
659 s = case
660 when a_in && b_in
661 order.index(a_ct) <=> order.index(b_ct)
662 when a_in
663 -1
664 when b_in
665 1
666 else
667 a_ct <=> b_ct
668 end
669
670 # reverse the ordering because parts that come last are displayed
671 # first in mail clients
672 (s * -1)
673 end
674
675 parts
676 end
677
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
678 def create_mail
679 m = TMail::Mail.new
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored
680
cf6299d Add ActionMailer#reply_to. [#245 state:resolved]
Adam authored
681 m.subject, = quote_any_if_necessary(charset, subject)
682 m.to, m.from = quote_any_address_if_necessary(charset, recipients, from)
683 m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
684 m.cc = quote_address_if_necessary(cc, charset) unless cc.nil?
685 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
686 m.mime_version = mime_version unless mime_version.nil?
cf6299d Add ActionMailer#reply_to. [#245 state:resolved]
Adam authored
687 m.date = sent_on.to_time rescue sent_on if sent_on
688
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
689 headers.each { |k, v| m[k] = v }
db045db @dhh Initial
dhh authored
690
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
691 real_content_type, ctype_attrs = parse_content_type
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
692
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
693 if @parts.empty?
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
694 m.set_content_type(real_content_type, nil, ctype_attrs)
e201fc7 @josh use autoload instead of explicit requires for ActionMailer
josh authored
695 m.body = normalize_new_lines(body)
0a132c2 Refactor ActionView::Path
Yehuda Katz + Carl Lerche authored
696 elsif @parts.size == 1 && @parts.first.parts.empty?
697 m.set_content_type(real_content_type, nil, ctype_attrs)
698 m.body = normalize_new_lines(@parts.first.body)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
699 else
700 if String === body
701 part = TMail::Mail.new
e201fc7 @josh use autoload instead of explicit requires for ActionMailer
josh authored
702 part.body = normalize_new_lines(body)
db0e8ff @jamis Parse content-type apart before using it so that sub-parts of the hea…
jamis authored
703 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
704 part.set_content_disposition "inline"
705 m.parts << part
706 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
707
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
708 @parts.each do |p|
709 part = (TMail::Mail === p ? p : p.to_mail(self))
710 m.parts << part
711 end
3b0e1d9 @josh Prefer string core_ext inflector methods over directly accessing Infl…
josh authored
712
49efa02 @jamis Nil charset caused subject line to be improperly quoted in implicitly…
jamis authored
713 if real_content_type =~ /multipart/
714 ctype_attrs.delete "charset"
715 m.set_content_type(real_content_type, nil, ctype_attrs)
716 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
717 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
718
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
719 @mail = m
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
720 end
721
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
722 def perform_delivery_smtp(mail)
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
723 destinations = mail.destinations
724 mail.ready_to_send
1d57ccb 1.9 compatibility - don't pass an array as the from address as this e…
Friedrich Göpel authored
725 sender = (mail['return-path'] && mail['return-path'].spec) || mail['from']
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
726
732c724 @granth Turn on STARTTLS if it is available in Net::SMTP (added in Ruby 1.8.7…
granth authored
727 smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
c2e7851 @josevalim Add ActionMailer::Base#enable_starttls_auto option for enabling/disab…
josevalim authored
728 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
729 smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
730 smtp_settings[:authentication]) do |smtp|
5c0d38f @dhh Fixed that a return-path header would be ignored (closes #7572) [joost]
dhh authored
731 smtp.sendmail(mail.encoded, sender, destinations)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
732 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
733 end
734
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
735 def perform_delivery_sendmail(mail)
77730f7 @NZKoz Avoid modifying the sendmail_settings hash when using the return path…
NZKoz authored
736 sendmail_args = sendmail_settings[:arguments]
737 sendmail_args += " -f \"#{mail['return-path']}\"" if mail['return-path']
738 IO.popen("#{sendmail_settings[:location]} #{sendmail_args}","w+") do |sm|
bb62568 @dhh Fix problem with sendmail delivery where headers should be delimited …
dhh authored
739 sm.print(mail.encoded.gsub(/\r/, ''))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
740 sm.flush
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
741 end
742 end
743
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
744 def perform_delivery_test(mail)
745 deliveries << mail
746 end
fbe6c3c @edavis10 Adds a :file delivery_method to save email to a file on disk
edavis10 authored
747
748 def perform_delivery_file(mail)
749 FileUtils.mkdir_p file_settings[:location]
750
751 (mail.to + mail.cc + mail.bcc).uniq.each do |to|
752 File.open(File.join(file_settings[:location], to), 'a') { |f| f.write(mail) }
753 end
754 end
db045db @dhh Initial
dhh authored
755 end
e201fc7 @josh use autoload instead of explicit requires for ActionMailer
josh authored
756
757 Base.class_eval do
758 include Helpers
759 helper MailHelper
760 end
db045db @dhh Initial
dhh authored
761 end
Something went wrong with that request. Please try again.