[Django]-How to send scheduled email with Crontab in Django

2👍

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.

👤Dos

0👍

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.

Leave a comment