aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2022-11-30 18:31:52 +0100
committerMatěj Cepl <mcepl@cepl.eu>2023-05-25 10:56:29 +0200
commit53f97b330e7b0cdcc07e57aad06fd45c8a0f1e5d (patch)
tree02f5542bfe3421ca9b36ce0c41b976d4a6ea67da
parent974100c32e45bd07ced44e9b179558a184fbb7d7 (diff)
downloadpygn-53f97b330e7b0cdcc07e57aad06fd45c8a0f1e5d.tar.gz
When sending nntp message fails, log the message to temporary file.
-rw-r--r--mail2news.py13
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()