We run an overnight script to send emails to a bunch of people at our company. A few days ago, the connection was held open for a long period of time and eventually timed out, and since then we have been unable to use the same code on the same machine to connect to the exchange SMTP server. Django is configured to use the same exact settings, runs on the same server, and continues to send out any error emails. In addition, running the same code on a different server we are still able to connect and send emails. I am utterly perplexed as to the causes and potential solutions.
Tl;dr:
- Python SMTPlib used to work fine on this server before a timeout a few days ago
- Django can send emails from the same server with the same authentication info
- The same code with the same login info still authenticates when run on a different server
Email connector:
class Email_Connect(object):
def __init__(self):
self.user = "mydomain\myuser"
self.pwd = "mypassword"
self.send_from = 'myuser@mycompany.com'
self.local_hostname = socket.getfqdn()
def smtp_conn(self,server='myrelay.mycompany.net'):
self.c = smtplib.SMTP(server, local_hostname=self.local_hostname)
self.c.set_debuglevel(1)
self.c.ehlo()
self.c.starttls()
self.c.ehlo()
self.c.login(self.user, self.pwd)
The code is called like so:
emailer = common.Email_Connect()
emailer.smtp_conn()
emailer.send_email(send_to,msg_subject,msg_body,None,attachment)
emailer.close()
Django Mail Settings (working fine on the same machine):
DEFAULT_FROM_EMAIL = 'myuser@mycompany.com'
SERVER_EMAIL = 'myuser@mycompany.com'
EMAIL_USE_TLS = True
EMAIL_HOST = "myrelay.mycompany.net"
EMAIL_HOST_USER = "myuser"
EMAIL_HOST_PASSWORD = "mypassword"
And here is the full debug output of running the code:
(production) c:\site\production\web\core>python program_replication.py
(21, 'send spool email reports')
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
send: 'ehlo mycomputer.mycompany.net\r\n'
reply: '250-smtp.mycompany.net Hello [10.1.*.*]\r\n'
reply: '250-SIZE 31457280\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250-CHUNKING\r\n'
reply: '250-XEXCH50\r\n'
reply: '250 XSHADOW\r\n'
reply: retcode (250); Msg: smtp.mycompany.net Hello [10.1.*.*]
SIZE 31457280
PIPELINING
DSN
ENHANCEDSTATUSCODES
STARTTLS
AUTH
8BITMIME
BINARYMIME
CHUNKING
XEXCH50
XSHADOW
send: 'STARTTLS\r\n'
reply: '220 2.0.0 SMTP server ready\r\n'
reply: retcode (220); Msg: 2.0.0 SMTP server ready
send: 'ehlo mycomputer.mycompany.net\r\n'
reply: '250-smtp.mycompany.net Hello [10.1.*.*]\r\n'
reply: '250-SIZE 31457280\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-AUTH\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250-CHUNKING\r\n'
reply: '250-XEXCH50\r\n'
reply: '250 XSHADOW\r\n'
reply: retcode (250); Msg: smtp.mycompany.net Hello [10.1.*.*]
SIZE 31457280
PIPELINING
DSN
ENHANCEDSTATUSCODES
AUTH
8BITMIME
BINARYMIME
CHUNKING
XEXCH50
XSHADOW
ERROR: No suitable authentication method found. replication module send spool email reports failed to run