fix decoding of sender e-mail if badly encoded

This commit is contained in:
Stephane Blondon
2018-03-21 18:24:59 +01:00
parent 79ce81aa9d
commit cbb46ef078
2 changed files with 4 additions and 1 deletions

View File

@@ -33,7 +33,7 @@ def decode_mail_header(value, default_charset='us-ascii'):
return str_decode(str_encode(value, default_charset, 'replace'), default_charset)
else:
for index, (text, charset) in enumerate(headers):
logger.debug("Mail header no. {}: {} encoding {}".format(index, str_decode(text, charset or 'utf-8'), charset))
logger.debug("Mail header no. {}: {} encoding {}".format(index, str_decode(text, charset or 'utf-8', 'replace'), charset))
try:
headers[index] = str_decode(text, charset or default_charset,
'replace')

View File

@@ -336,6 +336,9 @@ class TestParser(unittest.TestCase):
from_message_object = email.message_from_string("From: John Smith <johnsmith@gmail.com>")
self.assertEqual([{'email': 'johnsmith@gmail.com', 'name': 'John Smith'}], get_mail_addresses(from_message_object, 'from'))
invalid_encoding_in_from_message_object = email.message_from_string("From: =?UTF-8?Q?C=E4cilia?= <caciliahxg827m@example.org>")
self.assertEqual([{'email': 'caciliahxg827m@example.org', 'name': 'C<EFBFBD>cilia'}], get_mail_addresses(invalid_encoding_in_from_message_object, 'from'))
def test_parse_email_with_policy(self):
if not SMTP:
return