蟒蛇' smtplib 正常工作,但通过 cron 执行时不工作
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您在 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
就我而言,邮件正文中的
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.