Skip to content

Commit b91524f

Browse files
committed
Merged [7368] from rjsparks@nostrum.com: Improves the detail in the messages sent when creating tickets about failed sent email.
Changes a locally defined exception name to look less like one already defined in smtplib. This is related to ticket ietf-tools#1208. - Legacy-Id: 7382 Note: SVN reference [7368] has been migrated to Git commit 08f1323
1 parent dbc82f2 commit b91524f

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

ietf/utils/mail.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def add_headers(msg):
4040
msg['From'] = settings.DEFAULT_FROM_EMAIL
4141
return msg
4242

43-
class SMTPRefusedRecipients(smtplib.SMTPException):
43+
class SMTPSomeRefusedRecipients(smtplib.SMTPException):
4444

4545
def __init__(self, message, original_msg, refusals):
4646
smtplib.SMTPException.__init__(self, message)
@@ -99,11 +99,12 @@ def send_smtp(msg, bcc=None):
9999
server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
100100
unhandled = server.sendmail(frm, to, msg.as_string())
101101
if unhandled != {}:
102-
raise SMTPRefusedRecipients(message="%d addresses were refused"%len(unhandled),original_msg=msg,refusals=unhandled)
102+
raise SMTPSomeRefusedRecipients(message="%d addresses were refused"%len(unhandled),original_msg=msg,refusals=unhandled)
103103
except Exception as e:
104104
# need to improve log message
105105
log("Exception while trying to send email from '%s' to %s subject '%s'" % (frm, to, msg.get('Subject', '[no subject]')))
106106
if isinstance(e, smtplib.SMTPException):
107+
e.original_msg=msg
107108
raise
108109
else:
109110
raise smtplib.SMTPException({'really': sys.exc_info()[0], 'value': sys.exc_info()[1], 'tb': traceback.format_tb(sys.exc_info()[2])})
@@ -271,8 +272,8 @@ def log_smtp_exception(e):
271272

272273

273274
log("SMTP Exception: %s : %s" % (extype,value))
274-
if isinstance(e,SMTPRefusedRecipients):
275-
log(" Refused: %s"%(e.summary_refusals()))
275+
if isinstance(e,SMTPSomeRefusedRecipients):
276+
log(" SomeRefused: %s"%(e.summary_refusals()))
276277
log(" Traceback: %s" % tb)
277278
return (extype, value, tb)
278279

@@ -287,9 +288,9 @@ def smtp_error_logging(thing):
287288
To: <action@ietf.org>
288289
From: %s
289290
""") % settings.SERVER_EMAIL
290-
if isinstance(e,SMTPRefusedRecipients):
291+
if isinstance(e,SMTPSomeRefusedRecipients):
291292
msg += textwrap.dedent("""\
292-
Subject: Recipients were refused while sending mail with Subject: %s
293+
Subject: Some recipients were refused while sending mail with Subject: %s
293294
294295
This is a message from the datatracker to IETF-Action about an email
295296
delivery failure, when sending email from the datatracker.
@@ -301,21 +302,30 @@ def smtp_error_logging(thing):
301302
%s
302303
--------- END ORIGINAL MESSAGE ---------
303304
""") % (e.original_msg.get('Subject', '[no subject]'),e.detailed_refusals(),e.original_msg.as_string())
304-
305305
else:
306306
msg += textwrap.dedent("""\
307307
Subject: Datatracker error while sending email
308308
309309
This is a message from the datatracker to IETF-Action about an email
310310
delivery failure, when sending email from the datatracker.
311311
312+
The original message was not delivered to anyone.
313+
312314
SMTP Exception: %s
313315
314316
Error Message: %s
317+
315318
""") % (extype,value)
319+
if hasattr(e,'original_msg'):
320+
msg += textwrap.dedent("""\
321+
The original message follows:
322+
-------- BEGIN ORIGINAL MESSAGE --------
323+
%s
324+
--------- END ORIGINAL MESSAGE ---------
325+
""") % e.original_msg.as_string()
316326
try:
317327
send_mail_preformatted(request=None, preformatted=msg)
318328
except smtplib.SMTPException as ticket_mail_error:
319-
log("Exception encountered while send a ticket to the secretariat")
329+
log("Exception encountered while sending a ticket to the secretariat")
320330
(extype,value) = sys.exc_info()[:2]
321331
log("SMTP Exception: %s : %s" % (extype,value))

0 commit comments

Comments
 (0)