django, write send email with scheduled times like cron :
https://stackoverflow.com/questions/63552113/gmail-schedule-send-email-in-django
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_email'
EMAIL_HOST_PASSWORD = 'your password'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_PORT = 465
Then in your tasks.py you can add the function for scheduling emails as follows:
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
@periodic_task(
run_every=(crontab(hour=3, minute=34)), #runs exactly at 3:34am every day
name="Dispatch_scheduled_mail",
reject_on_worker_lost=True,
ignore_result=True)
def schedule_mail():
message = render_to_string('app/schedule_mail.html')
mail_subject = 'Scheduled Email'
to_email = getmail
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
django send email offical documentation:
https://docs.djangoproject.com/en/4.1/topics/email/
Mail is sent using the SMTP host and port specified in the EMAIL_HOST
and EMAIL_PORT
settings. The EMAIL_HOST_USER
and EMAIL_HOST_PASSWORD
settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS
and EMAIL_USE_SSL
settings control whether a secure connection is used.
django ModuleNotFoundError: No module named 'django_smtp_ssl'
https://stackoverflow.com/questions/45817296/django-1-8-6-modulenotfounderror-no-module-named-django-smtp-sslpip install django-smtp-ssl
For docker, add this in requirements.txt
django-smtp-ssl>=1.0,<2.0
https://pypi.org/project/django-smtp-ssl/#history
No comments:
Post a Comment