Skip to content

Commit

Permalink
empty charset fix
Browse files Browse the repository at this point in the history
References #1300
  • Loading branch information
ahorek authored and jeremy committed Jun 4, 2019
1 parent d4f552b commit 955f292
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/mail/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ def charset
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
Expand Down
4 changes: 3 additions & 1 deletion lib/mail/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,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

Expand Down
33 changes: 33 additions & 0 deletions spec/mail/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,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 '<h1>This is HTML</h1>'
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 "content-transfer-encoding" do

it "should use 7bit for only US-ASCII chars" do
Expand Down

0 comments on commit 955f292

Please sign in to comment.