如何在Django中与Crontab发送计划的电子邮件

发布于 2025-01-22 22:48:10 字数 989 浏览 3 评论 0原文

我喜欢与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 技术交流群。

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

发布评论

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

评论(2

短叹 2025-01-29 22:48:11

您可以使用内置的Django命令来避免第三方集成或外部库。
例如,您可以在my_app/management/commands/my_scheduled_job.py.py file:

from django.core.mail import send_mail
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        # ... do the job here
        send_mail(
            # ...
        )
        print('Successfully sent')

然后您可以像往常一样配置crontab命令,例如每天晚上8点,

0 20 * * * python manage.py my_scheduled_job

在这里有关自定义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:

from django.core.mail import send_mail
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        # ... do the job here
        send_mail(
            # ...
        )
        print('Successfully sent')

and then you can just configure your crontab command as usual, for example every day at 8PM:

0 20 * * * python manage.py my_scheduled_job

Here additional info about custom Django commands.

好倦 2025-01-29 22:48:11

看看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.

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