1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Parse content-type apart before using it so that sub-parts of the header can be set correctly (closes #2918)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3959 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2006-03-18 23:53:07 +00:00
parent 9c9069a675
commit db0e8ff1c3
5 changed files with 41 additions and 11 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Parse content-type apart before using it so that sub-parts of the header can be set correctly #2918 [Jamis Buck]
* Make custom headers work in subparts #4034 [elan@bluemandrill.com] * Make custom headers work in subparts #4034 [elan@bluemandrill.com]
* Template paths with dot chars in them no longer mess up implicit template selection for multipart messages #3332 [Chad Fowler] * Template paths with dot chars in them no longer mess up implicit template selection for multipart messages #3332 [Chad Fowler]

View file

@ -413,14 +413,16 @@ module ActionMailer
m.date = sent_on.to_time rescue sent_on if sent_on m.date = sent_on.to_time rescue sent_on if sent_on
headers.each { |k, v| m[k] = v } headers.each { |k, v| m[k] = v }
real_content_type, ctype_attrs = parse_content_type
if @parts.empty? if @parts.empty?
m.set_content_type content_type, nil, { "charset" => charset } m.set_content_type(real_content_type, nil, ctype_attrs)
m.body = Utils.normalize_new_lines(body) m.body = Utils.normalize_new_lines(body)
else else
if String === body if String === body
part = TMail::Mail.new part = TMail::Mail.new
part.body = Utils.normalize_new_lines(body) part.body = Utils.normalize_new_lines(body)
part.set_content_type content_type, nil, { "charset" => charset } part.set_content_type(real_content_type, nil, ctype_attrs)
part.set_content_disposition "inline" part.set_content_disposition "inline"
m.parts << part m.parts << part
end end
@ -430,7 +432,7 @@ module ActionMailer
m.parts << part m.parts << part
end end
m.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/ m.set_content_type(real_content_type, nil, ctype_attrs) if real_content_type =~ /multipart/
end end
@mail = m @mail = m

View file

@ -56,6 +56,8 @@ module ActionMailer
def to_mail(defaults) def to_mail(defaults)
part = TMail::Mail.new part = TMail::Mail.new
real_content_type, ctype_attrs = parse_content_type(defaults)
if @parts.empty? if @parts.empty?
part.content_transfer_encoding = transfer_encoding || "quoted-printable" part.content_transfer_encoding = transfer_encoding || "quoted-printable"
case (transfer_encoding || "").downcase case (transfer_encoding || "").downcase
@ -71,20 +73,20 @@ module ActionMailer
# Also don't set filename and name when there is none (like in # Also don't set filename and name when there is none (like in
# non-attachment parts) # non-attachment parts)
if content_disposition == "attachment" if content_disposition == "attachment"
part.set_content_type(content_type || defaults.content_type, nil, ctype_attrs.delete "charset"
squish("charset" => nil, "name" => filename)) part.set_content_type(real_content_type, nil,
squish("name" => filename).merge(ctype_attrs))
part.set_content_disposition(content_disposition, part.set_content_disposition(content_disposition,
squish("filename" => filename)) squish("filename" => filename).merge(ctype_attrs))
else else
part.set_content_type(content_type || defaults.content_type, nil, part.set_content_type(real_content_type, nil, ctype_attrs)
"charset" => (charset || defaults.charset))
part.set_content_disposition(content_disposition) part.set_content_disposition(content_disposition)
end end
else else
if String === body if String === body
part = TMail::Mail.new part = TMail::Mail.new
part.body = body part.body = body
part.set_content_type content_type, nil, { "charset" => charset } part.set_content_type(real_content_type, nil, ctype_attrs)
part.set_content_disposition "inline" part.set_content_disposition "inline"
m.parts << part m.parts << part
end end
@ -94,15 +96,16 @@ module ActionMailer
part.parts << prt part.parts << prt
end end
part.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/ part.set_content_type(real_content_type, nil, ctype_attrs) if real_content_type =~ /multipart/
end end
@headers.each { |k,v| part[k] = v } headers.each { |k,v| part[k] = v }
part part
end end
private private
def squish(values={}) def squish(values={})
values.delete_if { |k,v| v.nil? } values.delete_if { |k,v| v.nil? }
end end

View file

@ -38,5 +38,14 @@ module ActionMailer
part(params, &block) part(params, &block)
end end
private
def parse_content_type(defaults=nil)
return [defaults && defaults.content_type, {}] if content_type.blank?
ctype, *attrs = content_type.split(/;\s*/)
attrs = attrs.inject({}) { |h,s| k,v = s.split(/=/, 2); h[k] = v; h }
[ctype, {"charset" => charset || defaults && defaults.charset}.merge(attrs)]
end
end end
end end

View file

@ -246,6 +246,14 @@ class TestMailer < ActionMailer::Base
body "testing" body "testing"
end end
def custom_content_type_attributes
recipients "no.one@nowhere.test"
subject "custom content types"
from "some.one@somewhere.test"
content_type "text/plain; format=flowed"
body "testing"
end
class <<self class <<self
attr_accessor :received_body attr_accessor :received_body
end end
@ -787,5 +795,11 @@ EOF
mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient) mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient)
assert_equal 2, mail.parts.length assert_equal 2, mail.parts.length
end end
def test_custom_content_type_attributes
mail = TestMailer.create_custom_content_type_attributes
assert_match %r{format=flowed}, mail['content-type'].to_s
assert_match %r{charset=utf-8}, mail['content-type'].to_s
end
end end