通过python从localhost发送电子邮件到localhost

时间:2013-02-19 06:21:47

标签: python web-services email sendmail smtplib

在我投入生产之前,我正在本地计算机上构建和测试Web服务。我想测试邮件服务。我正在使用标准的python电子邮件和smtplib库。

import smtplib
from email.mime.text import MIMEText
fp = open('textfile', 'rb')
msg = MIMEText(fp.read())
fp.close()

me = 'me_email@localhost'
you = 'me_again_email@localhost'
msg['Subject'] = 'The contents of %s' %fp
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

我没有配置sendmail,因此会抛出错误。但由于我只是想测试我的网络服务,我不担心sendmail现在无法发送电子邮件。我的服务旨在从数据库中提取一些记录并向他们发送电子邮件。所以我想知道这个连接,python从db获取输入并推送电子邮件是否正常工作。我想收到通过脚本发送的localhost上的电子邮件。

1 个答案:

答案 0 :(得分:2)

需要配置SMTP server来发送电子邮件。除非您配置SMTP服务器,否则无法发送电子邮件。有关使用python smtplib的更多信息,请参见pymotw.comtutorialspoint.comPython docs