Sendgrid v3 mail/send: 是否可以发送幂等请求?

huangapple go评论53阅读模式
英文:

Sendgrid v3 mail/send: Is it possible to send idempotent request?

问题

使用SendGrid的v3 API是否可以发送幂等请求?我正在开发一个系统,可能会导致多次尝试发送相同的电子邮件,因此如果我们可以在请求中传递一个唯一的ID,并使用“如果您已经处理过具有相同唯一ID的请求,请忽略此请求”的逻辑,那将非常有帮助。

英文:

With SendGrid's v3 API is it possible to send an idempotent request? I'm working on a system that could result in multiple attempts for the same email send so it would be helpful if we could somehow, for instance, pass a unique ID in the request and use the logic of "ignore this request if you've already processed a request with the same unique ID"?

答案1

得分: 1

SendGrid的API不支持幂等性,并且将所有的POST请求随意发送到Mail Send端点,不验证重复请求。

避免此行为的替代方法是跟踪已发送的电子邮件,使用唯一标识符,就像您所要求的那样。SendGrid不提供任何跟踪已交付标识符的方法,因此您需要自行将它们存储在数据库中。

要确保只跟踪成功发送的邮件的唯一标识符,可以将它们附加到每封电子邮件中,使用唯一参数,这些参数将随电子邮件一起发送,并且稍后可以通过事件Webhooks接收。

使用自定义参数

要将唯一参数附加到邮件发送,请将它们附加到X-SMTPAPI标头。如果您使用Node.js库,可以通过在邮件对象的customArgs字段中添加一对键值条目来实现:

const SendGrid = require("@sendgrid/mail");
SendGrid.setApiKey(SENDGRID_API_KEY);

const Mail = {
    to: "johndoe@email.com",
    from: "company@email.com",
    cc: [],
    subject: "My Email",
    customArgs: {
        unique_id: "YOUR_UNIQUE_IDENTIIFER_HERE"
    }
}

SendGrid.send(Mail);

现在,每封发送的电子邮件都将包含您的唯一标识符,您可以稍后设置事件Webhook到您的端点来跟踪它,详见开始使用事件Webhook安全功能,所有库中都提供了Webhook助手来简化此过程。

事件Webhook负载

您将开始捕获包含交付和参与数据的邮件事件,但我们只关心存储我们的唯一标识符。

以下是邮件“delivered”事件的示例Webhook负载:

[
  {
    "email": "johndoe@email.com",
    "timestamp": 1513299569,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "delivered",
    "ip": "168.1.1.1",
    "category": "cat facts",
    "sg_event_id": "sg_event_id",
    "sg_message_id": "sg_message_id",
    "unique_id": "YOUR_UNIQUE_IDENTIIFER_HERE",
    "response": "250 OK"
  }
]

(对于其他事件类型,您可以在这里查看示例。)

您将看到我们的unique_id附加在与邮件相关的事件中,然后可以将其存储在数据库中,您将拥有已确认的邮件发送与唯一标识符的集合。

现在,唯一要做的是在发送电子邮件之前查询此数据库,以确保它之前未被成功交付。

英文:

SendGrid's API is not idempotent-safe and will dispatch all POST requests to the Mail Send endpoint at will without any validation for duplicate requests.

The alternative to avoiding this behaviour is to track which emails have been sent by a unique identifier just as you have requested. SendGrid does not offer any way of keeping a track of delivered identifiers, so you will have to store them yourself in a database.

What you can do to ensure that you only track the unique identifiers of mail sends that were successful and not invalid is to attach them to the email using unique arguments with each email, these are delivered with the email and can later be received through Event Webhooks.

Using Custom Arguments

To attach unique arguments to a mail send, you can attach them to the X-SMTPAPI headers. If you are using the Node.js library, this is achieved by adding a pair of key-value entries to the customArgs field in the mail object:

const SendGrid = require(&quot;@sendgrid/mail&quot;);
SendGrid.setApiKey(SENDGRID_API_KEY);

const Mail = {
    to: &quot;johndoe@email.com&quot;,
    from: &quot;company@email.com&quot;,
    cc: [],
    subject: &quot;My Email&quot;,
    customArgs: {
        unique_id: &quot;YOUR_UNIQUE_IDENTIIFER_HERE&quot;
    }
}

SendGrid.send(Mail);

Every dispatched email will now contain your unique identifier, for which you can then later track by setting up event webhooks to your endpoint, see Getting Started with the Event Webhook Security Features, there are webhook helpers available in all libraries to make this easier.

Event Webhook Payload

You will begin to capture mail events that contain delivery and engagement data, though we are interested in storing our unique identifier.

Here is an sample webhook payload for the mail &quot;delivered&quot; event:

[
  {
    &quot;email&quot;: &quot;johndoe@email.com&quot;,
    &quot;timestamp&quot;: 1513299569,
    &quot;smtp-id&quot;: &quot;&lt;14c5d75ce93.dfd.64b469@ismtpd-555&gt;&quot;,
    &quot;event&quot;: &quot;delivered&quot;,
    &quot;ip&quot;: &quot;168.1.1.1&quot;,
    &quot;category&quot;: &quot;cat facts&quot;,
    &quot;sg_event_id&quot;: &quot;sg_event_id&quot;,
    &quot;sg_message_id&quot;: &quot;sg_message_id&quot;,
    &quot;unique_id&quot;: &quot;YOUR_UNIQUE_IDENTIIFER_HERE&quot;,
    &quot;response&quot;: &quot;250 OK&quot;
  }
]

(For other event types, you can see examples here.)

You'll see that our unique_id is attached within the events related to the mail, this can then be stored in a database and you will have a collection of confirmed mail sends with unique identifiers.

All that is left to do is to simply query this database with your unique identifier before dispatching emails to ensure that it has not previously been delivered.

huangapple
  • 本文由 发表于 2023年6月2日 03:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定