'How to solve Django send_mail schedule problem?
I am using Pythonanywhere (PA) and like to use the PA's task scheduler (it runs every day and chacks if it is Wednesday or not) to send scheduled emails. I made a new file in my app folder called: weeklyemailmm.py. The email settings in the setting.py works with other emailing stuff on my site. What am I doing wrong?
settings.py
EMAIL_HOST = 'xxx'
EMAIL_PORT = 'xxx'
EMAIL_HOST_USER = 'xxx'
EMAIL_HOST_PASSWORD = 'xxx'
EMAIL_USE_TLS = True
I try to use the code below:
from django.core.mail import send_mail
import datetime
from django.conf import settings
settings.configure(settings, DEBUG=True)
today = datetime.date.today()
weekday = today.weekday()
subject = 'New weekly email'
message = 'Hi there!'
if (weekday == 2):
try:
send_mail(
'Subject here',
'Here is the message.',
'[email protected]',
['[email protected]'],
fail_silently=False,
)
print('It is Wednesday, email sent')
except:
print('It is not Wednesday')
else:
print('Email does not sent')
On this way I always get It is not Wednesday.
If I delete the try-except part and outdent it says:
RecursionError: maximum recursion depth exceeded while calling a Python object
If I delete the settings.configure(settings, DEBUG=True) that could be possibly wrong it say:
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Traceback
PSA/firm/weeklyemailmm.py
Traceback (most recent call last):
File "/Users/ruszakmiklos/Documents/GitHub/MPSA/firm/weeklyemailmm.py", line 23, in <module>
send_mail(
File "/usr/local/lib/python3.9/site-packages/django/core/mail/__init__.py", line 52, in send_mail
connection = connection or get_connection(
File "/usr/local/lib/python3.9/site-packages/django/core/mail/__init__.py", line 34, in get_connection
klass = import_string(backend or settings.EMAIL_BACKEND)
File "/usr/local/lib/python3.9/site-packages/django/conf/__init__.py", line 84, in __getattr__
val = getattr(self._wrapped, name)
File "/usr/local/lib/python3.9/site-packages/django/conf/__init__.py", line 249, in __getattr__
return getattr(self.default_settings, name)
File "/usr/local/lib/python3.9/site-packages/django/conf/__init__.py", line 84, in __getattr__
val = getattr(self._wrapped, name)...
And line 84 and 249 so on.
Solution 1:[1]
Using EmailMessage :
from django.core.mail import EmailMessage
today = datetime.date.today()
weekday = today.weekday()
subject = 'New weekly email'
message = 'Hi there!'
if (weekday == 2):
try:
msg = EmailMessage(subject, message, '[email protected]',
['[email protected]'])
msg.send()
print('It is Wednesday, email sent')
except:
print('It is not Wednesday')
else:
print('Email does not sent')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CPMaurya |
