Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 313 lines (273 sloc) 12.124 kB
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
1 require 'action_mailer/adv_attr_accessor'
2 require 'action_mailer/part'
5ddffc8 @jamis Allow for nested parts in multipart mails #1570 [Flurin Egger]
jamis authored
3 require 'action_mailer/part_container'
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
4 require 'tmail/net'
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
5
db045db @dhh Initial
dhh authored
6 module ActionMailer #:nodoc:
7 # Usage:
8 #
9 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
10 # # Set up properties
11 # # (Properties can also be specified via accessor methods
12 # # i.e. self.subject = "foo") and instance variables (@subject = "foo").
13 # def signup_notification(recipient)
14 # recipients recipient.email_address_with_name
15 # subject "New account information"
16 # body Hash.new("account" => recipient)
17 # from "system@example.com"
18 # end
19 #
20 # # explicitly specify multipart messages
21 # def signup_notification(recipient)
22 # recipients recipient.email_address_with_name
23 # subject "New account information"
24 # from "system@example.com"
25 #
26 # part :content_type => "text/html",
27 # :body => render_message("signup-as-html", :account => recipient)
28 #
29 # part "text/plain" do |p|
30 # p.body = render_message("signup-as-plain", :account => recipient)
31 # p.transfer_encoding = "base64"
32 # end
33 # end
34 #
35 # # attachments
36 # def signup_notification(recipient)
37 # recipients recipient.email_address_with_name
38 # subject "New account information"
39 # from "system@example.com"
40 #
41 # attachment :content_type => "image/jpeg",
42 # :body => File.read("an-image.jpg")
43 #
44 # attachment "application/pdf" do |a|
45 # a.body = generate_your_pdf_here()
46 # end
db045db @dhh Initial
dhh authored
47 # end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
48 #
49 # # implicitly multipart messages
50 # def signup_notification(recipient)
51 # recipients recipient.email_address_with_name
52 # subject "New account information"
53 # from "system@example.com"
54 # body(:account => "recipient")
55 #
56 # # ActionMailer will automatically detect and use multipart templates,
57 # # where each template is named after the name of the action, followed
58 # # by the content type. Each such detected template will be added as
59 # # a separate part to the message.
60 # #
61 # # for example, if the following templates existed:
62 # # * signup_notification.text.plain.rhtml
63 # # * signup_notification.text.html.rhtml
64 # # * signup_notification.text.xml.rxml
65 # # * signup_notification.text.x-yaml.rhtml
66 # #
67 # # Each would be rendered and added as a separate part to the message,
68 # # with the corresponding content type. The same body hash is passed to
69 # # each template.
db045db @dhh Initial
dhh authored
70 # end
71 # end
72 #
73 # # After this post_notification will look for "templates/application_mailer/post_notification.rhtml"
74 # ApplicationMailer.template_root = "templates"
75 #
76 # ApplicationMailer.create_comment_notification(david, hello_world) # => a tmail object
77 # ApplicationMailer.deliver_comment_notification(david, hello_world) # sends the email
c927aa0 @dhh Updated documentation
dhh authored
78 #
79 # = Configuration options
80 #
81 # These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
82 #
83 # * <tt>template_root</tt> - template root determines the base from which template references will be made.
84 #
85 # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
86 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
87 #
88 # * <tt>server_settings</tt> - Allows detailed configuration of the server:
89 # * <tt>:address</tt> Allows you to use a remote mail server. Just change it away from it's default "localhost" setting.
90 # * <tt>:port</tt> On the off change that your mail server doesn't run on port 25, you can change it.
91 # * <tt>:domain</tt> If you need to specify a HELO domain, you can do it here.
92 # * <tt>:user_name</tt> If your mail server requires authentication, set the username and password in these two settings.
93 # * <tt>:password</tt> If your mail server requires authentication, set the username and password in these two settings.
94 # * <tt>:authentication</tt> If your mail server requires authentication, you need to specify the authentication type here.
95 # This is a symbol and one of :plain, :login, :cram_md5
96 #
97 # * <tt>raise_delivery_errors</tt> - whether or not errors should be raised if the email fails to be delivered.
98 #
99 # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.
100 # Sendmail is assumed to be present at "/usr/sbin/sendmail".
101 #
102 # * <tt>perform_deliveries</tt> - Determines whether deliver_* methods are actually carried out. By default they are,
103 # but this can be turned off to help functional testing.
104 #
105 # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful
106 # for unit and functional testing.
af56c80 @dhh Updated documentation
dhh authored
107 #
108 # * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
109 # pick a different charset from inside a method with <tt>@charset</tt>.
db045db @dhh Initial
dhh authored
110 class Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
111 include ActionMailer::AdvAttrAccessor
5ddffc8 @jamis Allow for nested parts in multipart mails #1570 [Flurin Egger]
jamis authored
112 include ActionMailer::PartContainer
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
113
c927aa0 @dhh Updated documentation
dhh authored
114 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored
115
116 cattr_accessor :template_root
117 cattr_accessor :logger
118
119 @@server_settings = {
120 :address => "localhost",
121 :port => 25,
122 :domain => 'localhost.localdomain',
123 :user_name => nil,
124 :password => nil,
125 :authentication => nil
126 }
127 cattr_accessor :server_settings
128
129 @@raise_delivery_errors = true
130 cattr_accessor :raise_delivery_errors
131
132 @@delivery_method = :smtp
133 cattr_accessor :delivery_method
134
135 @@perform_deliveries = true
136 cattr_accessor :perform_deliveries
137
138 @@deliveries = []
139 cattr_accessor :deliveries
140
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
141 @@default_charset = "utf-8"
142 cattr_accessor :default_charset
143
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
144 @@default_content_type = "text/plain"
145 cattr_accessor :default_content_type
146
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
147 adv_attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers,
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored
148 :bcc, :cc, :charset, :content_type, :template
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
149
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
150 attr_reader :mail
151
152 # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
153 # will be initialized according to the named method. If not, the mailer will
154 # remain uninitialized (useful when you only need to invoke the "receive"
155 # method, for instance).
156 def initialize(method_name=nil, *parameters)
157 create!(method_name, *parameters) if method_name
158 end
159
160 # Initialize the mailer via the given +method_name+. The body will be
161 # rendered and a new TMail::Mail object created.
162 def create!(method_name, *parameters)
163 @bcc = @cc = @from = @recipients = @sent_on = @subject = nil
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
164 @charset = @@default_charset.dup
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
165 @content_type = @@default_content_type.dup
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored
166 @template = method_name
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
167 @parts = []
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored
168 @headers = {}
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
169 @body = {}
170
171 send(method_name, *parameters)
172
173 # If an explicit, textual body has not been set, we check assumptions.
174 unless String === @body
175 # First, we look to see if there are any likely templates that match,
176 # which include the content-type in their file name (i.e.,
177 # "the_template_file.text.html.rhtml", etc.).
178 if @parts.empty?
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored
179 templates = Dir.glob("#{template_path}/#{@template}.*")
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
180 templates.each do |path|
181 type = (File.basename(path).split(".")[1..-2] || []).join("/")
182 next if type.empty?
183 @parts << Part.new(:content_type => type,
053cb22 @jamis Use the real charset for parts generated by a template, instead of th…
jamis authored
184 :disposition => "inline", :charset => charset,
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
185 :body => render_message(File.basename(path).split(".")[0..-2].join('.'), @body))
186 end
187 end
db045db @dhh Initial
dhh authored
188
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
189 # Then, if there were such templates, we check to see if we ought to
190 # also render a "normal" template (without the content type). If a
191 # normal template exists (or if there were no implicit parts) we render
192 # it.
193 template_exists = @parts.empty?
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored
194 template_exists ||= Dir.glob("#{template_path}/#{@template}.*").any? { |i| i.split(".").length == 2 }
195 @body = render_message(@template, @body) if template_exists
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
196
197 # Finally, if there are other message parts and a textual body exists,
198 # we shift it onto the front of the parts and set the body to nil (so
199 # that create_mail doesn't try to render it in addition to the parts).
200 if !@parts.empty? && String === @body
201 @parts.unshift Part.new(:charset => charset, :body => @body)
202 @body = nil
425aa50 @dhh Added receiver method for incoming email
dhh authored
203 end
db045db @dhh Initial
dhh authored
204 end
205
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
206 # build the mail object itself
207 @mail = create_mail
208 end
db045db @dhh Initial
dhh authored
209
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
210 # Delivers the cached TMail::Mail object. If no TMail::Mail object has been
211 # created (via the #create! method, for instance) this will fail.
212 def deliver!
213 raise "no mail object available for delivery!" unless @mail
214 logger.info "Sent mail:\n #{mail.encoded}" unless logger.nil?
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
215
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
216 begin
217 send("perform_delivery_#{delivery_method}", @mail) if perform_deliveries
218 rescue Object => e
219 raise e if raise_delivery_errors
220 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
221
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
222 return @mail
223 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
224
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
225 private
226 def render_message(method_name, body)
5ec990a @jamis Helper support for ActionMailer
jamis authored
227 initialize_template_class(body).render_file(method_name)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
228 end
229
230 def template_path
231 template_root + "/" + Inflector.underscore(self.class.name)
db045db @dhh Initial
dhh authored
232 end
233
5ec990a @jamis Helper support for ActionMailer
jamis authored
234 def initialize_template_class(assigns)
235 ActionView::Base.new(template_path, assigns, self)
236 end
237
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
238 def create_mail
239 m = TMail::Mail.new
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored
240
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
241 m.subject, = quote_any_if_necessary(charset, subject)
242 m.to, m.from = quote_any_address_if_necessary(charset, recipients, from)
243 m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
244 m.cc = quote_address_if_necessary(cc, charset) unless cc.nil?
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored
245
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
246 m.date = sent_on.to_time rescue sent_on if sent_on
247 headers.each { |k, v| m[k] = v }
db045db @dhh Initial
dhh authored
248
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
249 if @parts.empty?
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
250 m.set_content_type content_type, nil, { "charset" => charset }
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
251 m.body = body
252 else
253 if String === body
254 part = TMail::Mail.new
255 part.body = body
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored
256 part.set_content_type content_type, nil, { "charset" => charset }
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
257 part.set_content_disposition "inline"
258 m.parts << part
259 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored
260
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
261 @parts.each do |p|
262 part = (TMail::Mail === p ? p : p.to_mail(self))
263 m.parts << part
264 end
3b4eb7a @jamis Allow specific "multipart/xxx" content-type to be set on multipart me…
jamis authored
265
266 m.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
267 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
268
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
269 @mail = m
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
270 end
271
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
272 def perform_delivery_smtp(mail)
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
273 destinations = mail.destinations
274 mail.ready_to_send
275
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
276 Net::SMTP.start(server_settings[:address], server_settings[:port], server_settings[:domain],
277 server_settings[:user_name], server_settings[:password], server_settings[:authentication]) do |smtp|
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored
278 smtp.sendmail(mail.encoded, mail.from, destinations)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
279 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
280 end
281
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
282 def perform_delivery_sendmail(mail)
283 IO.popen("/usr/sbin/sendmail -i -t","w+") do |sm|
284 sm.print(mail.encoded)
285 sm.flush
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
286 end
287 end
288
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
289 def perform_delivery_test(mail)
290 deliveries << mail
291 end
292
293 class << self
294 def method_missing(method_symbol, *parameters)#:nodoc:
295 case method_symbol.id2name
296 when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
297 when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
5ec990a @jamis Helper support for ActionMailer
jamis authored
298 when "new" then nil
299 else super
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored
300 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored
301 end
302
425aa50 @dhh Added receiver method for incoming email
dhh authored
303 def receive(raw_email)
304 logger.info "Received mail:\n #{raw_email}" unless logger.nil?
a2eb744 @dhh Added automatic decoding of base64 bodies #1214 [Jamis Buck]
dhh authored
305 mail = TMail::Mail.parse(raw_email)
306 mail.base64_decode
307 new.receive(mail)
425aa50 @dhh Added receiver method for incoming email
dhh authored
308 end
309
db045db @dhh Initial
dhh authored
310 end
311 end
312 end
Something went wrong with that request. Please try again.