diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG
index 4ae813e8aa..d46727fe91 100644
--- a/actionmailer/CHANGELOG
+++ b/actionmailer/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Multipart messages specify a MIME-Version header automatically #2003 [John Long]
+
* Add a unified render method to ActionMailer (delegates to ActionView::Base#render)
* Move mailer initialization to a separate (overridable) method, so that subclasses may alter the various defaults #1727
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 96574fcfd9..089b2ef0f6 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -110,6 +110,9 @@ module ActionMailer #:nodoc:
# pick a different charset from inside a method with @charset.
# * default_content_type - The default content type used for main part of the message. Defaults to "text/plain". You
# can also pick a different content type from inside a method with @content_type.
+ # * default_mime_version - The default mime version used for the message. Defaults to nil. You
+ # can also pick a different value from inside a method with @mime_version. When multipart messages are in
+ # use, @mime_version will be set to "1.0" if it is not set inside a method.
# * default_implicit_parts_order - When a message is built implicitly (i.e. multiple parts are assemble from templates
# which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
# ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client
@@ -150,13 +153,16 @@ module ActionMailer #:nodoc:
@@default_content_type = "text/plain"
cattr_accessor :default_content_type
+
+ @@default_mime_version = nil
+ cattr_accessor :default_mime_version
@@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ]
cattr_accessor :default_implicit_parts_order
adv_attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers,
:bcc, :cc, :charset, :content_type, :implicit_parts_order,
- :template, :mailer_name
+ :template, :mailer_name, :mime_version
attr_reader :mail
@@ -206,7 +212,8 @@ module ActionMailer #:nodoc:
unless String === @body
# First, we look to see if there are any likely templates that match,
# which include the content-type in their file name (i.e.,
- # "the_template_file.text.html.rhtml", etc.).
+ # "the_template_file.text.html.rhtml", etc.). Only do this if parts
+ # have not already been specified manually.
if @parts.empty?
templates = Dir.glob("#{template_path}/#{@template}.*")
templates.each do |path|
@@ -239,6 +246,10 @@ module ActionMailer #:nodoc:
end
end
+ # If this is a multipart e-mail add the mime_version if it is not
+ # already set.
+ @mime_version ||= "1.0" if !@parts.empty?
+
# build the mail object itself
@mail = create_mail
end
@@ -273,6 +284,7 @@ module ActionMailer #:nodoc:
@parts = []
@headers = {}
@body = {}
+ @mime_version = @@default_mime_version.dup if @@default_mime_version
end
def render_message(method_name, body)
@@ -329,6 +341,7 @@ module ActionMailer #:nodoc:
m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil?
m.cc = quote_address_if_necessary(cc, charset) unless cc.nil?
+ m.mime_version = mime_version unless mime_version.nil?
m.date = sent_on.to_time rescue sent_on if sent_on
headers.each { |k, v| m[k] = v }
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb
index 97bdcaa1f8..50eb1984b5 100755
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/mail_service_test.rb
@@ -94,6 +94,23 @@ class TestMailer < ActionMailer::Base
@charset = "utf-8"
end
+ def multipart_with_mime_version(recipient)
+ recipients recipient
+ subject "multipart with mime_version"
+ from "test@example.com"
+ sent_on Time.local(2004, 12, 12)
+ mime_version "1.1"
+ content_type "multipart/alternative"
+
+ part "text/plain" do |p|
+ p.body = "blah"
+ end
+
+ part "text/html" do |p|
+ p.body = "blah"
+ end
+ end
+
def explicitly_multipart_example(recipient, ct=nil)
recipients recipient
subject "multipart example"
@@ -239,6 +256,7 @@ class ActionMailerTest < Test::Unit::TestCase
expected.body = "Hello there, \n\nMr. #{@recipient}"
expected.from = "system@loudthinking.com"
expected.date = Time.local(2004, 12, 12)
+ expected.mime_version = nil
created = nil
assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
@@ -552,6 +570,11 @@ EOF
assert_nothing_raised { mail.body }
end
+ def test_multipart_with_mime_version
+ mail = TestMailer.create_multipart_with_mime_version(@recipient)
+ assert_equal "1.1", mail.mime_version
+ end
+
def test_explicitly_multipart_messages
mail = TestMailer.create_explicitly_multipart_example(@recipient)
assert_equal 3, mail.parts.length
@@ -585,6 +608,7 @@ EOF
def test_implicitly_multipart_messages
mail = TestMailer.create_implicitly_multipart_example(@recipient)
assert_equal 3, mail.parts.length
+ assert_equal "1.0", mail.mime_version
assert_equal "multipart/alternative", mail.content_type
assert_equal "text/yaml", mail.parts[0].content_type
assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset")