Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 jamis authored
2  actionmailer/CHANGELOG
View
@@ -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
17 actionmailer/lib/action_mailer/base.rb
View
@@ -110,6 +110,9 @@ module ActionMailer #:nodoc:
# pick a different charset from inside a method with <tt>@charset</tt>.
# * <tt>default_content_type</tt> - 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 <tt>@content_type</tt>.
+ # * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to nil. You
+ # can also pick a different value from inside a method with <tt>@mime_version</tt>. When multipart messages are in
+ # use, <tt>@mime_version</tt> will be set to "1.0" if it is not set inside a method.
# * <tt>default_implicit_parts_order</tt> - 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 @@ class Base
@@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 @@ def create!(method_name, *parameters) #: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 @@ def create!(method_name, *parameters) #: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 @@ def initialize_defaults(method_name)
@parts = []
@headers = {}
@body = {}
+ @mime_version = @@default_mime_version.dup if @@default_mime_version
end
def render_message(method_name, body)
@@ -329,6 +341,7 @@ def create_mail
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 }
24 actionmailer/test/mail_service_test.rb
View
@@ -94,6 +94,23 @@ def utf8_body(recipient)
@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 = "<b>blah</b>"
+ end
+ end
+
def explicitly_multipart_example(recipient, ct=nil)
recipients recipient
subject "multipart example"
@@ -239,6 +256,7 @@ def test_signed_up
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 @@ def test_decode_message_with_incorrect_charset
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 @@ def test_explicitly_multipart_with_invalid_content_type
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")
Please sign in to comment.
Something went wrong with that request. Please try again.