diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index 87b612b7e5..804f8f821a 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -402,10 +402,14 @@ def attempt_to_find_original_attachment_attributes(mail, body:, nested: false) all_attributes = get_attachment_attributes(mail) def calculate_hexdigest(body) - # ensure bodies have the same line endings - Digest::MD5.hexdigest(Mail::Utilities.binary_unsafe_to_lf( - body.rstrip - )) + # ensure bodies have the same line endings and are encoded the same + Digest::MD5.hexdigest( + Mail::Utilities.binary_unsafe_to_lf( + convert_string_to_utf8( + body.rstrip + ).string + ) + ) end hexdigest = calculate_hexdigest(body) diff --git a/spec/lib/mail_handler/backends/mail_backend_spec.rb b/spec/lib/mail_handler/backends/mail_backend_spec.rb index c5dda569b6..37237ab3a9 100644 --- a/spec/lib/mail_handler/backends/mail_backend_spec.rb +++ b/spec/lib/mail_handler/backends/mail_backend_spec.rb @@ -461,6 +461,7 @@ add_file filename: 'lf.txt', content: "bar\nbar" add_file filename: 'crlf-non-ascii.txt', content: "Aberdâr\r\n" add_file filename: 'lf-non-ascii.txt', content: "Aberdâr\n" + add_file filename: 'latin1.txt', content: "naïve" add_file filename: 'mail.eml', content: mail_attachment add_file filename: 'uuencoded.eml', content: mail_with_uuencoded end @@ -492,6 +493,11 @@ it { is_expected.to include(body: "Aberdâr\n") } end + context 'when binary body encoded as Latin 1 / ISO-8859-1' do + let(:body) { "na\xEFve".b } + it { is_expected.to include(body: "naïve") } + end + context 'when attached email headers are different' do let(:body) do <<~EML