1
votes

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?

1
Output $event what do you have? - chris85
@chris85 $event is an object and the script is called only when an event occur, is it possible to save somehow its content? - Julien
$event should be an array, no? print_r($event); is status there? - chris85
@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
@chris85 I finally understood the problem (I posted the answer below), thank anyway chris! - Julien

1 Answers

1
votes

I understood the problem :

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

[email protected]

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

Instead, if I send an email to:

[email protected]

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