繁体   English   中英

使用python很难发送电子邮件

[英]Difficulty sending e-mail using python

我正在尝试让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"

运行此脚本时,我收到消息“成功发送电子邮件”。 在我的另一个终端窗口(包含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"

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM