【问题标题】:Sendgrid - Trigger email via webhook from non-zapierSendgrid - 通过来自非 zapier 的 webhook 触发电子邮件
【发布时间】:2021-09-11 17:51:17
【问题描述】:

我已经做了很多研究,但似乎无法找到答案。 Sendgrid 有关于在发送电子邮件后发送信息的事件 webhook 的大量文档,但在指向 sendgrid 以发送电子邮件的入站 webhook 上却很少。

This is the only article I could find - but it's uniquely centered around Zapier which I'd rather not use.

有没有人有关于我如何使用指向 Sendgrid 的 webhook 来触发电子邮件而不使用 Zapier 的任何资源?

谢谢!

【问题讨论】:

    标签: email webhooks sendgrid


    【解决方案1】:

    这里是 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)
    

    【讨论】:

    • 你好@philnash - 啊,我的名字有点过时了。我目前正在尝试使用 Memberstack memberstack.com 建立一个会员模型的网站,目标是在有人注册后触发一封欢迎电子邮件。我相信他们使用 webhook 触发了这个事务性事件,并且他们所有的 API 都不是基于事件的。我将如何整合它?我完全理解本教程,但我不太清楚如何使其“基于事件”而不是“基于时间”。在这种情况下,我使用的是 Javascript!
    • 啊,我明白了。因此,在这种情况下,Memberstack 将在您的用户注册时发送一个 webhook。您需要一个可以接收该 webhook(HTTP 请求)的应用程序,然后运行将发送电子邮件的代码。如果你使用 JavaScript,那么你需要一个 Web 服务器,比如 Express,来接收这些传入的 HTTP 请求。或者您可以将其实现为Twilio Function。这有帮助吗?
    • 是的!我使用 netlify,我认为它们也具有无服务器功能,所以我会尝试一下。谢谢你的澄清。我非常困惑,因为我认为 webhook 可以通过某种方式将 sendgrid 指向端点,但无法弄清楚
    • 啊,是的,Netlify 确实有功能,他们可以做同样的事情。您真的不希望能够将任何 webhook 指向 URL 并让它代表您发送电子邮件,可能需要进行一些身份验证和一些自定义。这就是您可以在 Netlify 函数中执行的操作,将 Memberstack webhook 中的信息转换为您要发送的电子邮件。希望你一切顺利。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多