Improved error handling of emails with invalid byte sequence for utf8 encoding.

This commit is contained in:
Martin Edenhofer 2017-06-23 13:57:24 +02:00
parent 691b63d011
commit 88e3d9e574
3 changed files with 43 additions and 0 deletions

View file

@ -151,6 +151,7 @@ class Channel::EmailParser
if data[:body].empty? && mail.text_part if data[:body].empty? && mail.text_part
data[:body] = mail.text_part.body.decoded data[:body] = mail.text_part.body.decoded
data[:body] = Encode.conv(mail.text_part.charset, data[:body]) data[:body] = Encode.conv(mail.text_part.charset, data[:body])
data[:body] = data[:body].to_s.force_encoding('utf-8')
if !data[:body].valid_encoding? if !data[:body].valid_encoding?
data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?') data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')

16
test/fixtures/mail60.box vendored Normal file
View file

@ -0,0 +1,16 @@
From: Martin Edenhofer <martin@example.com>
Content-Type: multipart/alternative; boundary="Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394"
Subject: abc
Date: Fri, 4 May 2012 14:01:03 +0200
Message-Id: <BC182994-03FA-4DC5-8202-98CBFACA0887@example.com>
To: metest@znuny.com
Mime-Version: 1.0 (Apple Message framework v1257)
--Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394
Content-Transfer-Encoding: base64
Content-Type: text/plain;
charset=iso-gb2312
SGVyZSBpdCBnb2VzIC0gw6TDtsO8IC0g5beu5Ye65Lq6SGVyZSBpdCBnb2VzIC0g5Pb8IC0gaGkgrQ==
--Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394--

View file

@ -2449,6 +2449,32 @@ Some Text',
], ],
}, },
}, },
{
data: IO.binread('test/fixtures/mail60.box'),
success: true,
result: {
0 => {
priority: '2 normal',
title: 'abc',
},
1 => {
from: 'Martin Edenhofer <martin@example.com>',
sender: 'Customer',
type: 'email',
body: 'Here it goes - ?????? - ?????????Here it goes - ??? - hi ?',
},
},
verify: {
users: [
{
firstname: 'Martin',
lastname: 'Edenhofer',
fullname: 'Martin Edenhofer',
email: 'martin@example.com',
},
],
},
},
] ]
assert_process(files) assert_process(files)
end end