FIX: Correct corrupt encoding in emails containing attachments

This commit is contained in:
Martin Brennan 2020-09-29 14:10:57 +10:00 committed by GitHub
parent ffdcac9dfc
commit a8ed0b4612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -332,8 +332,11 @@ module Email
content = Mail::Part.new do
content_type "multipart/alternative"
part html_part
part text_part
# we have to re-specify the charset and give the part the decoded body
# here otherwise the parts will get encoded with US-ASCII which makes
# a bunch of characters not render correctly in the email
part content_type: "text/html; charset=utf-8", body: html_part.body.decoded
part content_type: "text/plain; charset=utf-8", body: text_part.body.decoded
end
@message.parts.unshift(content)

View File

@ -378,7 +378,7 @@ describe Email::Sender do
fab!(:post) { Fabricate(:post) }
fab!(:reply) do
raw = <<~RAW
Hello world!
Hello world! Its a great day!
#{UploadMarkdown.new(small_pdf).attachment_markdown}
#{UploadMarkdown.new(large_pdf).attachment_markdown}
#{UploadMarkdown.new(image).image_markdown}
@ -460,6 +460,13 @@ describe Email::Sender do
expect(message.html_part.body).to include("embedded-secure-image")
expect(message.attachments.length).to eq(4)
end
it "uses correct UTF-8 encoding for the body of the email" do
Email::Sender.new(message, :valid_type).send
expect(message.html_part.body).not_to include("Itâ\u0080\u0099s")
expect(message.html_part.body).to include("Its")
expect(message.html_part.charset.downcase).to eq("utf-8")
end
end
end
@ -491,6 +498,13 @@ describe Email::Sender do
expect(message.parts[0].content_type).to start_with("multipart/alternative")
expect(message.parts[0].parts.size).to eq(2)
end
it "uses correct UTF-8 encoding for the body of the email" do
Email::Sender.new(message, :valid_type).send
expect(message.html_part.body).not_to include("Itâ\u0080\u0099s")
expect(message.html_part.body).to include("Its")
expect(message.html_part.charset.downcase).to eq("utf-8")
end
end
context 'with a deleted post' do