Skip to content

Commit 6072945

Browse files
committed
Handle, log, and reraise an smtplib exception.
- Legacy-Id: 201
1 parent 4a5c6b5 commit 6072945

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

ietf/utils/mail.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.template.loader import render_to_string
88
from django.template import RequestContext
99
from ietf.utils import log
10+
import sys
1011

1112
def add_headers(msg):
1213
if not(msg.has_key('Message-ID')):
@@ -27,15 +28,22 @@ def send_smtp(msg):
2728
add_headers(msg)
2829
(fname, frm) = parseaddr(msg.get('From'))
2930
to = [addr for name, addr in getaddresses(msg.get_all('To') + msg.get_all('Cc', []))]
30-
# todo: exception handling
31-
server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
32-
if settings.DEBUG:
33-
server.set_debuglevel(1)
34-
if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
35-
server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
36-
server.sendmail(frm, to, msg.as_string())
31+
try:
32+
server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
33+
if settings.DEBUG:
34+
server.set_debuglevel(1)
35+
if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
36+
server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
37+
server.sendmail(frm, to, msg.as_string())
38+
# note: should pay attention to the return code, as it may
39+
# indicate that someone didn't get the email.
40+
except smtplib.SMTPException:
41+
server.quit()
42+
# need to improve log message
43+
log("got exception '%s' (%s) trying to send email from '%s' to %s subject '%s'" % (sys.exc_info()[0], sys.exc_info()[1], frm, to, msg.get('Subject', '[no subject]')))
44+
raise
3745
server.quit()
38-
log("sent email from '%s' to '%s' subject '%s'" % (frm, to, msg.get('Subject', '[no subject]')))
46+
log("sent email from '%s' to %s subject '%s'" % (frm, to, msg.get('Subject', '[no subject]')))
3947

4048
def copy_email(msg, to):
4149
'''

0 commit comments

Comments
 (0)