Skip to content
Newer
Older
100644 463 lines (399 sloc) 18 KB
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
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 Jul 1, 2005
3 require 'action_mailer/part_container'
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored Jul 6, 2005
4 require 'action_mailer/utils'
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored Jun 6, 2005
5 require 'tmail/net'
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
6
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored Oct 16, 2005
7 module ActionMailer
db045db @dhh Initial
dhh authored Nov 24, 2004
8 # Usage:
9 #
10 # class ApplicationMailer < ActionMailer::Base
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
11 # # Set up properties
475bd74 @dhh Fix docs (closes #2598)
dhh authored Oct 26, 2005
12 # # Properties can also be specified via accessor methods
13 # # (i.e. self.subject = "foo") and instance variables (@subject = "foo").
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
14 # def signup_notification(recipient)
15 # recipients recipient.email_address_with_name
16 # subject "New account information"
2789b5d @dhh Tuned documentation for release (AM)
dhh authored Jul 6, 2005
17 # body { "account" => recipient }
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
18 # from "system@example.com"
19 # end
20 #
21 # # explicitly specify multipart messages
22 # def signup_notification(recipient)
23 # recipients recipient.email_address_with_name
24 # subject "New account information"
25 # from "system@example.com"
26 #
27 # part :content_type => "text/html",
28 # :body => render_message("signup-as-html", :account => recipient)
29 #
30 # part "text/plain" do |p|
31 # p.body = render_message("signup-as-plain", :account => recipient)
32 # p.transfer_encoding = "base64"
33 # end
34 # end
35 #
36 # # attachments
37 # def signup_notification(recipient)
38 # recipients recipient.email_address_with_name
39 # subject "New account information"
40 # from "system@example.com"
41 #
42 # attachment :content_type => "image/jpeg",
43 # :body => File.read("an-image.jpg")
44 #
45 # attachment "application/pdf" do |a|
46 # a.body = generate_your_pdf_here()
47 # end
db045db @dhh Initial
dhh authored Nov 24, 2004
48 # end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
49 #
50 # # implicitly multipart messages
51 # def signup_notification(recipient)
52 # recipients recipient.email_address_with_name
53 # subject "New account information"
54 # from "system@example.com"
55 # body(:account => "recipient")
56 #
57 # # ActionMailer will automatically detect and use multipart templates,
58 # # where each template is named after the name of the action, followed
59 # # by the content type. Each such detected template will be added as
60 # # a separate part to the message.
61 # #
62 # # for example, if the following templates existed:
63 # # * signup_notification.text.plain.rhtml
64 # # * signup_notification.text.html.rhtml
65 # # * signup_notification.text.xml.rxml
66 # # * signup_notification.text.x-yaml.rhtml
67 # #
68 # # Each would be rendered and added as a separate part to the message,
69 # # with the corresponding content type. The same body hash is passed to
70 # # each template.
db045db @dhh Initial
dhh authored Nov 24, 2004
71 # end
72 # end
73 #
475bd74 @dhh Fix docs (closes #2598)
dhh authored Oct 26, 2005
74 # # After this, post_notification will look for "templates/application_mailer/post_notification.rhtml"
db045db @dhh Initial
dhh authored Nov 24, 2004
75 # ApplicationMailer.template_root = "templates"
76 #
77 # ApplicationMailer.create_comment_notification(david, hello_world) # => a tmail object
78 # ApplicationMailer.deliver_comment_notification(david, hello_world) # sends the email
c927aa0 @dhh Updated documentation
dhh authored Dec 16, 2004
79 #
80 # = Configuration options
81 #
82 # These options are specified on the class level, like <tt>ActionMailer::Base.template_root = "/my/templates"</tt>
83 #
84 # * <tt>template_root</tt> - template root determines the base from which template references will be made.
85 #
86 # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available.
87 # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.
88 #
89 # * <tt>server_settings</tt> - Allows detailed configuration of the server:
475bd74 @dhh Fix docs (closes #2598)
dhh authored Oct 26, 2005
90 # * <tt>:address</tt> Allows you to use a remote mail server. Just change it from its default "localhost" setting.
91 # * <tt>:port</tt> On the off chance that your mail server doesn't run on port 25, you can change it.
c927aa0 @dhh Updated documentation
dhh authored Dec 16, 2004
92 # * <tt>:domain</tt> If you need to specify a HELO domain, you can do it here.
475bd74 @dhh Fix docs (closes #2598)
dhh authored Oct 26, 2005
93 # * <tt>:user_name</tt> If your mail server requires authentication, set the username in this setting.
94 # * <tt>:password</tt> If your mail server requires authentication, set the password in this setting.
c927aa0 @dhh Updated documentation
dhh authored Dec 16, 2004
95 # * <tt>:authentication</tt> If your mail server requires authentication, you need to specify the authentication type here.
96 # This is a symbol and one of :plain, :login, :cram_md5
97 #
98 # * <tt>raise_delivery_errors</tt> - whether or not errors should be raised if the email fails to be delivered.
99 #
100 # * <tt>delivery_method</tt> - Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.
101 # Sendmail is assumed to be present at "/usr/sbin/sendmail".
102 #
103 # * <tt>perform_deliveries</tt> - Determines whether deliver_* methods are actually carried out. By default they are,
104 # but this can be turned off to help functional testing.
105 #
106 # * <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful
107 # for unit and functional testing.
af56c80 @dhh Updated documentation
dhh authored Feb 24, 2005
108 #
109 # * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored Jul 1, 2005
110 # pick a different charset from inside a method with <tt>@charset</tt>.
475bd74 @dhh Fix docs (closes #2598)
dhh authored Oct 26, 2005
111 # * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored Jul 1, 2005
112 # can also pick a different content type from inside a method with <tt>@content_type</tt>.
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored Aug 22, 2005
113 # * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to nil. You
114 # can also pick a different value from inside a method with <tt>@mime_version</tt>. When multipart messages are in
115 # use, <tt>@mime_version</tt> will be set to "1.0" if it is not set inside a method.
475bd74 @dhh Fix docs (closes #2598)
dhh authored Oct 26, 2005
116 # * <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 Jul 1, 2005
117 # which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
118 # ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client
119 # and appear last in the mime encoded message. You can also pick a different order from inside a method with
120 # <tt>@implicit_parts_order</tt>.
db045db @dhh Initial
dhh authored Nov 24, 2004
121 class Base
0fe8e3d @dhh Added a config example in README #1626 [courtenay]
dhh authored Jul 9, 2005
122 include AdvAttrAccessor, PartContainer
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
123
01b1a87 @dhh Added reusable reloading support through the inclusion of the Relodab…
dhh authored Jan 29, 2006
124 # Action Mailer subclasses should be reloaded by the dispatcher in Rails
125 # when Dependencies.mechanism = :load.
126 def self.inherited(child) #:nodoc:
127 child.send :include, Reloadable
128 super
129 end
130
c927aa0 @dhh Updated documentation
dhh authored Dec 16, 2004
131 private_class_method :new #:nodoc:
db045db @dhh Initial
dhh authored Nov 24, 2004
132
133 cattr_accessor :template_root
134 cattr_accessor :logger
135
136 @@server_settings = {
137 :address => "localhost",
138 :port => 25,
139 :domain => 'localhost.localdomain',
140 :user_name => nil,
141 :password => nil,
142 :authentication => nil
143 }
144 cattr_accessor :server_settings
145
146 @@raise_delivery_errors = true
147 cattr_accessor :raise_delivery_errors
148
149 @@delivery_method = :smtp
150 cattr_accessor :delivery_method
151
152 @@perform_deliveries = true
153 cattr_accessor :perform_deliveries
154
155 @@deliveries = []
156 cattr_accessor :deliveries
157
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored Feb 19, 2005
158 @@default_charset = "utf-8"
159 cattr_accessor :default_charset
160
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored Jun 3, 2005
161 @@default_content_type = "text/plain"
162 cattr_accessor :default_content_type
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored Aug 22, 2005
163
164 @@default_mime_version = nil
165 cattr_accessor :default_mime_version
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored Jun 3, 2005
166
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored Jul 1, 2005
167 @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
168 cattr_accessor :default_implicit_parts_order
169
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored Oct 16, 2005
170 # Specify the BCC addresses for the message
171 adv_attr_accessor :bcc
172
173 # Define the body of the message. This is either a Hash (in which case it
174 # specifies the variables to pass to the template when it is rendered),
175 # or a string, in which case it specifies the actual text of the message.
176 adv_attr_accessor :body
177
178 # Specify the CC addresses for the message.
179 adv_attr_accessor :cc
180
181 # Specify the charset to use for the message. This defaults to the
182 # +default_charset+ specified for ActionMailer::Base.
183 adv_attr_accessor :charset
184
185 # Specify the content type for the message. This defaults to <tt>text/plain</tt>
186 # in most cases, but can be automatically set in some situations.
187 adv_attr_accessor :content_type
188
189 # Specify the from address for the message.
190 adv_attr_accessor :from
191
192 # Specify additional headers to be added to the message.
193 adv_attr_accessor :headers
194
195 # Specify the order in which parts should be sorted, based on content-type.
196 # This defaults to the value for the +default_implicit_parts_order+.
197 adv_attr_accessor :implicit_parts_order
198
199 # Override the mailer name, which defaults to an inflected version of the
200 # mailer's class name. If you want to use a template in a non-standard
201 # location, you can use this to specify that location.
202 adv_attr_accessor :mailer_name
203
204 # Defaults to "1.0", but may be explicitly given if needed.
205 adv_attr_accessor :mime_version
206
207 # The recipient addresses for the message, either as a string (for a single
208 # address) or an array (for multiple addresses).
209 adv_attr_accessor :recipients
210
211 # The date on which the message was sent. If not set (the default), the
212 # header will be set by the delivery agent.
213 adv_attr_accessor :sent_on
214
215 # Specify the subject of the message.
216 adv_attr_accessor :subject
217
218 # Specify the template name to use for current message. This is the "base"
219 # template name, without the extension or directory, and may be used to
220 # have multiple mailer methods share the same template.
221 adv_attr_accessor :template
165097e @dhh Added access to custom headers, like cc, bcc, and reply-to #268 [Andr…
dhh authored Dec 7, 2004
222
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored Oct 16, 2005
223 # The mail object instance referenced by this mailer.
224 attr_reader :mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
225
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored Jul 31, 2005
226 class << self
227 def method_missing(method_symbol, *parameters)#:nodoc:
228 case method_symbol.id2name
229 when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
230 when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
231 when "new" then nil
232 else super
233 end
234 end
235
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored Oct 16, 2005
236 # Receives a raw email, parses it into an email object, decodes it,
237 # instantiates a new mailer, and passes the email object to the mailer
238 # object's #receive method. If you want your mailer to be able to
239 # process incoming messages, you'll need to implement a #receive
240 # method that accepts the email object as a parameter:
241 #
242 # class MyMailer < ActionMailer::Base
243 # def receive(mail)
244 # ...
245 # end
246 # end
247 def receive(raw_email)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored Jul 31, 2005
248 logger.info "Received mail:\n #{raw_email}" unless logger.nil?
249 mail = TMail::Mail.parse(raw_email)
250 mail.base64_decode
251 new.receive(mail)
252 end
253
254 # Deliver the given mail object directly. This can be used to deliver
255 # a preconstructed mail object, like:
256 #
257 # email = MyMailer.create_some_mail(parameters)
258 # email.set_some_obscure_header "frobnicate"
259 # MyMailer.deliver(email)
260 def deliver(mail)
261 new.deliver!(mail)
262 end
263 end
264
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
265 # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
266 # will be initialized according to the named method. If not, the mailer will
267 # remain uninitialized (useful when you only need to invoke the "receive"
268 # method, for instance).
2789b5d @dhh Tuned documentation for release (AM)
dhh authored Jul 6, 2005
269 def initialize(method_name=nil, *parameters) #:nodoc:
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
270 create!(method_name, *parameters) if method_name
271 end
272
273 # Initialize the mailer via the given +method_name+. The body will be
274 # rendered and a new TMail::Mail object created.
2789b5d @dhh Tuned documentation for release (AM)
dhh authored Jul 6, 2005
275 def create!(method_name, *parameters) #:nodoc:
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored Aug 22, 2005
276 initialize_defaults(method_name)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
277 send(method_name, *parameters)
278
279 # If an explicit, textual body has not been set, we check assumptions.
280 unless String === @body
281 # First, we look to see if there are any likely templates that match,
282 # which include the content-type in their file name (i.e.,
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored Aug 22, 2005
283 # "the_template_file.text.html.rhtml", etc.). Only do this if parts
284 # have not already been specified manually.
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
285 if @parts.empty?
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored Jun 30, 2005
286 templates = Dir.glob("#{template_path}/#{@template}.*")
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
287 templates.each do |path|
f2ee215 @jeremy Stricter matching for implicitly multipart filenames excludes files e…
jeremy authored Nov 15, 2005
288 # TODO: don't hardcode rhtml|rxml
289 next unless md = /^([^\.]+)\.([^\.]+\.[^\+]+)\.(rhtml|rxml)$/.match(File.basename(path))
290 template_name = "#{md.captures[0]}.#{md.captures[1]}"
291 content_type = md.captures[1].gsub('.', '/')
292 @parts << Part.new(:content_type => content_type,
053cb22 @jamis Use the real charset for parts generated by a template, instead of th…
jamis authored Jun 22, 2005
293 :disposition => "inline", :charset => charset,
f2ee215 @jeremy Stricter matching for implicitly multipart filenames excludes files e…
jeremy authored Nov 15, 2005
294 :body => render_message(template_name, @body))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
295 end
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored Jul 1, 2005
296 unless @parts.empty?
297 @content_type = "multipart/alternative"
8373b55 @jamis * Don't add charset to content-type header for a part that contains s…
jamis authored Sep 1, 2005
298 @charset = nil
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored Jul 1, 2005
299 @parts = sort_parts(@parts, @implicit_parts_order)
300 end
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
301 end
db045db @dhh Initial
dhh authored Nov 24, 2004
302
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
303 # Then, if there were such templates, we check to see if we ought to
304 # also render a "normal" template (without the content type). If a
305 # normal template exists (or if there were no implicit parts) we render
306 # it.
307 template_exists = @parts.empty?
34df9be @jamis Template paths with dot chars in them no longer mess up implicit temp…
jamis authored Jan 29, 2006
308 template_exists ||= Dir.glob("#{template_path}/#{@template}.*").any? { |i| File.basename(i).split(".").length == 2 }
813a8b9 @jamis Allow template to be explicitly specified #1448 [tuxie@dekadance.se]
jamis authored Jun 30, 2005
309 @body = render_message(@template, @body) if template_exists
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
310
311 # Finally, if there are other message parts and a textual body exists,
312 # we shift it onto the front of the parts and set the body to nil (so
313 # that create_mail doesn't try to render it in addition to the parts).
314 if !@parts.empty? && String === @body
315 @parts.unshift Part.new(:charset => charset, :body => @body)
316 @body = nil
425aa50 @dhh Added receiver method for incoming email
dhh authored Mar 20, 2005
317 end
db045db @dhh Initial
dhh authored Nov 24, 2004
318 end
319
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored Aug 22, 2005
320 # If this is a multipart e-mail add the mime_version if it is not
321 # already set.
322 @mime_version ||= "1.0" if !@parts.empty?
323
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
324 # build the mail object itself
325 @mail = create_mail
326 end
db045db @dhh Initial
dhh authored Nov 24, 2004
327
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored Jul 31, 2005
328 # Delivers a TMail::Mail object. By default, it delivers the cached mail
329 # object (from the #create! method). If no cached mail object exists, and
330 # no alternate has been given as the parameter, this will fail.
59f1df1 @jamis Update/extend ActionMailer documentation (rdoc)
jamis authored Oct 16, 2005
331 def deliver!(mail = @mail)
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored Jul 31, 2005
332 raise "no mail object available for delivery!" unless mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
333 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 Apr 11, 2005
334
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
335 begin
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored Jul 31, 2005
336 send("perform_delivery_#{delivery_method}", mail) if perform_deliveries
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
337 rescue Object => e
338 raise e if raise_delivery_errors
339 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored Feb 19, 2005
340
eda1719 @jamis ActionMailer::Base.deliver(email) had been accidentally removed, but …
jamis authored Jul 31, 2005
341 return mail
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
342 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored Feb 19, 2005
343
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
344 private
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored Aug 22, 2005
345 # Set up the default values for the various instance variables of this
346 # mailer. Subclasses may override this method to provide different
347 # defaults.
348 def initialize_defaults(method_name)
95e8740 @dhh Allow Mailers to have custom initialize methods that set default inst…
dhh authored Oct 26, 2005
349 @charset ||= @@default_charset.dup
350 @content_type ||= @@default_content_type.dup
351 @implicit_parts_order ||= @@default_implicit_parts_order.dup
352 @template ||= method_name
353 @mailer_name ||= Inflector.underscore(self.class.name)
354 @parts ||= []
355 @headers ||= {}
356 @body ||= {}
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored Aug 22, 2005
357 @mime_version = @@default_mime_version.dup if @@default_mime_version
f474f33 @jamis Move mailer initialization to a separate (overridable) method, so tha…
jamis authored Aug 22, 2005
358 end
359
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
360 def render_message(method_name, body)
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored Aug 22, 2005
361 render :file => method_name, :body => body
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
362 end
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored Aug 22, 2005
363
364 def render(opts)
365 body = opts.delete(:body)
366 initialize_template_class(body).render(opts)
367 end
368
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
369 def template_path
ae1e852 @jamis Add a unified render method to ActionMailer (delegates to ActionView:…
jamis authored Aug 22, 2005
370 "#{template_root}/#{mailer_name}"
db045db @dhh Initial
dhh authored Nov 24, 2004
371 end
372
5ec990a @jamis Helper support for ActionMailer
jamis authored Jun 16, 2005
373 def initialize_template_class(assigns)
374 ActionView::Base.new(template_path, assigns, self)
375 end
376
5c98152 @jamis Better multipart support with implicit multipart/alternative and sort…
jamis authored Jul 1, 2005
377 def sort_parts(parts, order = [])
378 order = order.collect { |s| s.downcase }
379
380 parts = parts.sort do |a, b|
381 a_ct = a.content_type.downcase
382 b_ct = b.content_type.downcase
383
384 a_in = order.include? a_ct
385 b_in = order.include? b_ct
386
387 s = case
388 when a_in && b_in
389 order.index(a_ct) <=> order.index(b_ct)
390 when a_in
391 -1
392 when b_in
393 1
394 else
395 a_ct <=> b_ct
396 end
397
398 # reverse the ordering because parts that come last are displayed
399 # first in mail clients
400 (s * -1)
401 end
402
403 parts
404 end
405
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
406 def create_mail
407 m = TMail::Mail.new
74a612c @dhh Added that delivery errors are caught in a way so the mail is still r…
dhh authored May 2, 2005
408
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
409 m.subject, = quote_any_if_necessary(charset, subject)
410 m.to, m.from = quote_any_address_if_necessary(charset, recipients, from)
411 m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
412 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 May 2, 2005
413
dca4d4e @jamis Multipart messages specify a MIME-Version header automatically #2003
jamis authored Aug 22, 2005
414 m.mime_version = mime_version unless mime_version.nil?
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
415 m.date = sent_on.to_time rescue sent_on if sent_on
416 headers.each { |k, v| m[k] = v }
db045db @dhh Initial
dhh authored Nov 24, 2004
417
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
418 if @parts.empty?
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored Jun 3, 2005
419 m.set_content_type content_type, nil, { "charset" => charset }
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored Jul 6, 2005
420 m.body = Utils.normalize_new_lines(body)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
421 else
422 if String === body
423 part = TMail::Mail.new
bde3df2 @jamis Correctly normalize newlines in outgoing emails before encoding the b…
jamis authored Jul 6, 2005
424 part.body = Utils.normalize_new_lines(body)
100fd72 @jamis Added a "content_type" accessor to allow messages to explicitly speci…
jamis authored Jun 3, 2005
425 part.set_content_type content_type, nil, { "charset" => charset }
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
426 part.set_content_disposition "inline"
427 m.parts << part
428 end
3fad0cd @dhh Added support for charsets for both subject and body. The default cha…
dhh authored Feb 19, 2005
429
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
430 @parts.each do |p|
431 part = (TMail::Mail === p ? p : p.to_mail(self))
432 m.parts << part
433 end
3b4eb7a @jamis Allow specific "multipart/xxx" content-type to be set on multipart me…
jamis authored Jun 24, 2005
434
435 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 May 29, 2005
436 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored Apr 11, 2005
437
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
438 @mail = m
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored Apr 11, 2005
439 end
440
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
441 def perform_delivery_smtp(mail)
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored Jun 6, 2005
442 destinations = mail.destinations
443 mail.ready_to_send
444
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
445 Net::SMTP.start(server_settings[:address], server_settings[:port], server_settings[:domain],
446 server_settings[:user_name], server_settings[:password], server_settings[:authentication]) do |smtp|
508c26c @jamis BCC headers are removed when sending via SMTP
jamis authored Jun 6, 2005
447 smtp.sendmail(mail.encoded, mail.from, destinations)
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
448 end
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored Apr 11, 2005
449 end
450
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
451 def perform_delivery_sendmail(mail)
452 IO.popen("/usr/sbin/sendmail -i -t","w+") do |sm|
bb62568 @dhh Fix problem with sendmail delivery where headers should be delimited …
dhh authored Jul 17, 2005
453 sm.print(mail.encoded.gsub(/\r/, ''))
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
454 sm.flush
256e800 @dhh Fixed quoting for all address headers, not just to #955 [Jamis Buck] …
dhh authored Apr 11, 2005
455 end
456 end
457
359caef @jamis A very thorough refactoring, resulting in new mail property setters a…
jamis authored May 29, 2005
458 def perform_delivery_test(mail)
459 deliveries << mail
460 end
db045db @dhh Initial
dhh authored Nov 24, 2004
461 end
462 end
Something went wrong with that request. Please try again.