2013-01-17 41 views
2

我複製並粘貼這個例子,並填寫我自己的電子郵件地址。Python電子郵件 - 簡單的例子運行沒有錯誤,但電子郵件永遠不會到達

# Import smtplib for the actual sending function 
import smtplib 

# Import the email modules we'll need 
from email.mime.text import MIMEText 

# Open a plain text file for reading. For this example, assume that 
# the text file contains only ASCII characters. 
fp = open('/Users/Jon/dev/iit/test-tools/logs/practice_tests.test_one.log', 'rb') 
# Create a text/plain message 
msg = MIMEText(fp.read()) 
fp.close() 

me = '[email protected]' 
you = '[email protected]' 

# me == the sender's email address 
# you == the recipient's email address 
msg['Subject'] = 'The contents of the log file' 
msg['From'] = me 
msg['To'] = you 

# Send the message via our own SMTP server, but don't include the 
# envelope header. 
s = smtplib.SMTP('localhost', 1025) 
s.sendmail(me, [you], msg.as_string()) 
s.quit() 

我再打開另一個終端窗口,運行以下命令:

python -m smtpd -n -c DebuggingServer localhost:1025 

當我運行該文件以發送電子郵件,它完成沒有錯誤,並且正在運行python -m smtpd -n -c DebuggingServer localhost:1025命令打印終端看起來正確的一個很好的日誌消息:

---------- MESSAGE FOLLOWS ---------- 
Content-Type: text/plain; charset="us-ascii" 
MIME-Version: 1.0 
Content-Transfer-Encoding: 7bit 
Subject: The contents of the log file 
From: [email protected] 
To: [email protected] 
X-Peer: 127.0.0.1 

line one 
line two 
line three 
------------ END MESSAGE ------------ 

但是,當我檢查我的雅虎電子郵件地址,電子郵件從未收到。我等了大約5分鐘,我認爲這是足夠的。我做錯了什麼?

回答

5

smtpd documentation

類smtpd.DebuggingServer(localaddr,remoteaddr) 創建一個新的調試服務器。參數是根據SMTPServer。 消息將被丟棄,並打印在stdout上。

+0

哈哈,爲我複製粘貼而不理解...... – jononomo

1

調試你的郵件,看到了幕後,只是設置SMTP對象的DEBUGLEVEL一個好辦法:

debuglevel = True 
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) 
mail.set_debuglevel(debuglevel) 
mail.starttls() 
mail.login(SMTP_USERNAME, SMTP_PASSWORD) 
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string()) 
mail.quit() 

這可以讓你在過程中看到步驟/檢查:

send: 'ehlo localhost.localdomain\r\n' 
reply: '250-smtp.mail.yahoo.com\r\n' 
reply: '250-PIPELINING\r\n' 
reply: '250-SIZE 41697280\r\n' 
reply: '250-8 BITMIME\r\n' 
reply: '250-AUTH PLAIN LOGIN XYMCOOKIE\r\n' 
reply: '250 STARTTLS\r\n' 
reply: retcode (250); Msg: smtp.mail.yahoo.com 
PIPELINING 
SIZE 41697280 
8 BITMIME 
AUTH PLAIN LOGIN XYMCOOKIE 
STARTTLS 
send: 'STARTTLS\r\n' 
reply: '220 2.0.0 Start TLS\r\n' 
reply: retcode (220); Msg: 2.0.0 Start TLS 
send: 'ehlo localhost.localdomain\r\n' 
reply: '250-smtp.mail.yahoo.com\r\n' 
reply: '250-PIPELINING\r\n' 
reply: '250-SIZE 41697280\r\n' 
reply: '250-8 BITMIME\r\n' 
reply: '250 AUTH PLAIN LOGIN XYMCOOKIE\r\n' 
reply: retcode (250); Msg: smtp.mail.yahoo.com 
PIPELINING 
SIZE 41697280 
8 BITMIME 
AUTH PLAIN LOGIN XYMCOOKIE 
send: 'AUTH PLAIN AGZlZHVzcaddfjbkejkjkVAeWFob28uY29tAG1taHR0ODAxQA==\r\n' 
reply: '235 2.0.0 OK\r\n' 
reply: retcode (235); Msg: 2.0.0 OK 
send: 'mail FROM:<[email protected]> size=471\r\n' 
reply: '250 OK , completed\r\n' 
reply: retcode (250); Msg: OK , completed 
send: 'rcpt TO:<[email protected]>\r\n' 
reply: '250 OK , completed\r\n' 
reply: retcode (250); Msg: OK , completed 
send: 'data\r\n' 
reply: '354 Start Mail. End with CRLF.CRLF\r\n' 
reply: retcode (354); Msg: Start Mail. End with CRLF.CRLF 
data: (354, 'Start Mail. End with CRLF.CRLF') 
send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nSubject: REMINDER:Company - Service at appointmentTime\r\nFrom: [email protected]\r\nTo: [email protected]\r\n\r\n\r\nHello, [username]! Just wanted to send a friendly appointment\r\nreminder for your appointment:\r\n[Company]\r\nWhere: [companyAddress]\r\nTime: [appointmentTime]\r\n\r\nCompany URL: [companyUrl]\r\n\r\nChange appointment?? Add Service??\r\n\r\nchange notification preference (text msg/email)\r\n.\r\n' 
reply: '250 OK , completed\r\n' 
reply: retcode (250); Msg: OK , completed 
data: (250, 'OK , completed') 
send: 'quit\r\n' 
reply: '221 Service Closing transmission\r\n' 
reply: retcode (221); Msg: Service Closing transmission 
相關問題