Julien
Julien

Reputation: 4163

Sendgrid : get bounce status code with webhooks (php api)

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?

Upvotes: 1

Views: 1178

Answers (1)

Julien
Julien

Reputation: 4163

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:)

Upvotes: 1

Related Questions