From 53f97b330e7b0cdcc07e57aad06fd45c8a0f1e5d Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Wed, 30 Nov 2022 18:31:52 +0100 Subject: When sending nntp message fails, log the message to temporary file. --- mail2news.py | 13 +++++++++++-- 1 file 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() -- cgit