Search code examples
pythondjangocrondjango-mailer

Django-mailer - autosend not working locally (maybe: bad localhost crontab path?)


I'm new to django and I've installed django-mailer 2.0. It's working when I manually send the queued mails: (venv)$ python manage.py send_mail, but when I set up the crontab (which is the first time I use a cron job), it's not working. I guess there might be some mistakes in the paths.

Official documentation of django-mailer suggests:
* * * * * (/path/to/your/python /path/to/your/manage.py send_mail >> ~/cron_mail.log 2>&1)

Mine:

# first I tried:
* * * * * (/usr/bin/python3 /Users/username/Documents/GitHub/projectname/manage.py send_mail >> ~/cron_mail.log 2>&1)

# then I tried:
* * * * * (/Users/username/Documents/GitHub/projectname/venv/bin/python /Users/username/Documents/GitHub/projectname/manage.py send_mail >> ~/cron_mail.log 2>&1)

# also this:
* * * * * cd /Users/username/Documents/GitHub/projectname; venv/bin/python manage.py send_mail

# I've tried Romeo's solution as well

None is working.. Help please!

However, when I use exactly the same command in bash, it works:

$ cd /Users/username/Documents/GitHub/projectname
$ venv/bin/python manage.py send_mail

this works indeed! I'm utterly confused...

PS. about django-mailer: I manually sent 4 queued emails each to 2 email addresses, however, 2 got missing never delivered (not lost in junk mails either). Is this normal?


Solution

  • What you can try is to change to the directory where the code is located and then run it:

    * * * * * cd /Users/username/Documents/GitHub/projectname; /usr/bin/python3 manage.py send_mail >>  /Users/username/cron_mail.log 2>&1
    

    Also is better to use absolute paths in cron. And add this in script plus import your environment variables.:

    #!/bin/bash
    source /Users/username/.bash_profile #or .bashrc
    cd /Users/username/Documents/GitHub/projectname
    venv/bin/python manage.py send_mail >>  /Users/username/cron_mail.log 2>&1
    

    and then make the script executable:

    chmod +rx script.sh
    

    and add it in cron:

    * * * * * /path/to/script.sh