Reputation: 57
I would like to send an automated mail using python, django and crontab. So I did the following things.
Created a cron_tab.py(which is inside the folder home/myhome/django/myapp/registration/cron_tab.py) which looks like below:
from django.core.mail import send_mail, EmailMessage,EmailMultiAlternatives
subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
And using terminal I entered in to crontab by issuing the below command
crontab -e
I scheduled the task like
* 1 * * * /home/myhome/django/myapp/registration/cron_tab.py
But I didn't mail receive. What am I doing wrong? Please Somebody help me. After changing the file mode I get the following error and pasted the traceback
myhome@myhome:~/django/myapp/registration$ ./cron_tab.py
from: can't read /var/mail/django.core.mail
./cron_tab.py: line 3: subject: command not found
./cron_tab.py: line 4: from_email: command not found
./cron_tab.py: line 5: to: command not found
./cron_tab.py: line 6: text_content: command not found
./cron_tab.py: line 7: html_content: command not found
./cron_tab.py: line 8: syntax error near unexpected token `('
./cron_tab.py: line 8: `msg = EmailMultiAlternatives(subject, text_content, from_email,
[to])'
Upvotes: 1
Views: 403
Reputation: 3043
./cron_tab.py
on the /home/myhome/django/myapp/registration/
directory. If this does not work execute: chmod +x /home/myhome/django/myapp/registration/cron_tab.py
anywhere on your command line, making sure your script starts with #!/usr/bin/env python
pointing to your Python interpreter.If you are not using any other feature from Django (like models, querysets, etc), this approach for sending the email should work as a separate script not knowing anything from Django. On the other hand, if using some of the Django features you will have two approaches to run this script:
a) As a management command => If using this approach, I recommend you creating a separate script for the crontab which only will run:
cd /home/myhome/django/
python manage.py <your_command>
(On this case: )
b) As a Python script loading your Django configuration => You can run your script using directly.
Upvotes: 1
Reputation: 1638
The issue is that you are using a Python script that is not probably an executable and it doesn't declare the executable in the first line of the line.
You have two options:
1) Add the python interpreter in the cron call (in this example the python interpreter is in /usr/bin/python:
2) Made the python script executable (2.1+2.2)
2.1.- Made the file executable
$ chmod +x /home/myhome/django/myapp/registration/cron_tab.py
2.2.- Edit the file and in the first line declare in the interpreter:
#!/usr/bin/python
from django.core.mail import send_mail, EmailMessage,EmailMultiAlternatives
Upvotes: 2
Reputation: 45575
I think your cron_tab.py
didn't read django configuration from setting.py
. What happens then you run this script from the shell?
Anyway you should consider to use custom management command for this task.
Upvotes: 2