1

I use Sendgrid to send emails. Emails are sent using their web API and I use an endpoint to get each email events.

Here is the code of this endpoint (that Sendgrid call each time when an event occur, after sending an email).

I would like to get the status code of each event (eg : 5.1.1 for a bounce).

Here is what I tried so far :

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

foreach ($events as $event) {
   $sg_message_id = $event['sg_message_id']; //OKAY
   $event =         $event['event'];         //OKAY (eg : "bounce")
   $status =        $event['status'];        //NOT OKAY ("undefined index")

   //and if I try :
   $status = $event['event']['status']; //I get the first letter of the event (eg : "b" for bounce)
}

the documentation (here : https://sendgrid.com/docs/API_Reference/Webhooks/event.html, Bounce part) says there is a field "status" (eg : 5.1.1), and I don't understand why it doesn't work,

Any idea?

Julien
  • 3,743
  • 9
  • 38
  • 67
  • Output `$event` what do you have? – chris85 Nov 30 '16 at 23:26
  • @chris85 $event is an object and the script is called only when an event occur, is it possible to save somehow its content? – Julien Nov 30 '16 at 23:31
  • `$event` should be an array, no? `print_r($event);` is status there? – chris85 Nov 30 '16 at 23:42
  • @chris85 yes $event is an array and the error log says "status" not is there, but the doc above says we can access more information, but I cannot do a print_r because only sendgrid call this page by posting data, is it possible to store this array in a database? – Julien Nov 30 '16 at 23:53
  • @chris85 I finally understood the problem (I posted the answer below), thank anyway chris! – Julien Dec 01 '16 at 00:12

1 Answers1

1

I understood the problem :

I tried to simulate a bounce error using a fake user email like

user@gmail1.com

but Sendgrid didn't process the email (host not reacheable) and returned a "dropped" event.

Instead, if I send an email to:

another-user-unknow@gmail.com

then sendgrid return a process event and then a bounce event, with the bounce status code:)

Julien
  • 3,743
  • 9
  • 38
  • 67