Home > Back-end >  How to send weekly scheduled emails in Django
How to send weekly scheduled emails in Django

Time:11-27

I like to send scheduled emails for my users every Fridays. I'm using Pythonanywhere so I can't use Celery because PA can work with it. I like to use PA's Task function that can't schedule weekly tasks so it should check every day if it is Friday. I started to write a function but I'm not so experienced and stuck. I don't know how to write the function if it does not have any html where I hit a button to trigger the function.

I made a weekly_email_mm.py file in my project directory:

import datetime
from django.core.mail import send_mail
from django.shortcuts import render

today = datetime.date.today()
weekday = today.weekday()

def send_mm_email(???):
    
    subject = 'Hello'
    message = 'Hi there'
    
    if (weekday == 4):
        send_mail(
            subject,
            message,
            '[email protected]',
            ['[email protected]'],
            fail_silently=False,
        )
        print('Friday, mails sent')
    
    else:
        print('Not Friday')    
    
    return render(???)

Thank you in advance if you can help me out!

CodePudding user response:

You can use django-q,

https://django-q.readthedocs.io/en/latest/

it is easy to use and comes with django admin panel integration, you have to separately run qcluster server and use crontab field to create cron job.To create a job at every friday at 11:00 .use

0 11 * * 5

on crontab field in django admin panel or you can also create through code, follow the tutorial.

  • Related