如何在Django中与Crontab发送计划的电子邮件
我喜欢与Crontab一起在Django发送预定的电子邮件。我制作了一个非常简单的应用程序来测试如何在每分钟(仅出于测试目的)中发送电子邮件。我想我做错了什么,因为我无法收到邮件。
用户/cron.py
from django.core.mail import send_mail
def my_scheduled_job():
send_mail(
'subject',
'Here is the message.',
'[email protected]',
['[email protected]'],
fail_silently=False,
)
print('Successfully sent')
设置.py
CRONJOBS = [
('*/1 * * * *', 'users.cron.my_scheduled_job')
]
我添加了这样的工作:
python3 manage.py crontab add
然后
python3 manage.py runserver
我的邮件服务器配置正常,发送了其他所有电子邮件,但是我无法收到这些电子邮件,什么都没有发生。我不喜欢使用芹菜或django q。
I like to send scheduled emails in Django with Crontab. I made a very simple app to test how can I send an email in every minutes (just for testing purposes). I think I am doing something wrong, because I can't get the mails.
users/cron.py
from django.core.mail import send_mail
def my_scheduled_job():
send_mail(
'subject',
'Here is the message.',
'[email protected]',
['[email protected]'],
fail_silently=False,
)
print('Successfully sent')
settings.py
CRONJOBS = [
('*/1 * * * *', 'users.cron.my_scheduled_job')
]
I added the job like this:
python3 manage.py crontab add
then
python3 manage.py runserver
My mailing server configured fine, every other emails are sent, but I can't get these emails, nothing happens. I don't like to use Celery or Django Q.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用内置的Django命令来避免第三方集成或外部库。
例如,您可以在
my_app/management/commands/my_scheduled_job.py.py
file:然后您可以像往常一样配置crontab命令,例如每天晚上8点,
在这里有关自定义Django命令的其他信息。
You can use built-in Django commands to avoid third party integrations or external libraries.
For example you can add the following in a
my_app/management/commands/my_scheduled_job.py
file:and then you can just configure your crontab command as usual, for example every day at 8PM:
Here additional info about custom Django commands.
看看Django-mailer:
https://github.com/pinax/pinax/pinax/django-mailer
它可以替换标准的电子邮件后端,并将消息队列存储在DB中(因此不需要芹菜)。
您只需通过常规CRON安排其管理命令,基本上就是这样。
Take a look at django-mailer:
https://github.com/pinax/django-mailer
It can replace the standard email backend and stores message queue in the db (so no Celery needed).
You just schedule it's management command through regular cron and that's basically it.