2017-08-23 22 views
0

我想讓Python發送電子郵件。首先,我開始蟒蛇smptd所如下:使用python發送電子郵件困難

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

然後,從https://www.tutorialspoint.com/python/python_sending_email.htm修改實例,我有以下腳本:

#!/usr/bin/python 

import smtplib 

sender = '[email protected]' 
receivers = ['[email protected]'] 

message = """From: From Person <[email protected]> 
To: To Person <[email protected]> 
Subject: SMTP e-mail test 

This is a test e-mail message. 
""" 

try: 
    smtpObj = smtplib.SMTP('localhost:1025') 
    smtpObj.sendmail(sender, receivers, message) 
    print "Successfully sent email" 
except SMTPException: 
    print "Error: unable to send email" 

當我運行該腳本,我得到的消息「發送成功電子郵件」。在我的其他終端窗口(含蟒蛇smptd所之一),它顯示了這個腳本運行後:

---------- MESSAGE FOLLOWS ---------- 
From: From Person <[email protected]> 
To: To Person <[email protected]> 
Subject: SMTP e-mail test 
X-Peer: ::1 

This is a test e-mail message. 
------------ END MESSAGE ------------ 

但是......沒有電子郵件被髮送。任何想法可能是錯誤的?這只是正常 - 電子郵件被髮送:

/usr/sbin/sendmail [email protected] <<<"hello" 

回答

0

你實際上是通過本地發展SMTP服務器發送(第一個例子)您的電子郵件,因爲你提到的DebuggingServer類,如documention說:

創建一個新的調試服務器。參數是根據SMTPServer。 消息將被丟棄,並打印在stdout上。

+0

這樣做。謝謝!我在互聯網上找到的每一個例子都提到了DebuggingServer參數,而且他們中沒有一個人提到這會拋棄這個消息! – brt381