Skip to content

Commit 5ca9581

Browse files
committed
Reworked smtp error warning and logging to not use hard-to-read context_managers. Fixes bug ietf-tools#1390. Commit ready to merge.
- Legacy-Id: 8584
1 parent b74d4e7 commit 5ca9581

1 file changed

Lines changed: 59 additions & 68 deletions

File tree

ietf/utils/mail.py

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import copy
1919
import textwrap
2020
import traceback
21-
from contextlib import contextmanager
2221

2322
# Testing mode:
2423
# import ietf.utils.mail
@@ -215,9 +214,13 @@ def send_mail_mime(request, to, frm, subject, msg, cc=None, extra=None, toUser=F
215214
debugging = getattr(settings, "USING_DEBUG_EMAIL_SERVER", False) and settings.EMAIL_HOST == 'localhost' and settings.EMAIL_PORT == 2025
216215

217216
if test_mode or debugging or settings.SERVER_MODE == 'production':
218-
with smtp_error_logging(send_smtp) as logging_send:
219-
with smtp_error_user_warning(logging_send,request) as send:
220-
send(msg, bcc)
217+
try:
218+
send_smtp(msg,bcc)
219+
except smtplib.SMTPException as e:
220+
log_smtp_exception(e)
221+
build_warning_message(request, e)
222+
send_error_email(e)
223+
221224
elif settings.SERVER_MODE == 'test':
222225
if toUser:
223226
copy_email(msg, to, toUser=True, originalBcc=bcc)
@@ -230,9 +233,12 @@ def send_mail_mime(request, to, frm, subject, msg, cc=None, extra=None, toUser=F
230233
if copy_to and not test_mode and not debugging: # if we're running automated tests, this copy is just annoying
231234
if bcc:
232235
msg['X-Tracker-Bcc']=bcc
233-
with smtp_error_logging(copy_email) as logging_copy:
234-
with smtp_error_user_warning(logging_copy,request) as copy:
235-
copy(msg, copy_to,originalBcc=bcc)
236+
try:
237+
copy_email(msg, copy_to, originalBcc=bcc)
238+
except smtplib.SMTPException as e:
239+
log_smtp_exception(e)
240+
build_warning_message(request, e)
241+
send_error_email(e)
236242

237243
def parse_preformatted(preformatted, extra={}, override={}):
238244
"""Parse preformatted string containing mail with From:, To:, ...,"""
@@ -276,10 +282,8 @@ def send_mail_message(request, message, extra={}):
276282
send_mail_text(request, message.to, message.frm, message.subject,
277283
message.body, cc=message.cc, bcc=message.bcc, extra=e)
278284

279-
def log_smtp_exception(e):
280-
285+
def exception_components(e):
281286
# See if it's a non-smtplib exception that we faked
282-
283287
if len(e.args)==1 and isinstance(e.args[0],dict) and e.args[0].has_key('really'):
284288
orig = e.args[0]
285289
extype = orig['really']
@@ -289,78 +293,65 @@ def log_smtp_exception(e):
289293
extype = sys.exc_info()[0]
290294
value = sys.exc_info()[1]
291295
tb = traceback.format_tb(sys.exc_info()[2])
296+
return (extype, value, tb)
292297

293-
298+
def log_smtp_exception(e):
299+
(extype, value, tb) = exception_components(e)
294300
log("SMTP Exception: %s : %s" % (extype,value))
295301
if isinstance(e,SMTPSomeRefusedRecipients):
296302
log(" SomeRefused: %s"%(e.summary_refusals()))
297303
log(" Traceback: %s" % tb)
298-
return (extype, value, tb)
299-
300-
@contextmanager
301-
def smtp_error_user_warning(thing,request):
302-
try:
303-
yield thing
304-
except smtplib.SMTPException as e:
305-
(extype, value, tb) = log_smtp_exception(e)
306-
307-
if request:
308-
warning = "An error occured while sending email:\n"
309-
if getattr(e,'original_msg',None):
310-
warning += "Subject: %s\n" % e.original_msg.get('Subject','[no subject]')
311-
warning += "To: %s\n" % e.original_msg.get('To','[no to]')
312-
warning += "Cc: %s\n" % e.original_msg.get('Cc','[no cc]')
313-
if isinstance(e,SMTPSomeRefusedRecipients):
314-
warning += e.detailed_refusals()
315-
else:
316-
warning += "SMTP Exception: %s\n"%extype
317-
warning += "Error Message: %s\n\n"%value
318-
warning += "The message was not delivered to anyone."
319-
messages.warning(request,warning,extra_tags='preformatted',fail_silently=True)
320-
321-
raise
322-
323-
@contextmanager
324-
def smtp_error_logging(thing):
325-
try:
326-
yield thing
327-
except smtplib.SMTPException as e:
328-
(extype, value, tb) = log_smtp_exception(e)
329304

330-
msg = MIMEMultipart()
331-
msg['To'] = '<action@ietf.org>'
332-
msg['From'] = settings.SERVER_EMAIL
305+
def build_warning_message(request, e):
306+
(extype, value, tb) = exception_components(e)
307+
if request:
308+
warning = "An error occured while sending email:\n"
309+
if getattr(e,'original_msg',None):
310+
warning += "Subject: %s\n" % e.original_msg.get('Subject','[no subject]')
311+
warning += "To: %s\n" % e.original_msg.get('To','[no to]')
312+
warning += "Cc: %s\n" % e.original_msg.get('Cc','[no cc]')
333313
if isinstance(e,SMTPSomeRefusedRecipients):
334-
msg['Subject'] = 'Subject: Some recipients were refused while sending mail with Subject: %s' % e.original_msg.get('Subject','[no subject]')
335-
textpart = textwrap.dedent("""\
336-
This is a message from the datatracker to IETF-Action about an email
337-
delivery failure, when sending email from the datatracker.
338-
339-
%s
340-
341-
""") % e.detailed_refusals()
314+
warning += e.detailed_refusals()
342315
else:
343-
msg['Subject'] = 'Datatracker error while sending email'
344-
textpart = textwrap.dedent("""\
345-
This is a message from the datatracker to IETF-Action about an email
346-
delivery failure, when sending email from the datatracker.
316+
warning += "SMTP Exception: %s\n"%extype
317+
warning += "Error Message: %s\n\n"%value
318+
warning += "The message was not delivered to anyone."
319+
messages.warning(request,warning,extra_tags='preformatted',fail_silently=True)
320+
321+
def send_error_email(e):
322+
(extype, value, tb) = exception_components(e)
323+
msg = MIMEMultipart()
324+
msg['To'] = '<action@ietf.org>'
325+
msg['From'] = settings.SERVER_EMAIL
326+
if isinstance(e,SMTPSomeRefusedRecipients):
327+
msg['Subject'] = 'Subject: Some recipients were refused while sending mail with Subject: %s' % e.original_msg.get('Subject','[no subject]')
328+
textpart = textwrap.dedent("""\
329+
This is a message from the datatracker to IETF-Action about an email
330+
delivery failure, when sending email from the datatracker.
347331
348-
The original message was not delivered to anyone.
332+
%s
349333
350-
SMTP Exception: %s
334+
""") % e.detailed_refusals()
335+
else:
336+
msg['Subject'] = 'Datatracker error while sending email'
337+
textpart = textwrap.dedent("""\
338+
This is a message from the datatracker to IETF-Action about an email
339+
delivery failure, when sending email from the datatracker.
351340
352-
Error Message: %s
353-
354-
""") % (extype,value)
355-
if hasattr(e,'original_msg'):
356-
textpart += "The original message follows:\n"
357-
msg.attach(MIMEText(textpart,_charset='utf-8'))
358-
if hasattr(e,'original_msg'):
359-
msg.attach(MIMEMessage(e.original_msg))
341+
The original message was not delivered to anyone.
360342
361-
send_error_to_secretariat(msg)
343+
SMTP Exception: %s
362344
345+
Error Message: %s
346+
347+
""") % (extype,value)
348+
if hasattr(e,'original_msg'):
349+
textpart += "The original message follows:\n"
350+
msg.attach(MIMEText(textpart,_charset='utf-8'))
351+
if hasattr(e,'original_msg'):
352+
msg.attach(MIMEMessage(e.original_msg))
363353

354+
send_error_to_secretariat(msg)
364355

365356
def send_error_to_secretariat(msg):
366357

0 commit comments

Comments
 (0)