Thursday, July 1, 2021

Re: SChedule email

You can create a custom command using this doc

Now, you can call it like

python manage.py mycustomcommandname

so in Linux environment  you can use crontab (with crontab -e) to execute the command.


El jue, 1 jul 2021 a las 15:28, SKYLINE TV (<skylineict@gmail.com>) escribió:
You can check out celery and how to integrate it with django. Once done, task scheduling is easy,first add your gmail configuration in settings.py as follows:

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()

Then finally your email template 'schedule_mail.html
{% autoescape off %} 
Hello , 
 This is a test email if you are seeing this,
 your email got delivered! 
 Regards, Coding Team.
 {% endautoescape %}
On Thursday, July 1, 2021 at 1:54:31 PM UTC+1 divyamu...@gmail.com wrote:
HI,

I wanted to schedule a email every jan and aug 6 monthly basis without celery using settings.py. Can anyone help?

once in every 6months mail should be triggered

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a87a8623-d123-404d-91b8-d30a01d91dc0n%40googlegroups.com.


--
"La utopía sirve para caminar" Fernando Birri


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG%2B5VyPk%3D31F3j1D%2BsH8AA_2DDtK6XQikKBK1aRcvpzEvJJR-A%40mail.gmail.com.

No comments:

Post a Comment