Search code examples
sendgrid

SendGrid send mail - how to keep track of emails?


I am sending emails from sendgrid from my backend. When I send the email, the response is 202 and the response body is empty. Since the response body is empty, i do not have any information like an ID to track the email events in my system.

When I go to fetch the events, i see they have an ID, but since I don't have that ID from their creation, I cant map emails I sent to events I received.

Can you give an email an ID or label it somehow when you send it? So that you can map al the events from it?

What we are trying to do is to have a list of "transactions" where each "Transaction" is affiliated with an email. We want to provide email event data in our app, but can't figure out how to map


Solution

  • The response body of a successful Mail Send API call is empty, but the response contains a x-message-id header which you can use to retrieve details about the messages being sent.

    Since one API call to the Mail Send API can result in multiple emails being sent, this ID identifies the group of messages, and they'll all be prefixed with the x-message-id. For example, if x-message-id returns W86EgYT6SQKk0lRflfLRsA, the IDs of your messages will start with that and then be suffixed with something like .filterdrecv-5645d9c87f-78xgx-1-62841247-82.1. Together it'd look like W86EgYT6SQKk0lRflfLRsA.filterdrecv-5645d9c87f-78xgx-1-62841247-82.1.

    If you want to track the messages, you can use the event webhook or pull the information via the Email Activity Feed API.

    This Stack Overflow Q&A about tracking bulk email statuses may also be useful to look at.