I want to take the receiver email address and schedule date as an input and send mail to the schedule date. I have following lines of code in views.py Actually I'm looking forward to use apscheduler package for async background task in django but any suggestions and solutions are appreciable.
class SendMailView(APIView):
def post(self, request, *args, **kwargs):
'''
POST Method for sending Email
'''
send_to = request.data.get('receiver_email')
schedule_for = request.data.get('schedule_date')
email_plaintext_message = " Hello"
send_mail(
# title:
"Test mail,
# message:
email_plaintext_message,
# from:
'some@gmail.com,
# to:
[send_to]
)
return Response({"status":"Email Send"},status=status.HTTP_200_OK)
Consider using Celery. All you need to do is follow the installation, start a worker and the beat, with that you can scheduler tasks.
task.py
from celery import shared_task
@shared_task
def sendScheduledEmail(email_to):
#do your stuff
And on your API POST you schedule that:
sendScheduledEmail.apply_async([email_to],eta=datetime.datetime(2021, 07, 06, 09, 30))
I didnt test this, so let me know if you get any errors