Sample code for 30+ languages & platforms
Visual FoxPro

Amazon SES v2: Check an address on the account suppression list

See more Amazon SES Examples

Look up one address in the sending Region with ses:GetSuppressedDestination. The record distinguishes a bounce from a complaint. An absent record tells you only that this account suppression lookup found no entry. The example signs the exact encoded mailbox path and also works with temporary credentials.

GetSuppressedDestination

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcAddress
LOCAL lcRegion
LOCAL lcAccessKey
LOCAL lcSecretKey
LOCAL lcSessionToken
LOCAL loHost
LOCAL loMailbox
LOCAL loPath
LOCAL loCanonicalPath
LOCAL lnReplaced
LOCAL loClock
LOCAL loStamp
LOCAL lcDate
LOCAL loRest
LOCAL loHeaders
LOCAL lcSignedHeaders
LOCAL loCrypto
LOCAL loCanonical
LOCAL loScope
LOCAL loToSign
LOCAL loSeed
LOCAL lcKey
LOCAL lcSignature
LOCAL loAuthorization
LOCAL loConnection
LOCAL lcResponseText
LOCAL loResponse

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0

*  Look up the address exactly as stored, including any plus tag.
lcAddress = "avery+orders@example.com"
lcRegion = "us-west-2"
lcAccessKey = "AWS_ACCESS_KEY_ID"
lcSecretKey = "AWS_SECRET_ACCESS_KEY"
lcSessionToken = ""
loHost = CreateObject('Chilkat.StringBuilder')
loHost.Append("email.")
loHost.Append(lcRegion)
loHost.Append(".amazonaws.com")
loMailbox = CreateObject('Chilkat.StringBuilder')
loMailbox.Append(lcAddress)
loPath = CreateObject('Chilkat.StringBuilder')
loPath.Append("/v2/email/suppression/addresses/")
loPath.Append(loMailbox.GetEncoded("url_rfc3986","utf-8"))

*  SigV4 for SES encodes the wire path a second time in the canonical request.
*  Only the canonical copy escapes the percent signs. Send the original path over HTTPS.
loCanonicalPath = CreateObject('Chilkat.StringBuilder')
loCanonicalPath.Append(loPath.GetAsString())
lnReplaced = loCanonicalPath.Replace("%","%25")
loClock = CreateObject('Chilkat.CkDateTime')
loClock.SetFromCurrentSystemTime()
loStamp = CreateObject('Chilkat.StringBuilder')
loStamp.Append(loClock.GetAsTimestamp(0))
lnReplaced = loStamp.Replace("-","")
lnReplaced = loStamp.Replace(":","")
lcDate = loStamp.GetRange(0,8,0)
loRest = CreateObject('Chilkat.Rest')
loRest.AddHeader("Host",loHost.GetAsString())
loRest.AddHeader("x-amz-date",loStamp.GetAsString())
loHeaders = CreateObject('Chilkat.StringBuilder')
loHeaders.Append("host:")
loHeaders.Append(loHost.GetAsString())
loHeaders.Append(CHR(10) + "x-amz-date:")
loHeaders.Append(loStamp.GetAsString())
loHeaders.Append(CHR(10))
lcSignedHeaders = "host;x-amz-date"
IF (lcSessionToken <> "") THEN
    loRest.AddHeader("x-amz-security-token",lcSessionToken)
    loHeaders.Append("x-amz-security-token:")
    loHeaders.Append(lcSessionToken)
    loHeaders.Append(CHR(10))
    lcSignedHeaders = "host;x-amz-date;x-amz-security-token"
ENDIF

loCrypto = CreateObject('Chilkat.Crypt2')
loCrypto.HashAlgorithm = "sha256"
loCrypto.MacAlgorithm = "hmac"
loCrypto.EncodingMode = "hexlower"
loCrypto.Charset = "utf-8"
loCanonical = CreateObject('Chilkat.StringBuilder')
loCanonical.Append("GET" + CHR(10))
loCanonical.Append(loCanonicalPath.GetAsString())
loCanonical.Append(CHR(10) + CHR(10))
loCanonical.Append(loHeaders.GetAsString())
loCanonical.Append(CHR(10))
loCanonical.Append(lcSignedHeaders)
loCanonical.Append(CHR(10))
loCanonical.Append(loCrypto.HashStringENC(""))
loScope = CreateObject('Chilkat.StringBuilder')
loScope.Append(lcDate)
loScope.Append("/")
loScope.Append(lcRegion)
loScope.Append("/ses/aws4_request")
loToSign = CreateObject('Chilkat.StringBuilder')
loToSign.Append("AWS4-HMAC-SHA256" + CHR(10))
loToSign.Append(loStamp.GetAsString())
loToSign.Append(CHR(10))
loToSign.Append(loScope.GetAsString())
loToSign.Append(CHR(10))
loToSign.Append(loCrypto.HashStringENC(loCanonical.GetAsString()))

