Skip to content
This repository
Browse code

Multipart messages specify a MIME-Version header automatically #2003

…[John Long]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2038 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit dca4d4e86d7fa753428d24a8ab33dc08a0401581 1 parent ae1e852
Jamis Buck authored August 22, 2005
2  actionmailer/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Multipart messages specify a MIME-Version header automatically #2003 [John Long]
  4
+
3 5
 * Add a unified render method to ActionMailer (delegates to ActionView::Base#render)
4 6
 
5 7
 * Move mailer initialization to a separate (overridable) method, so that subclasses may alter the various defaults #1727
17  actionmailer/lib/action_mailer/base.rb
@@ -110,6 +110,9 @@ module ActionMailer #:nodoc:
110 110
   #   pick a different charset from inside a method with <tt>@charset</tt>.
111 111
   # * <tt>default_content_type</tt> - The default content type used for main part of the message. Defaults to "text/plain". You
112 112
   #   can also pick a different content type from inside a method with <tt>@content_type</tt>. 
  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.
113 116
   # * <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assemble from templates
114 117
   #   which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
115 118
   #   ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client
@@ -150,13 +153,16 @@ class Base
150 153
 
151 154
     @@default_content_type = "text/plain"
152 155
     cattr_accessor :default_content_type
  156
+    
  157
+    @@default_mime_version = nil
  158
+    cattr_accessor :default_mime_version
153 159
 
154 160
     @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
155 161
     cattr_accessor :default_implicit_parts_order
156 162
 
157 163
     adv_attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers,
158 164
                       :bcc, :cc, :charset, :content_type, :implicit_parts_order,
159  
-                      :template, :mailer_name
  165
+                      :template, :mailer_name, :mime_version
160 166
 
161 167
     attr_reader       :mail
162 168
 
@@ -206,7 +212,8 @@ def create!(method_name, *parameters) #:nodoc:
206 212
       unless String === @body
207 213
         # First, we look to see if there are any likely templates that match,
208 214
         # which include the content-type in their file name (i.e.,
209  
-        # "the_template_file.text.html.rhtml", etc.).
  215
+        # "the_template_file.text.html.rhtml", etc.). Only do this if parts
  216
+        # have not already been specified manually.
210 217
         if @parts.empty?
211 218
           templates = Dir.glob("#{template_path}/#{@template}.*")
212 219
           templates.each do |path|
@@ -239,6 +246,10 @@ def create!(method_name, *parameters) #:nodoc:
239 246
         end
240 247
       end
241 248
 
  249
+      # If this is a multipart e-mail add the mime_version if it is not
  250
+      # already set.
  251
+      @mime_version ||= "1.0" if !@parts.empty?
  252
+
242 253
       # build the mail object itself
243 254
       @mail = create_mail
244 255
     end
@@ -273,6 +284,7 @@ def initialize_defaults(method_name)
273 284
         @parts = []
274 285
         @headers = {}
275 286
         @body = {}
  287
+        @mime_version = @@default_mime_version.dup if @@default_mime_version
276 288
       end
277 289
 
278 290
       def render_message(method_name, body)
@@ -329,6 +341,7 @@ def create_mail
329 341
         m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
330 342
         m.cc  = quote_address_if_necessary(cc, charset) unless cc.nil?
331 343
 
  344
+        m.mime_version = mime_version unless mime_version.nil?
332 345
         m.date = sent_on.to_time rescue sent_on if sent_on
333 346
         headers.each { |k, v| m[k] = v }
334 347
 
24  actionmailer/test/mail_service_test.rb
@@ -94,6 +94,23 @@ def utf8_body(recipient)
94 94
     @charset    = "utf-8"
95 95
   end
96 96
 
  97
+  def multipart_with_mime_version(recipient)
  98
+    recipients   recipient
  99
+    subject      "multipart with mime_version"
  100
+    from         "test@example.com"
  101
+    sent_on      Time.local(2004, 12, 12)
  102
+    mime_version "1.1"
  103
+    content_type "multipart/alternative"
  104
+
  105
+    part "text/plain" do |p|
  106
+      p.body = "blah"
  107
+    end
  108
+
  109
+    part "text/html" do |p|
  110
+      p.body = "<b>blah</b>"
  111
+    end
  112
+  end
  113
+
97 114
   def explicitly_multipart_example(recipient, ct=nil)
98 115
     recipients   recipient
99 116
     subject      "multipart example"
@@ -239,6 +256,7 @@ def test_signed_up
239 256
     expected.body    = "Hello there, \n\nMr. #{@recipient}"
240 257
     expected.from    = "system@loudthinking.com"
241 258
     expected.date    = Time.local(2004, 12, 12)
  259
+    expected.mime_version = nil
242 260
 
243 261
     created = nil
244 262
     assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
@@ -552,6 +570,11 @@ def test_decode_message_with_incorrect_charset
552 570
     assert_nothing_raised { mail.body }
553 571
   end
554 572
 
  573
+  def test_multipart_with_mime_version
  574
+    mail = TestMailer.create_multipart_with_mime_version(@recipient)
  575
+    assert_equal "1.1", mail.mime_version
  576
+  end
  577
+
555 578
   def test_explicitly_multipart_messages
556 579
     mail = TestMailer.create_explicitly_multipart_example(@recipient)
557 580
     assert_equal 3, mail.parts.length
@@ -585,6 +608,7 @@ def test_explicitly_multipart_with_invalid_content_type
585 608
   def test_implicitly_multipart_messages
586 609
     mail = TestMailer.create_implicitly_multipart_example(@recipient)
587 610
     assert_equal 3, mail.parts.length
  611
+    assert_equal "1.0", mail.mime_version
588 612
     assert_equal "multipart/alternative", mail.content_type
589 613
     assert_equal "text/yaml", mail.parts[0].content_type
590 614
     assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset")

0 notes on commit dca4d4e

Please sign in to comment.
Something went wrong with that request. Please try again.