Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to associate sendgrid webhook sg_message_id to sent mail?

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.

like image 511
chaintng Avatar asked Nov 12 '15 04:11

chaintng


People also ask

How do I send a SendGrid API email?

Send your email using the APIPaste the curl call into your favorite text editor. Copy your API key and paste it in the "Authorization" header. In the data section, specify the "to", "from", and "reply to" names and email addresses and enter a subject. Copy the code and paste it in your terminal.


3 Answers

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://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html

like image 145
Dmitry Polyakovsky Avatar answered Oct 05 '22 23:10

Dmitry Polyakovsky


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"
like image 23
Marek Lisý Avatar answered Oct 06 '22 01:10

Marek Lisý


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.

like image 36
VictorA Avatar answered Oct 05 '22 23:10

VictorA