*  Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
loSeed = CreateObject('Chilkat.StringBuilder')
loSeed.Append("AWS4")
loSeed.Append(lcSecretKey)
loCrypto.SetMacKeyString(loSeed.GetAsString())
lcKey = loCrypto.MacStringENC(lcDate)
loCrypto.SetMacKeyEncoded(lcKey,"hex")
lcKey = loCrypto.MacStringENC(lcRegion)
loCrypto.SetMacKeyEncoded(lcKey,"hex")
lcKey = loCrypto.MacStringENC("ses")
loCrypto.SetMacKeyEncoded(lcKey,"hex")
lcKey = loCrypto.MacStringENC("aws4_request")
loCrypto.SetMacKeyEncoded(lcKey,"hex")
lcSignature = loCrypto.MacStringENC(loToSign.GetAsString())
loAuthorization = CreateObject('Chilkat.StringBuilder')
loAuthorization.Append("AWS4-HMAC-SHA256 Credential=")
loAuthorization.Append(lcAccessKey)
loAuthorization.Append("/")
loAuthorization.Append(loScope.GetAsString())
loAuthorization.Append(", SignedHeaders=")
loAuthorization.Append(lcSignedHeaders)
loAuthorization.Append(", Signature=")
loAuthorization.Append(lcSignature)
loRest.AddHeader("Authorization",loAuthorization.GetAsString())
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
lnSuccess = loConnection.Connect(loHost.GetAsString(),443,1,10000)
IF (lnSuccess <> 1) THEN
    ? loConnection.LastErrorText
    RELEASE loHost
    RELEASE loMailbox
    RELEASE loPath
    RELEASE loCanonicalPath
    RELEASE loClock
    RELEASE loStamp
    RELEASE loRest
    RELEASE loHeaders
    RELEASE loCrypto
    RELEASE loCanonical
    RELEASE loScope
    RELEASE loToSign
    RELEASE loSeed
    RELEASE loAuthorization
    RELEASE loConnection
    CANCEL
ENDIF

lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loMailbox
    RELEASE loPath
    RELEASE loCanonicalPath
    RELEASE loClock
    RELEASE loStamp
    RELEASE loRest
    RELEASE loHeaders
    RELEASE loCrypto
    RELEASE loCanonical
    RELEASE loScope
    RELEASE loToSign
    RELEASE loSeed
    RELEASE loAuthorization
    RELEASE loConnection
    CANCEL
ENDIF

lcResponseText = loRest.FullRequestNoBody("GET",loPath.GetAsString())
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loMailbox
    RELEASE loPath
    RELEASE loCanonicalPath
    RELEASE loClock
    RELEASE loStamp
    RELEASE loRest
    RELEASE loHeaders
    RELEASE loCrypto
    RELEASE loCanonical
    RELEASE loScope
    RELEASE loToSign
    RELEASE loSeed
    RELEASE loAuthorization
    RELEASE loConnection
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode = 404) THEN
    ? "No account suppression entry for this address in this Region."
    RELEASE loHost
    RELEASE loMailbox
    RELEASE loPath
    RELEASE loCanonicalPath
    RELEASE loClock
    RELEASE loStamp
    RELEASE loRest
    RELEASE loHeaders
    RELEASE loCrypto
    RELEASE loCanonical
    RELEASE loScope
    RELEASE loToSign
    RELEASE loSeed
    RELEASE loAuthorization
    RELEASE loConnection
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "SES returned HTTP " + STR(loRest.ResponseStatusCode) + " " + lcResponseText
    RELEASE loHost
    RELEASE loMailbox
    RELEASE loPath
    RELEASE loCanonicalPath
    RELEASE loClock
    RELEASE loStamp
    RELEASE loRest
    RELEASE loHeaders
    RELEASE loCrypto
    RELEASE loCanonical
    RELEASE loScope
    RELEASE loToSign
    RELEASE loSeed
    RELEASE loAuthorization
    RELEASE loConnection
    CANCEL
ENDIF

loResponse = CreateObject('Chilkat.JsonObject')
lnSuccess = loResponse.Load(lcResponseText)
IF (lnSuccess <> 1) THEN
    ? loResponse.LastErrorText
    RELEASE loHost
    RELEASE loMailbox
    RELEASE loPath
    RELEASE loCanonicalPath
    RELEASE loClock
    RELEASE loStamp
    RELEASE loRest
    RELEASE loHeaders
    RELEASE loCrypto
    RELEASE loCanonical
    RELEASE loScope
    RELEASE loToSign
    RELEASE loSeed
    RELEASE loAuthorization
    RELEASE loConnection
    RELEASE loResponse
    CANCEL
ENDIF

? loResponse.StringOf("SuppressedDestination.EmailAddress")
? "Reason: " + loResponse.StringOf("SuppressedDestination.Reason")
? "Last updated (Unix seconds): " + loResponse.StringOf("SuppressedDestination.LastUpdateTime")

RELEASE loHost
RELEASE loMailbox
RELEASE loPath
RELEASE loCanonicalPath
RELEASE loClock
RELEASE loStamp
RELEASE loRest
RELEASE loHeaders
RELEASE loCrypto
RELEASE loCanonical
RELEASE loScope
RELEASE loToSign
RELEASE loSeed
RELEASE loAuthorization
RELEASE loConnection
RELEASE loResponse