10

I am trying to diagnose why sending email through Amazon SES is not working via python.

The following example demonstrates the problem, where user and pass are set to the appropriate credentials.

>>> import smtplib
>>> s = smtplib.SMTP_SSL("email-smtp.us-east-1.amazonaws.com", 465)
>>> s.login(user, pw)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/smtplib.py", line 549, in login
    self.ehlo_or_helo_if_needed()
  File "/usr/lib/python2.6/smtplib.py", line 510, in ehlo_or_helo_if_needed
    (code, resp) = self.helo()
  File "/usr/lib/python2.6/smtplib.py", line 372, in helo
    (code,msg)=self.getreply()
  File "/usr/lib/python2.6/smtplib.py", line 340, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

This message is not particularly useful, and have tried other vraiations, but can't seem to get it to work.

I can send email using my thunderbird email client with these settings, so my assumption is that I am mission something TLS-related.

Steffen Opel
  • 61,065
  • 11
  • 183
  • 208
Kevin Dolan
  • 4,474
  • 3
  • 30
  • 45

3 Answers3

12

I don't think SMTP_SSL works anymore with SES. One must use starttls()

smtp = smtplib.SMTP("email-smtp.us-east-1.amazonaws.com")
smtp.starttls()
smtp.login(SESSMTPUSERNAME, SESSMTPPASSWORD)
smtp.sendmail(me, you, msg)
jrwren
  • 15,914
  • 5
  • 32
  • 52
8

A full example:

import smtplib

user = ''
pw   = ''
host = 'email-smtp.us-east-1.amazonaws.com'
port = 465
me   = u'me@me.com'
you  = ('you@you.com',)
body = 'Test'
msg  = ("From: %s\r\nTo: %s\r\n\r\n"
       % (me, ", ".join(you)))

msg = msg + body

s = smtplib.SMTP_SSL(host, port, 'yourdomain')
s.set_debuglevel(1)
s.login(user, pw)

s.sendmail(me, you, msg)
Mordi
  • 581
  • 2
  • 5
5

I have determined this problem to be caused by the timing. Because I was executing that code from the command line, the server would timeout. If I put it into a python file and run it, it executes fast enough to ensure the message is sent.

Kevin Dolan
  • 4,474
  • 3
  • 30
  • 45