【发布时间】:2021-09-11 17:51:17
【问题描述】:
我已经做了很多研究,但似乎无法找到答案。 Sendgrid 有关于在发送电子邮件后发送信息的事件 webhook 的大量文档,但在指向 sendgrid 以发送电子邮件的入站 webhook 上却很少。
有没有人有关于我如何使用指向 Sendgrid 的 webhook 来触发电子邮件而不使用 Zapier 的任何资源?
谢谢!
【问题讨论】:
我已经做了很多研究,但似乎无法找到答案。 Sendgrid 有关于在发送电子邮件后发送信息的事件 webhook 的大量文档,但在指向 sendgrid 以发送电子邮件的入站 webhook 上却很少。
有没有人有关于我如何使用指向 Sendgrid 的 webhook 来触发电子邮件而不使用 Zapier 的任何资源?
谢谢!
【问题讨论】:
这里是 Twilio SendGrid 开发人员宣传员。
当您想在诸如 SendGrid 之类的第三方服务上触发某些内容时,它被描述为 API 请求而不是 Webhook。要使用 SendGrid 发送电子邮件,您需要向 SendGrid API 发出 API 请求。
我看到你的用户名是 Python Learner,这是how to send an email with Python using the SendGrid API 的教程。
本教程生成的代码如下所示,但我建议您通读整个教程,因为有很多东西要学习:
import sendgrid
import os
from sendgrid.helpers.mail import Mail, Email, To, Content
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com") # Change to your verified sender
to_email = To("test@example.com") # Change to your recipient
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)
# Get a JSON-ready representation of the Mail object
mail_json = mail.get()
# Send an HTTP POST request to /mail/send
response = sg.client.mail.send.post(request_body=mail_json)
print(response.status_code)
print(response.headers)
【讨论】: