Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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