Sample code for 30+ languages & platforms
PureBasic

Amazon SES v2: Look up delivery history for a message ID

See more Amazon SES Examples

Enable Virtual Deliverability Manager for the sending account. Use the SES MessageId from a recent eligible send to a real test mailbox in the same Region. Mailbox simulator sends are excluded from VDM. Events are grouped by destination. Timestamps in the REST response are Unix epoch seconds.

GetMessageInsights · VDM dashboard

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = 0

    messageId.s = "YOUR_SES_MESSAGE_ID"
    path.i = CkStringBuilder::ckCreate()
    If path.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(path,"/v2/email/insights/")
    CkStringBuilder::ckAppend(path,messageId)
    CkStringBuilder::ckAppend(path,"/")
    ;  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    region.s = "us-west-2"
    host.i = CkStringBuilder::ckCreate()
    If host.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(host,"email.")
    CkStringBuilder::ckAppend(host,region)
    CkStringBuilder::ckAppend(host,".amazonaws.com")

    signer.i = CkAuthAws::ckCreate()
    If signer.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Supply the credentials from your application's credential provider.
    CkAuthAws::setCkAccessKey(signer, "AWS_ACCESS_KEY_ID")
    CkAuthAws::setCkSecretKey(signer, "AWS_SECRET_ACCESS_KEY")
    CkAuthAws::setCkRegion(signer, region)
    CkAuthAws::setCkServiceName(signer, "ses")

    connection.i = CkSocket::ckCreate()
    If connection.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSocket::setCkRequireSslCertVerify(connection, 1)
    success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(host),443,1,10000)
    If success <> 1
        Debug CkSocket::ckLastErrorText(connection)
        CkStringBuilder::ckDispose(path)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckUseConnection(rest,connection,0)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkStringBuilder::ckDispose(path)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    success = CkRest::ckSetAuthAws(rest,signer)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkStringBuilder::ckDispose(path)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  For temporary AWS credentials, supply the session token from the same provider.
    sessionToken.s = ""
    If sessionToken <> ""
        CkRest::ckAddQueryParam(rest,"X-Amz-Security-Token",sessionToken)
    EndIf

    responseText.s = CkRest::ckFullRequestNoBody(rest,"GET",CkStringBuilder::ckGetAsString(path))
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkStringBuilder::ckDispose(path)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) = 404
        Debug "No insights found. Check the Region, message ID, and VDM coverage."
        CkStringBuilder::ckDispose(path)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "SES returned HTTP " + Str(CkRest::ckResponseStatusCode(rest)) + " " + responseText
        CkStringBuilder::ckDispose(path)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    response.i = CkJsonObject::ckCreate()
    If response.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoad(response,responseText)
    If success <> 1
        Debug CkJsonObject::ckLastErrorText(response)
        CkStringBuilder::ckDispose(path)
        CkStringBuilder::ckDispose(host)
        CkAuthAws::ckDispose(signer)
        CkSocket::ckDispose(connection)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(response)
        ProcedureReturn
    EndIf

    i.i = 0
    recipients.i = CkJsonObject::ckSizeOfArray(response,"Insights")
    If recipients < 1
        Debug "No recipient history is available yet."
    EndIf

    While (i < recipients)
        CkJsonObject::setCkI(response, i)
        Debug CkJsonObject::ckStringOf(response,"Insights[i].Destination")
        j.i = 0
        count.i = CkJsonObject::ckSizeOfArray(response,"Insights[i].Events")
        While (j < count)
            CkJsonObject::setCkJ(response, j)
            Debug CkJsonObject::ckStringOf(response,"Insights[i].Events[j].Timestamp") + " " + CkJsonObject::ckStringOf(response,"Insights[i].Events[j].Type")
            If CkJsonObject::ckHasMember(response,"Insights[i].Events[j].Details.Bounce.DiagnosticCode") = 1
                Debug CkJsonObject::ckStringOf(response,"Insights[i].Events[j].Details.Bounce.DiagnosticCode")
            EndIf

            If CkJsonObject::ckHasMember(response,"Insights[i].Events[j].Details.Complaint.ComplaintFeedbackType") = 1
                Debug CkJsonObject::ckStringOf(response,"Insights[i].Events[j].Details.Complaint.ComplaintFeedbackType")
            EndIf

            j = j + 1
        Wend
        i = i + 1
    Wend


    CkStringBuilder::ckDispose(path)
    CkStringBuilder::ckDispose(host)
    CkAuthAws::ckDispose(signer)
    CkSocket::ckDispose(connection)
    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(response)


    ProcedureReturn
EndProcedure