Skip to content

Commit

Permalink
empty charset fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ahorek authored and jeremy committed Jun 4, 2019
1 parent 2950963 commit b2a7c72
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 @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/mail/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 @@ -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 '<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 "convert_to_multipart" do
subject do
read_fixture('emails', 'attachment_emails', 'attachment_only_email.eml').tap(&:convert_to_multipart)
Expand Down

0 comments on commit b2a7c72

Please sign in to comment.