SendGrid Event Webhook – PHP Example | Gary Smith's Blog

Programmer & tech geek

SendGrid Event Webhook – PHP Example

I’ve been on the Internet since the late 80’s and an Internet Service Provider since the mid 90’s, so I’ve always had an interest in email deliverability, diagnosing delivery issues and efficiency of email delivery mechanisms. I use¬†websites such as MX Toolbox, and to make sure our email reputation is good as this ensures maximum delivery. For the past couple of years I’ve been working with a company that sends out around 1 million emails to subscribers each month.

Initially we had our own SMTP server and I managed our email reputation, responded to spam complaints, removed dead email addresses etc, but in February this year we decided to switch to as our SMTP provider. This freed up some of my time as they take care of the bounces and spam complaints, but being a hands on kind of guy, I don’t enjoy ‘blissful ignorance’. With this in mind I started looking at what options there were for me to receive some notifications of important events such as spam complaints, drops, deferrals and bounces. I’d like to be able to automatically remove bounced email addresses from our lists and also those who report our emails as spam (unfortunately many people still don’t see the very prominent unsubscribe link at the bottom of our emails).

Enter the SendGrid Event Webhook. This is a handy little app in SendGrid that will post details, in JSON format, to the URL you configure in the app. You can also choose which notifications you want to receive and they have a simple PHP code sample for dumping the data to a text file.

My code goes a little bit further and shows you how to parse the JSON into an associative array and then send the notification to you via email. I ended up using¬†file_get_contents(‘php://input’) as I wasn’t getting anything in $HTTP_RAW_POST_DATA, even with it turned on in my php.ini. It may have been because I use CodeIgniter or it may have been one of the security measures on the server that was causing an issue.


	  function sg_webhook()
		// Get the incoming raw post data into an array
		$jarray = json_decode($result,true);
		// Initialise our message body and set the message subject
		$subject="Notification From Sendgrid";
                // Parse the array
                foreach($jarray as $key => $val)
			foreach($val as $label => $value)
				$msg.="\n".$label." = ".$value;
                // The rest of this uses the CodeIgniter email lib - just change it to your mail function/class of choice
		$this->email->from('','Sendgrid Notification');

It’s very basic, but I’ll be able to use it as the basis for some further automation to remove bad addresses from our mailing lists. You can find out more about the SendGrid Event Webhook HERE.

One Comment

  • Thanks mate, using CI too, spent a minute trying to figure out why $this->input->post() didn’t work. This worked fine!

Post a Comment

Your email address will not be published. Required fields are marked *

  • Recent Rides

  • Recent Posts

  • Archives

  • Tags