Skip to content
This repository has been archived by the owner on Dec 11, 2021. It is now read-only.

Commit

Permalink
Handle invalid Unicode in message body.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Ferrier committed Aug 30, 2020
1 parent edb1c3c commit a23fac9
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion email2pdf
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ def handle_plain_message_body(part):
logger.info("Determined email is plain text with charset " + str(charset))

if isinstance(payload, bytes):
payload = str(payload, charset)
try:
payload = str(payload, charset)
except UnicodeDecodeError:
logger.warning("UnicodeDecodeErrors in plain message body, using 'replace'")
payload = str(payload, charset, errors='replace')

payload = "\n".join( # Wrap long lines, individually
[ textwrap.fill(line, width=80) for line in payload.splitlines() ]
Expand Down

0 comments on commit a23fac9

Please sign in to comment.