Search code examples
pythondjangoformsemail

How to send confirmation emails and scheduled emails in Django


I'm working on a Django project involving a custom user registration form. My goal is to implement a two-step email notification process upon form submission:

Immediate Email Confirmation: Automatically send a customized email to the user immediately after they submit the form. Scheduled Email Notification: Send a second customized email at a later date, which is determined by the information provided when the form is created (e.g., a specific date for event reminders). The scheduling of the second email needs to be dynamic, allowing for different dates based on the form's context, such as varying event dates.

How can I achieve this with Django? especially for scheduling emails to be dispatched at a future date. Note that I expect a volume of 1000 submissions per month.

Thanks for your help in advance.


Solution

  • you can use redis and then with a celery task send emails as you want.

    like code below:

    for when you are creating form.

    def create_form():
        # smth happens here
        redis.set('send_email:2024-05-10', 'specific data you want to customize the email with')
    

    for celery task

    @shared_task
    def send_second_email():
        now = datetime.now()
        for key in redis.keys('send_email:*'):
            time = key.split(':')[1]
            if time < now: # you need to parse time
                data = redis.get(key)
                # send second email here