[英]How to schedule an email using twilio sendgrid in django?
I'm currently building an app which contains email sending to multiple users which i'm able to do but i want to add a functionality which schedule's an email, for instance I'm using sent_at
method as you can see below:-我目前正在构建一个应用程序,其中包含 email 发送给多个用户,我可以这样做,但我想添加一个计划为 email 的功能,例如我正在使用
sent_at
方法,如下所示:-
settings.py设置.py
EMAIL_FROM = 'EMAIL'
EMAIL_API_CLIENT ='XXXXXXXX'
views.py视图.py
import json
from sendgrid import SendGridAPIClient
from django.conf import settings
message = Mail(from_email=settings.EMAIL_FROM,
to_emails=selectedphone,
subject=subject,
html_content=editor)
message.extra_headers = {'X-SMTPAPI': json.dumps({'send_at':
FinalScheduleTime})}
sg = SendGridAPIClient(settings.EMAIL_API_CLIENT)
response = sg.send(message)
if response.status_code == 202:
emailstatus = "Accepted"
elif .....
else.....
I've also tried message.extra_headers = {'SendAt':FinalScheduleTime}
but it's not working either.我也试过
message.extra_headers = {'SendAt':FinalScheduleTime}
但它也不起作用。
Here the FinalScheduleTime is of the datetime object.这里的 FinalScheduleTime 是日期时间 object。 The sendgrip api accepts the UNIX timestamp according to the documentation.
sendgrip api 根据文档接受 UNIX 时间戳。 You can check it here
你可以在这里查看
Hence to convert your datetime object into unix time stamp, you can use the time module of python.因此,要将您的日期时间 object 转换为 unix 时间戳,您可以使用 python 的时间模块。
scheduleTime = int(time.mktime(FinalScheduleTime.timetuple()))
Also, replace the message.extra_headers with message.send_at.此外,将 message.extra_headers 替换为 message.send_at。
Hence, your final code will look like:因此,您的最终代码将如下所示:
import json
import time
from sendgrid import SendGridAPIClient
from django.conf import settings
message = Mail(from_email=settings.EMAIL_FROM,
to_emails=selectedphone,
subject=subject,
html_content=editor)
scheduleTime = int(time.mktime(FinalScheduleTime.timetuple()))
message.send_at = scheduleTime
sg = SendGridAPIClient(settings.EMAIL_API_CLIENT)
response = sg.send(message)
if response.status_code == 202:
emailstatus = "Accepted"
elif .....
else.....
This is an official blog by Twilio on Using Twilio SendGrid To Send Emails from Python Django Applications - https://www.twilio.com/blog/scheduled-emails-python-flask-twilio-sendgrid This is an official blog by Twilio on Using Twilio SendGrid To Send Emails from Python Django Applications - https://www.twilio.com/blog/scheduled-emails-python-flask-twilio-sendgrid
Also here are, official docs这里还有官方文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.