![](/img/trans.png)
[英]Email to SMS using SendGrid InboundParse and Twilio
[英]How to schedule an email using twilio sendgrid in django?
我目前正在構建一個應用程序,其中包含 email 發送給多個用戶,我可以這樣做,但我想添加一個計划為 email 的功能,例如我正在使用sent_at
方法,如下所示:-
設置.py
EMAIL_FROM = 'EMAIL'
EMAIL_API_CLIENT ='XXXXXXXX'
視圖.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.....
我也試過message.extra_headers = {'SendAt':FinalScheduleTime}
但它也不起作用。
這里的 FinalScheduleTime 是日期時間 object。 sendgrip api 根據文檔接受 UNIX 時間戳。 你可以在這里查看
因此,要將您的日期時間 object 轉換為 unix 時間戳,您可以使用 python 的時間模塊。
scheduleTime = int(time.mktime(FinalScheduleTime.timetuple()))
此外,將 message.extra_headers 替換為 message.send_at。
因此,您的最終代碼將如下所示:
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
這里還有官方文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.