diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 969aa8447..076f1d48f 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -12,6 +12,7 @@ Performance: Bugs: * Fix transfer encoding when message encoding is blank. (jakubonty, saks) +* Fix 7bit/base64 content transfer encoding mismatch. (ahorek) * Fix UTF-8 attachment filename quoting. (ahorek) diff --git a/lib/mail/body.rb b/lib/mail/body.rb index 911a0bc03..b87a2e07c 100644 --- a/lib/mail/body.rb +++ b/lib/mail/body.rb @@ -154,7 +154,13 @@ def encoded(transfer_encoding = nil) ([preamble] + encoded_parts).join(crlf_boundary) + end_boundary + epilogue.to_s else dec = Mail::Encodings.get_encoding(encoding) - enc = negotiate_best_encoding(transfer_encoding) + enc = + if Utilities.blank?(transfer_encoding) + dec + else + negotiate_best_encoding(transfer_encoding) + end + if dec.nil? # Cannot decode, so skip normalization raw_source diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb index 3c29b02bc..062b59333 100644 --- a/spec/mail/message_spec.rb +++ b/spec/mail/message_spec.rb @@ -1405,6 +1405,25 @@ def message_headers_should_match(message, other) end + it "rfc2046 can be decoded" do + mail = Mail.new + mail.body << Mail::Part.new.tap do |part| + part.content_disposition = 'attachment; filename="test.eml"' + part.content_type = 'message/rfc822' + part.body = "This is NOT plain text ASCII − かきくけこ" * 30 + end + + roundtripped = Mail.new(mail.encoded) + expect(roundtripped.content_transfer_encoding).to eq '7bit' + expect(roundtripped.parts.last.content_transfer_encoding).to eq '' + + # Check that the part's transfer encoding isn't set to 7bit, causing + # the actual content to end up encoded with base64. + expect(roundtripped.encoded).to include('NOT plain') + expect(roundtripped.content_transfer_encoding).to eq '7bit' + expect(roundtripped.parts.last.content_transfer_encoding).to eq '' + end + # https://www.ietf.org/rfc/rfc2046.txt # No encoding other than "7bit", "8bit", or "binary" is permitted for # the body of a "message/rfc822" entity.