Amazon SES SMTP Python Usage

Kevin Dolan picture Kevin Dolan · Jan 31, 2012 · Viewed 10.2k times · Source

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.

Answer

jrwren picture jrwren · Sep 10, 2012

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)