diff options
-rw-r--r-- | mail2news.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/mail2news.py b/mail2news.py index 0091cf4..ced7897 100644 --- a/mail2news.py +++ b/mail2news.py @@ -14,7 +14,7 @@ Gets news email and sends it via SMTP. class mail2news is hopefully conform to rfc850. """ -from io import StringIO +import io from collections import OrderedDict import email import logging @@ -23,6 +23,7 @@ import os from re import findall from socket import gethostbyaddr, gethostname import sys +import tempfile #logging.basicConfig(level=logging.DEBUG) @@ -180,6 +181,14 @@ class mail2news(object): if self.verbose: server.set_debuglevel(2) - server.post(io.BytesIO(self.message.as_bytes())) + msg_bytes = self.message.as_bytes() + try: + server.post(io.BytesIO(msg_bytes)) + except UnicodeEncodeError: + with tempfile.NamedTemporaryFile(suffix="eml", prefix="failed_msg", + delete=False) as tmpf: + tmpf.write(msg_bytes) + logging.info(f"failed file name = {tmpf.name}") + logging.exception("Failed to convert message!") server.quit() |