Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

SendGrid: Look up a bounced email address

See more SendGrid Examples

Look up a recipient on the bounce suppression list without changing it. SendGrid returns an array. An empty array means there is no bounce record for this lookup; it does not prove a particular message reached the inbox.

Retrieve a bounce

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonArray;
use Chilkat2\JsonObject;

$success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$http = new Http();
$http->RequireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$http->AuthToken = 'SENDGRID_API_KEY';
$resp = new HttpResponse();

// SetUrlVar encodes the raw address as a URL component, including the plus sign.
$http->SetUrlVar('address', 'avery+orders@example.com');

try {
    $http->HttpNoBody('GET', 'https://api.sendgrid.com/v3/suppression/bounces/{$address}', $resp);
} catch (\Chilkat2\Exception $e) {
    if ($resp->StatusCode == 0) {
        echo $e->getMessage(), "\n";
    } else {
        echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
    }

    exit;
}

if ($resp->StatusCode != 200) {
    echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
    exit;
}

$rows = new JsonArray();
try {
    $rows->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo "Unexpected response: expected JSON.\n";
    exit;
}

if ($rows->Size == 0) {
    echo "No bounce record for this address.\n";
    exit;
}

$row = new JsonObject();
$i = 0;
while ($i < $rows->Size) {
    $rows->ObjectAt2($i, $row);
    echo $row->StringOf('email'), ': ', $row->StringOf('status'), ' ', $row->StringOf('reason'), "\n";
    $i = $i + 1;
}