Event Webhook PHP Code Example

Parse Webhook

In this example, we want to parse all emails at address@email.sendgrid.biz and post the parsed email to https://sendgrid.com/email.php.

Given this scenario, the following are the parameters you would set at the Parse API settings page:

Hostname: email.sendgrid.biz
URL: https://sendgrid.com/email.php

To test this scenario, we sent an email to example@example.com and created the following form at https://sendgrid.com/email.php:

<?php
$to = $_POST["to"];
$from = $_POST["from"];
$body = $_POST["text"];
$subject = $_POST["subject"];
$num_attachments = $_POST["attachments"];

if($num_attachments){
  for($i = 1; $i <= $num_attachments; $i++) {
    $attachment = $_FILES['attachment' . $i];
      // $attachment will have all the parameters expected in a the PHP $_FILES object
      // http://www.php.net/manual/en/features.file-upload.post-method.php#example-369
  }
}
?>

Event Webhook

To use the Event Webhook, you must first setup Event Notification.

In this scenario, we assume you've set the Event Notification URL to go the endpoint /parse.php on your server. Given this scenario the following code will allow you to process events:

<?php
$data = file_get_contents("php://input");
$events = json_decode($data, true);

foreach ($events as $event) {
  // Here, you now have each event and can process them how you like
  process_event($event);
}

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the SendGrid tag on Stack Overflow.

Thank you for your feedback!

Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!

Refer us and get $10 in 3 simple steps!

Step 1

Get link

Get a free personal referral link here

Step 2

Give $10

Your user signs up and upgrade using link

Step 3

Get $10

1,250 free SMSes
OR 1,000 free voice mins
OR 12,000 chats
OR more