使用python发送电子邮件很困难

时间:2017-08-23 12:38:32

标签: python email smtp

我正在尝试让Python发送电子邮件。首先,我按如下方式启动了python smptd:

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

然后,根据https://www.tutorialspoint.com/python/python_sending_email.htm调整示例,我有以下脚本:

#!/usr/bin/python

import smtplib

sender = 'from@fromdomain.com'
receivers = ['brt381@gmail.com']

message = """From: From Person <from@fromdomain.com>
To: To Person <brt381@gmail.com>
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"

当我运行此脚本时,收到消息“Successfully sent email”。在我的另一个终端窗口(包含python smptd的窗口)中,它在运行脚本后显示:

---------- MESSAGE FOLLOWS ----------
From: From Person <from@fromdomain.com>
To: To Person <brt381@gmail.com>
Subject: SMTP e-mail test
X-Peer: ::1

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

但是......没有发送电子邮件。知道什么可能是错的吗?这很好 - 电子邮件发送:

/usr/sbin/sendmail brt381@gmail.com <<<"hello"

1 个答案:

答案 0 :(得分:0)

您实际上是通过本地开发smtp服务器发送(在第一个示例中)您的电子邮件,因为您提到了DebuggingServer类,正如documention所说:

  

创建一个新的调试服务器。参数是根据SMTPServer。   邮件将被丢弃,并打印在标准输出上。