Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty charset fix #1300

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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