【问题标题】:How to programmatically delete an email from list of bounced emails?如何以编程方式从退回的电子邮件列表中删除电子邮件?
【发布时间】:2019-08-07 13:33:10
【问题描述】:

我正在开发一个基于 GAE(Google App Engine)的 python 应用程序,其中集成了 sendgrid python SDK(v3.2.10)。我现在正在尝试做的是,每当 sendgrid 推送类型为“bounce”的事件 webhook 时,我想从 sendgrid 上的退回电子邮件列表中删除该退回的电子邮件。

我已经浏览了官方网站上提供的文档。首先,我尝试使用 SDK 删除电子邮件地址,它在 localhost 上运行良好。但是在将其部署到实时服务器后,它什么也不做,属于例外条款。

代码sn-p:

try:
    send_grid_client = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
    data = {"emails": [email.strip()]}
    delete_response = send_grid_client.client.suppression.bounces.delete(
                                    request_body=data)
except Exception as exception:
    logging.info('Exception is: {}'.format(exception))
    pass

由于它没有按预期工作,我现在尝试使用 REST API 来做同样的事情。

代码sn-p:

import requests
data = {"emails": [email]}
headers = {"Authorization": "Bearer {}".format(SENDGRID_API_KEY)}
delete_response = requests.delete("https://api.sendgrid.com/v3/suppression/bounces", data=json.dumps(data), headers=headers)
logging.info(delete_response)
logging.info(delete_response.status_code)
logging.info(delete_response.text)

现在,sendgrid API 不断返回错误 400,并带有消息 {"errors":[{"field":null,"message":"emails or delete_all params required"}]}。我根本不知道如何克服这个问题。也许我错过了如何在 delete 函数中传递请求正文,但是我想不通。

【问题讨论】:

    标签: python python-requests sendgrid sendgrid-api-v3 sendgrid-api-v2


    【解决方案1】:

    我刚刚发现了这个问题。

    是 SendGrid API 文档here 引起了混淆,因为没有明确提到当您要删除单个电子邮件地址或电子邮件列表时,它们具有不同的调用同一端点的方式。

    对于单个电子邮件,它需要在 URL 中传递,即https://api.sendgrid.com/v3/suppression/bounces/{email_address}

    对于邮件列表,该列表需要在删除请求的正文中传递。即它看起来像这样{"emails": [email_address_1, email_address_1, ...]}

    在上面的问题中,一封电子邮件应该被删除,它在删除请求中以{"emails": [email_address_1]} 的形式传递。 Sendgrid API 无法消化此信息并引发错误。电子邮件地址将在 URL 中传递。

    此问题已解决。但是,我想知道为什么 Sendgrid API 无法消化此信息{"emails": [email_address_1]}。为什么他们很难假设列表中的元素总是大于 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多