简体   繁体   English

如何在 django 中使用 twilio sendgrid 安排 email?

[英]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.

相关问题 Email 使用 SendGrid InboundParse 和 Twilio 到 SMS - Email to SMS using SendGrid InboundParse and Twilio Django - Twilio 计划消息未发送 - Django - Twilio schedule message is not sending 在 Heroku 中为 Django 应用程序使用 Sendgrid 设置电子邮件 - Setting up email with Sendgrid in Heroku for a Django App 如何将 email 添加到 Sendgrid 的自动化中? - How does one add an email to an automation in Sendgrid? Django - 安排时间 WhatsApp 消息通过 Twilio 发送 - Django - schedule time WhatsApp message send through Twilio PHPMailer 使用 Sendgrid,自定义每个 email 但只发送一个 email - PHPMailer using Sendgrid, customizing each email but sending just one email How to send HTML email with SendGrid Rest API using custom html file with Nodejs handlebars? - How to send HTML email with SendGrid Rest API using custom html file with Nodejs handlebars? 如何在 .NET 应用程序中使用 SendGrid V3 将 email 发送给多个收件人 - how to send an email using SendGrid V3 to multiple recipients, in a .NET app 通过使用 SendGrid 的单独 email 验证注册定制 - Sign up customization with separate email verification using SendGrid 当应用程序需要 email 地址作为用户名时,如何使用 SendGrid SMTP? - How to use SendGrid SMTP when application requires email address as username?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM