Skip to content

Commit 169137f

Browse files
committed
Set debugging before establishing connection.
Use starttls when sending a username and password. Fail if the EHLO reply doesn't advertise starttls, or if the starttls reply isn't 220. - Legacy-Id: 954
1 parent 463182d commit 169137f

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

ietf/utils/mail.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from email.MIMEMultipart import MIMEMultipart
77
import smtplib
88
from django.conf import settings
9+
from django.core.exceptions import ImproperlyConfigured
910
from django.template.loader import render_to_string
1011
from django.template import RequestContext
1112
from ietf.utils import log
@@ -33,10 +34,17 @@ def send_smtp(msg):
3334
to = [addr for name, addr in getaddresses(msg.get_all('To') + msg.get_all('Cc', []))]
3435
server = None
3536
try:
36-
server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
37+
server = smtplib.SMTP()
3738
if settings.DEBUG:
3839
server.set_debuglevel(1)
40+
server.connect(settings.EMAIL_HOST, settings.EMAIL_PORT)
3941
if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
42+
server.ehlo()
43+
if 'starttls' not in server.esmtp_features:
44+
raise ImproperlyConfigured('password configured but starttls not supported')
45+
(retval, retmsg) = server.starttls()
46+
if retval != 220:
47+
raise ImproperlyConfigured('password configured but tls failed: %d %s' % ( retval, retmsg ))
4048
server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
4149
server.sendmail(frm, to, msg.as_string())
4250
# note: should pay attention to the return code, as it may

0 commit comments

Comments
 (0)