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

2024-05-19 09:33:11 发布

您现在位置: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"

当我运行这个脚本时,我收到一条消息“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"

Tags: tofromtest脚本commessage电子邮件email

热门问题