I need to send an emails (bulk sending), and I have two cases:
- The same email sent into multiple recipients
- Different emails sent into different recipients
How, in both cases, I can control statuses?
First case will return single x-message-id, but how it can be translated into separate status for each recipients? (delivered, opened and so on?)
Second case, I believe, need to be done by sending one-by one, is that correct? Or there is also method to send bulk emails in that case?
I'm using C# and official .NET library.
To send the same email sent to multiple recipients:
MailHelper.CreateSingleEmailToMultipleRecipientsand you'll need to specify the text and HTML content in code. (Sample)MailHelper.CreateSingleTemplateEmailToMultipleRecipientsmethod which will use a SendGrid Dynamic Email Template. In that case the email template is already stored in SendGrid and you simply provide the ID of the template.showAllRecipientsnamed parameter which defaults tofalse. Whenfalserecipients cannot see other recipients, whentruerecipients can see other recipients.To send different email to different people:
MailHelper.CreateMultipleEmailsToMultipleRecipientswhich will use a single text and HTML body template, but you can add substitution tags and pass in substitution data so the email will be personalized for every recipient. (Sample)MailMessagebut use multiplePersonalizationobjects to override any of theMailMessageproperties for specific recipients. (Sample)MailMessageobjects and submit them individually to the API, although the above options are made for this scenario.To send any of these emails, your API key must have the
mail.sendpermission.There are additional ways to send bulk email with SendGrid, which I documented here in much more detail.
As you noted, when you send email in bulk, only a single message ID is returned via the
x-message-idheader. When you later try to retrieve the details using that message ID, you will not find any messages. That is because the message ID in thex-message-idheader is used as a base ID and the real message IDs will append another ID. For example,x-message-idwill return"W86EgYT6SQKk0lRflfLRsA", but when you retrieve one of the individual messages, it will looks something like this:"W86EgYT6SQKk0lRflfLRsA.filterdrecv-5645d9c87f-78xgx-1-62841247-82.1".You can retrieve these messages via the E-Mail Activity API like this:
Take note of the
queryproperty which is has the$"msg_id LIKE '{messageId}%'"value. This query will filter to messages that begin with the message ID returned from the bulk email (messageId), and as a result retrieve all the individual messages. You cannot query these messages immediately after submitting the bulk email to SendGrid's queue as it takes some time for them to become available through the API. In my code I queried these messages every 30 seconds until the count of the recipients in the bulk email matched the messages returned.The resulting JSON data looks like this:
As you can see, this includes the
statusproperty.Note: You must purchase additional email activity history to gain access to the Email Activity Feed API.
Note: To retrieve message via the Email Activity Feed API, your API key must have the
email_activity.readpermission.This would be one way to retrieve the status of your email messages. Another way to keep track of the status of email messages would be to create a public ASP.NET endpoint configure the URL as the SendGrid Event Webhook. SendGrid will send an HTTP request to your ASP.NET endpoint for every event which you can use to update the status of email messages.
PS: You may already be doing this, but whether you're sending a single email to multiple recipients or multiple emails to multiple recipients, Twilio recommends setting the
SendAtproperty on theSendGridMessagewhen sending bulk emails.Quoting from the SendGrid docs:
I hope that answered all your questions, if not let me know. I can't wait to see what you build!