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

Ruby
require 'chilkat'

success = false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
#  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY")
resp = Chilkat::CkHttpResponse.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 != true)
    if (resp.get_StatusCode() == 0)
        print http.lastErrorText() + "\n";
    else
        print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
    end

    exit
end

if (resp.get_StatusCode() != 200)
    print "HTTP " + resp.get_StatusCode().to_s() + ": " + resp.bodyStr() + "\n";
    exit
end

rows = Chilkat::CkJsonArray.new()
success = rows.Load(resp.bodyStr())
if (success != true)
    print "Unexpected response: expected JSON." + "\n";
    exit
end

if (rows.get_Size() == 0)
    print "No bounce record for this address." + "\n";
    exit
end

row = Chilkat::CkJsonObject.new()
i = 0
while (i < rows.get_Size())
    rows.ObjectAt2(i,row)
    print row.stringOf("email") + ": " + row.stringOf("status") + " " + row.stringOf("reason") + "\n";
    i = i + 1
end