From 89f55e55877ef65e516cdff11336c8f488fff0af Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 21 Mar 2019 10:10:24 -0700 Subject: [PATCH] Fix regression in content_type for text part after converted to multipart This was broken by 404ecdd290f89658fb05da02ea5b6139fd82ebdb. It removed the setting of the default :content_type entry for the text part, before copying the content fields to the text part. This puts back the setting of the default :content_type entry for the text part. In 2.7.1: ```ruby m = Mail.new m.body 'c' m.add_file :filename=>'a.txt', :content=>'b' m.parts.first.content_type =~ /text\/plain/ # => 0 ``` Before this commit: ```ruby m = Mail.new m.body 'c' m.add_file :filename=>'a.txt', :content=>'b' m.parts.first.content_type =~ /text\/plain/ # => nil m.encoded m.parts.first.content_type =~ /text\/plain/ # => 0 ``` After this commit: ```ruby m = Mail.new m.body 'c' m.add_file :filename=>'a.txt', :content=>'b' m.parts.first.content_type =~ /text\/plain/ # => 0 ``` --- lib/mail/message.rb | 2 +- spec/mail/message_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/mail/message.rb b/lib/mail/message.rb index 73eb1e3e4..74656df82 100644 --- a/lib/mail/message.rb +++ b/lib/mail/message.rb @@ -1771,7 +1771,7 @@ def add_file(values) private_constant :MULTIPART_CONVERSION_CONTENT_FIELDS if respond_to?(:private_constant) def convert_to_multipart - text_part = Mail::Part.new(:body => body.decoded) + text_part = Mail::Part.new(:content_type => 'text/plain;', :body => body.decoded) MULTIPART_CONVERSION_CONTENT_FIELDS.each do |field_name| if value = send(field_name) diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb index e2405cf58..6b7d834bc 100644 --- a/spec/mail/message_spec.rb +++ b/spec/mail/message_spec.rb @@ -1507,6 +1507,28 @@ def message_headers_should_match(message, other) end end + describe "add_file sets content_type from attachment" do + it "should work if set before body" do + m = Mail.new + m.add_file :filename=>'a.txt', :content=>'b' + m.body 'c' + expect(m.parts.first.content_type).to match(/text\/plain/) + expect(m.parts.first.body.encoded).to eq('b') + expect(m.parts.last.content_type).to match(/text\/plain/) + expect(m.parts.last.body.encoded).to eq('c') + end + + it "should work if set after body" do + m = Mail.new + m.body 'c' + m.add_file :filename=>'a.txt', :content=>'b' + expect(m.parts.first.content_type).to match(/text\/plain/) + expect(m.parts.first.body.encoded).to eq('c') + expect(m.parts.last.content_type).to match(/text\/plain/) + expect(m.parts.last.body.encoded).to eq('b') + end + end + describe "content-transfer-encoding" do it "should use 7bit for only US-ASCII chars" do