Search code examples
pythondjangocelerydjango-celerydjango-celery-beat

How to send emails with Django/Celery based on DateTimes stored in the database?


I want to send reminder emails based on different dates saved in the database. I saw some links that was kinda related to that but most of them was really messy.

Table send_email_at: [2022/07/12, 2022/08/19, 2022/07/23, ...]

So the ideal would be to get all dates > now and if it's due just send the email


Solution

  • Run a regular task with beat (as you have tagged in your question) once a day, and in the task, filter your table for the current date, then send emails if anything exists:

    @shared_task
    def send_emails_if_due():
        if SendEmailAt.objects.filter(when=timezone.now().date()).exists():
            send_emails()