gpt4 book ai didi

python - 通过 python 从本地主机发送电子邮件到本地主机

转载 作者:行者123 更新时间:2023-12-01 05:49:25 25 4
gpt4 key购买 nike

我正在本地计算机上构建和测试 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 从数据库获取输入并推送电子邮件是否正常工作。我想在本地主机上接收通过脚本发送的电子邮件。

最佳答案

SMTP server需要配置用于发送电子邮件。除非配置 SMTP 服务器,否则无法发送电子邮件。有关使用 python smtplib 的更多信息可以在 pymotw.com 中找到。 , tutorialspoint.comPython docs .

关于python - 通过 python 从本地主机发送电子邮件到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14951109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com