From 2691fff217a47a3caa4126f78729f0ea846b15cf Mon Sep 17 00:00:00 2001 From: Son Date: Mon, 1 Nov 2021 10:11:36 +0100 Subject: [PATCH] handle UnicodeDecodeError in replace() --- app/email_utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/email_utils.py b/app/email_utils.py index 4aa467f4..b04d29a1 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -967,7 +967,11 @@ def replace(msg: Message, old, new) -> Message: if encoding == EmailEncoding.QUOTED: LOG.d("handle quoted-printable replace %s -> %s", old, new) # first decode the payload - new_payload = quopri.decodestring(payload).decode("utf-8") + try: + new_payload = quopri.decodestring(payload).decode("utf-8") + except UnicodeDecodeError: + LOG.w("cannot decode payload:%s", payload) + return msg # then replace the old text new_payload = new_payload.replace(old, new) clone_msg = copy(msg)