failover when ascii encoding fails

This commit is contained in:
Son NK 2020-11-09 20:58:39 +01:00
parent 44c3ac1741
commit b2f9479bce

View file

@ -622,7 +622,11 @@ def parseaddr_unicode(addr) -> (str, str):
def copy(msg: Message) -> Message:
"""return a copy of message"""
return email.message_from_bytes(to_bytes(msg))
try:
return email.message_from_bytes(to_bytes(msg))
except UnicodeEncodeError:
LOG.warning("to_bytes() fails, try string")
return email.message_from_string(msg.as_string())
def to_bytes(msg: Message):