How to send weekly scheduled emails with PythonAnywhere

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,
            'something@from.com',
            ['user@xy.com'],
            fail_silently=False,
        )
        print('Friday, mails sent')
    
    else:
        print('Not Friday')    
    
    return render(???)

Thank you in advance!

Disclaimer: I’ve never used PythonAnywhere and don’t know much about it other than what I’m getting from the docs.

From Scheduled tasks | PythonAnywhere help, it looks like you just need to write a regular Python script that effectively runs from the shell.
(Also see Using a virtualenv in a scheduled task | PythonAnywhere help)

If you need to use Django facilities (models, templates, etc), you’ll probably want to create this as a custom admin command.

If you keep the file name the same, you would then schedule it as python manage.py weekly_email_mm, assuming you’re running it from with the proper virtual environment and your current directory is the location of your manage.py file.

Thank you for spending time to me.
My question is more basic or dumb. I know how to schedule the file in PA. My problem is that I can’t write my code correct. I wrote email function before but it triggers as I save a user. In this case I don’t know how to write the function. Do I need to write and html file and and url to it? Or I just need a request. And what to return, the request or something else? :flushed:

That’s where this comes into play:

You’ll convert your script into a manage.py command.

Ok, thank you very much Ken :slight_smile: