Fix utf decoding bug in email parsing code

For some reason mozilla thunderbird sometimes marks email parts as 8bit
even though they are utf-8. I guess the best way to work around this is
to add a try-catch block because this really cannot be predicted.
This commit is contained in:
Timothy Hobbs
2020-01-23 15:30:08 +01:00
parent af2d0d59b7
commit 9963a3fe5d
3 changed files with 93 additions and 1 deletions

View File

@ -487,13 +487,18 @@ def object_from_message(message, queue, logger):
body.encode('utf-8')
logger.debug("Discovered plain text MIME part")
else:
try:
email_body = encoding.smart_text(part.get_payload(decode=True))
except UnicodeDecodeError:
email_body = encoding.smart_text(part.get_payload(decode=False))
payload = """
<html>
<head>
<meta charset="utf-8"/>
</head>
%s
</html>""" % encoding.smart_text(part.get_payload(decode=True))
</html>""" % email_body
files.append(
SimpleUploadedFile(_("email_html_body.html"), payload.encode("utf-8"), 'text/html')
)