首页 > 解决方案 > 如何在 web apache 服务器上自动化 python 脚本

问题描述

我想在 python 中创建一个自动脚本,当我的网站上有新订单时,它将通过电子邮件通知我。到目前为止,我发现我每分钟都可以使用来自 apache 服务器的 cronjobs,但这似乎有点过头了,因为我每分钟都没有收到订单。

所以我正在寻找更好的解决方案。

标签: pythonpython-3.xautomation

解决方案


这是您无需自动化的用户下订单时可以调用此代码的代码

如果您使用的是 django,则可以使用:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)

没有 django 你可以使用这个:

import smtplib

sender = 'from@example.com'
receivers = ['to@example.com']

message = “This is a test e-mail message."

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

推荐阅读