Sample code for 30+ languages & platforms
Perl 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 Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

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

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

$success = $http->HttpNoBody('GET', 'https://api.sendgrid.com/v3/suppression/bounces/{$address}', $resp);
if (!$success) {
    if ($resp->StatusCode == 0) {
        print $http->LastErrorText, "\n";
    } else {
        print 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
    }

    exit;
}

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

my $rows = Chilkat2::JsonArray->new;
$success = $rows->Load($resp->BodyStr);
if (!$success) {
    print "Unexpected response: expected JSON.\n";
    exit;
}

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

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