How to associate sendgrid webhook sg_message_id to sent mail?
Asked Answered
S

4

29

Sendgrid webhook has sg_message_id But the response of Web API v2 when sending mail doesn't have sg_message_id. In fact, there is only message showing only success or failed.

So, how do i associate between sent mail and the webhook callback?

I have check the past questions in SO but it doesn't provide be the solution for this.

Thank you.

Stallard answered 12/11, 2015 at 4:32 Comment(0)
P
39

You can use unique arguments. Take your unique ID (like primary key) and give it to sendgrid API during sending email. You will get it back in the event webhook.

https://docs.sendgrid.com/for-developers/sending-email/unique-arguments

When you want to use "unique arguments" through SMTP you should be able to do so using the X-SMTPAPI message header.

https://docs.sendgrid.com/for-developers/sending-email/building-an-x-smtpapi-header

Po answered 8/12, 2015 at 21:45 Comment(5)
This must be accepted as answer. Thanks Dmitry! Actually this is something that must be added to the Sendgrid docs!!!Georgie
Is there something analogous for the v3 SendGrid API?Dynamoelectric
@Dynamoelectric yes, for the v3 API, use custom_argsShipshape
Note: if your hook fails and you need retrospect data you will need to pay undisclosed amount for Email Activity API. (Price is not shown for free users for some reason).Hardi
Here's the v3 SendGrid documentation for custom_args / unique_args.Corncrib
P
23

EDIT: The best practice seems to be using custom arguments (see documentation). Which is not apparent from the documentation (as of today).


In Web API v3, the response includes a special header X-Message-Id (see https://sendgrid.com/docs/Glossary/x_message_id.html).

This can be later matched with sg_message_id in webhook: https://sendgrid.com/docs/Glossary/message_id.html

The documentation does not make it clear, but the X-Message-Id header is actually not equal to sg_message_id, but it is a prefix of sg_message_id.

For example:

  • X-Message-Id = "MUvGg3V1ThOu3oe8eRqFrA"
  • sg_message_id = "MUvGg3V1ThOu3oe8eRqFrA.filter0001p2iad2-21183-5AF0BD9B-E.0"
Palaeozoic answered 7/5, 2018 at 12:30 Comment(4)
I would advise against this; it is entirely possible that this type of matching does not work 100% of the time. As you noted in your edit a custom argument avoids this possibilityShipshape
Me too, actually. At the time, the documentation told everywhere people should use those X-Message-Id, however the implementation was all kind of weird...Sunshine
After those 30 days - if user will do any activity with that message (like open it again or click) after i.e. 40 days (so beyond 30 days limit) will that message come back to email activity history?Lonne
I would not use it. This pattern can be changed by sendgrid any day any time. Don't depend on it.Bagworm
O
1

The Web APIv2 call that sends out the email only responds with a {message :"success"} answer indeed, but the sg_message_id is not generated at that point. The API call answer simply acknowledges that the email was sent to SendGrid for processing.

The email is sent to SendGrid, and there it receives the sg_message_id value that is provided through the Event Webhook posts.

Obsolete answered 12/11, 2015 at 8:42 Comment(1)
So, it is noway to match the request & webhook? what do u think?Stallard
D
0

Thank you for the answers above!!! You got me on the right track! For anyone else using ColdFusion, this was the exact syntax I needed with cfmail for the data to get sent back with the SendGrid webhook data:

<cfmailparam 
    name="X-SMTPAPI"
    value = '{"unique_args:{"email_guid":"#email_guid#"}}'>
Dita answered 12/7, 2024 at 21:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.