2012-01-31 181 views
10

我想診斷爲什麼通過Amazon SES發送電子郵件不通過python工作。亞馬遜SES SMTP Python使用

以下示例演示了此問題,其中userpass設置爲適當的憑據。

>>> 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 

此消息不是特別有用,並嘗試其他vraiations,但似乎無法使其工作。

我可以用這些設置使用我的thunderbird電子郵件客戶端發送電子郵件,所以我的假設是我的使命與TLS相關。

回答

4

我確定這個問題是由時間引起的。因爲我從命令行執行該代碼,服務器會超時。如果我把它放到python文件中並運行它,它會執行得足夠快以確保發送消息。

8

完整的例子:

import smtplib 

user = '' 
pw = '' 
host = 'email-smtp.us-east-1.amazonaws.com' 
port = 465 
me = u'[email protected]' 
you = ('[email protected]',) 
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) 
11

我不認爲SMTP_SSL工作了與SES。必須使用starttls()

smtp = smtplib.SMTP("email-smtp.us-east-1.amazonaws.com") 
smtp.starttls() 
smtp.login(SESSMTPUSERNAME, SESSMTPPASSWORD) 
smtp.sendmail(me, you, msg)