handle the AttributeError that can also be raised by as_bytes()
This commit is contained in:
parent
99d31698e7
commit
0f7ccec51a
|
@ -841,20 +841,20 @@ def to_bytes(msg: Message):
|
||||||
"""replace Message.as_bytes() method by trying different policies"""
|
"""replace Message.as_bytes() method by trying different policies"""
|
||||||
try:
|
try:
|
||||||
return msg.as_bytes()
|
return msg.as_bytes()
|
||||||
except UnicodeEncodeError:
|
except (UnicodeEncodeError, AttributeError):
|
||||||
LOG.w("as_bytes fails with default policy, try SMTP policy")
|
LOG.w("as_bytes fails with default policy, try SMTP policy")
|
||||||
try:
|
try:
|
||||||
return msg.as_bytes(policy=policy.SMTP)
|
return msg.as_bytes(policy=policy.SMTP)
|
||||||
except UnicodeEncodeError:
|
except (UnicodeEncodeError, AttributeError):
|
||||||
LOG.w("as_bytes fails with SMTP policy, try SMTPUTF8 policy")
|
LOG.w("as_bytes fails with SMTP policy, try SMTPUTF8 policy")
|
||||||
try:
|
try:
|
||||||
return msg.as_bytes(policy=policy.SMTPUTF8)
|
return msg.as_bytes(policy=policy.SMTPUTF8)
|
||||||
except UnicodeEncodeError:
|
except (UnicodeEncodeError, AttributeError):
|
||||||
LOG.w("as_bytes fails with SMTPUTF8 policy, try converting to string")
|
LOG.w("as_bytes fails with SMTPUTF8 policy, try converting to string")
|
||||||
msg_string = msg.as_string()
|
msg_string = msg.as_string()
|
||||||
try:
|
try:
|
||||||
return msg_string.encode()
|
return msg_string.encode()
|
||||||
except UnicodeEncodeError as e:
|
except (UnicodeEncodeError, AttributeError) as e:
|
||||||
LOG.w("can't encode msg, err:%s", e)
|
LOG.w("can't encode msg, err:%s", e)
|
||||||
return msg_string.encode(errors="replace")
|
return msg_string.encode(errors="replace")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue