From a23fac96da64b8fa12a8d10e464be10c6873ced2 Mon Sep 17 00:00:00 2001 From: Andrew Ferrier Date: Sun, 30 Aug 2020 09:43:02 +0100 Subject: [PATCH] Handle invalid Unicode in message body. --- email2pdf | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/email2pdf b/email2pdf index 383080f..59f92da 100755 --- a/email2pdf +++ b/email2pdf @@ -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() ]