In my project I am working with Sendgrid
which is used to send bulk transactional or marketing emails. In my project it is working fine. I am able to send transactional emails from my project. But for reading the activities of each email (like open, click, bounce, invalid-emails), I need to go into my sendgrid account and one by one check, which is very tedius and time consuming option. I want to do it automatically in my project. I have read the sendgrid documentation
, but I didn't find any way to get a list of OPEN mails, BOUNCED emails.
could you please help me, how can I do this.
You need to use the WebHooks built into SendGrid. It's called the Event Webhook.
https://sendgrid.com/docs/API_Reference/Webhooks/event.html
Basically you setup the webhook in SendGrid (go to the Settings -> Mail settings) and click on 'Event Notification'. Turn it on and select the events you're interested in. You can use a site called Request bin to test what happens.
http://requestb.in/
I found the easiest way to test this was to fire an email through SendGrid and then see what it sent through requestb.in. I then wrote the API in my system (C# WebApi) based on the data.
I don't know what language / frameworks you're using but here's my API in WebApi/C#.
Api Controller:
[HttpPost]
[Route("api/emailWebHooks")]
public async Task<IHttpActionResult> UpdateEmail(IList<SendGridNotification> sendGridNotifications)
{
Log.Debug("UpdateEmail: " + JsonConvert.SerializeObject(sendGridNotifications));
var updated = await _sendEmailService.UpdateSendGridEmailState(sendGridNotifications);
return Ok(updated);
}
And the class:
public class SendGridNotification
{
/// <summary>
/// The SendGrid event id
/// </summary>
[JsonProperty(PropertyName = "sg_event_id")]
public string SgEventId { get; set; }
/// <summary>
/// The SendGrid message id
/// </summary>
[JsonProperty(PropertyName = "sg_message_id")]
public string SgMessageId { get; set; }
/// <summary>
/// The 'event' property. e.g. Processed
/// </summary>
[JsonProperty(PropertyName = "event")]
public string EventName { get; set; }
/// <summary>
/// The email id - this is unique argument added when the email was sent
/// </summary>
[JsonProperty(PropertyName = "emailId")]
public string EmailId { get; set; }
/// <summary>
/// The email address of the recipient
/// </summary>
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }
/// <summary>
/// SendGrid's smtp id
/// </summary>
[JsonProperty(PropertyName = "smtp-id")]
public string SmtpId { get; set; }
/// <summary>
/// The timestamp of the email
/// </summary>
[JsonProperty(PropertyName = "timestamp")]
public long Timestamp { get; set; }
/// <summary>
/// The IP address - of the server?
/// </summary>
[JsonProperty(PropertyName = "ip")]
public string IpAddress { get; set; }
/// <summary>
/// The http response
/// </summary>
[JsonProperty(PropertyName = "response")]
public string Response { get; set; }
}
SendGrid sends a set of responses so make sure you're API handles a list.
The EmailId in the API is a unique argument I sent to SendGrid so that when this message comes back I know which email the message refers to.
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With