email.message_from_string can also throw KeyError when 'content-transfer-encoding' is absent

This commit is contained in:
Son NK 2020-11-10 17:26:24 +01:00
parent 632a5bbbc8
commit 4666d21f63

View file

@ -636,7 +636,7 @@ def copy(msg: Message) -> Message:
try:
# prefer the unicode way
return email.message_from_string(msg.as_string())
except UnicodeEncodeError:
except (UnicodeEncodeError, ValueError):
LOG.warning("as_string() fails, try to_bytes")
return email.message_from_bytes(to_bytes(msg))