Skip to content

Commit

Permalink
Fix regression in content_type for text part after converted to multi…
Browse files Browse the repository at this point in the history
…part

This was broken by 404ecdd.  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
```
  • Loading branch information
jeremyevans committed Dec 3, 2022
1 parent 800022a commit 89f55e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mail/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions spec/mail/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 89f55e5

Please sign in to comment.