蟒蛇' smtplib 正常工作,但通过 cron 执行时不工作

发布于 2024-09-14 14:05:39 字数 403 浏览 2 评论 0原文

我的 python 代码如下所示。手动执行时效果很好。但是当通过 cronjob 执行时,电子邮件不会被发送。这是代码:

msg = MIMEMultipart()

msg['From'] = sender
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(message))

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(login, password)
print mailServer.sendmail(login, to, msg.as_string())

I have python code that looks like below. It works fine when executed manually. But when executed via a cronjob, the email is not being sent. Here is the code:

msg = MIMEMultipart()

msg['From'] = sender
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(message))

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(login, password)
print mailServer.sendmail(login, to, msg.as_string())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2024-09-21 14:05:39

也许您在 Unix 中的环境在手动/从 cron 执行时有所不同。在 cron 中执行“env > file”并在交互运行时与 env 进行比较

Maybe your environment in Unix is different when executed manually/from cron.Do a "env >file" in cron and compare to env when running interactively

吲‖鸣 2024-09-21 14:05:39

就我而言,邮件正文中的 os.getenv 不起作用。

发生错误的原因可能是 cron 脚本默认没有任何 shell 环境变量。

In my case, it was os.getenv inside mailbody that didn't work.

Maybe the error occurs because cron script doesn't by default have any shell environment variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文