From b2a7c7298fd0910224672ed1bd4ae5a6c4ecc1b0 Mon Sep 17 00:00:00 2001 From: pavel Date: Tue, 18 Dec 2018 02:17:42 +0100 Subject: [PATCH] empty charset fix --- lib/mail/header.rb | 6 +++++- lib/mail/message.rb | 4 +++- spec/mail/message_spec.rb | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/mail/header.rb b/lib/mail/header.rb index 4436ad449..9edc29f1c 100644 --- a/lib/mail/header.rb +++ b/lib/mail/header.rb @@ -169,7 +169,11 @@ def []=(name, value) def charset=(val) params = self[:content_type].parameters rescue nil if params - params[:charset] = val + if val + params[:charset] = val + else + params.delete(:charset) + end end @charset = val end diff --git a/lib/mail/message.rb b/lib/mail/message.rb index da01b4971..f5176ac85 100644 --- a/lib/mail/message.rb +++ b/lib/mail/message.rb @@ -1477,7 +1477,9 @@ def add_charset warning = "Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.\n" warn(warning) end - header[:content_type].parameters['charset'] = @charset + if @charset + header[:content_type].parameters['charset'] = @charset + end end end diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb index 8419c5a56..1432b360b 100644 --- a/spec/mail/message_spec.rb +++ b/spec/mail/message_spec.rb @@ -1437,6 +1437,39 @@ def message_headers_should_match(message, other) expect(mail.parts.last.content_transfer_encoding).to match(/7bit|8bit|binary/) end + describe 'charset=' do + before do + @mail = Mail.new do + to 'mikel@test.lindsaar.net' + from 'bob@test.lindsaar.net' + subject 'Multipart email' + text_part do + body 'This is plain text' + end + html_part do + content_type 'text/html; charset=UTF-8' + body '

This is HTML

' + end + end + end + + it "should not add an empty charset header" do + @mail.charset = nil + + expect(@mail.multipart?).to eq true + expect(@mail.parts.count).to eq 2 + expect(@mail.encoded.scan(/charset=UTF-8/).count).to eq 2 + end + + it "should remove the charset header" do + @mail.charset = 'iso-8859-1' + @mail.charset = nil + + expect(@mail.encoded.scan(/charset=UTF-8/).count).to eq 2 + expect(@mail.encoded.scan(/charset=iso-8859-1/).count).to eq 0 + end + end + describe "convert_to_multipart" do subject do read_fixture('emails', 'attachment_emails', 'attachment_only_email.eml').tap(&:convert_to_multipart)