Sample code for 30+ languages & platforms
Xbase++

Amazon SES v2: Publish delivery events to SNS and correlate a tagged send

See more Amazon SES Examples

Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.

Configure the SNS destination · SES event payload

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL nConfigureDestination
LOCAL cSetName
LOCAL cRegion
LOCAL oHost
LOCAL oSigner
LOCAL oConnection
LOCAL oRest
LOCAL cSessionToken
LOCAL oPath
LOCAL oDestination
LOCAL cSetupResponse
LOCAL oRequest
LOCAL cResponseText
LOCAL oResponse

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

nConfigureDestination := 1
cSetName := "application-mail"
//  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
cRegion := "us-west-2"
oHost := CreateObject("Chilkat.StringBuilder")
oHost:Append("email.")
oHost:Append(cRegion)
oHost:Append(".amazonaws.com")

oSigner := CreateObject("Chilkat.AuthAws")
//  Supply the credentials from your application's credential provider.
oSigner:AccessKey := "AWS_ACCESS_KEY_ID"
oSigner:SecretKey := "AWS_SECRET_ACCESS_KEY"
oSigner:Region := cRegion
oSigner:ServiceName := "ses"

oConnection := CreateObject("Chilkat.Socket")
oConnection:RequireSslCertVerify := 1
nSuccess := oConnection:Connect(oHost:GetAsString(), 443, 1, 10000)
IF (nSuccess != 1)
    ? oConnection:LastErrorText
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    RETURN
ENDIF

oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:UseConnection(oConnection, 0)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    RETURN
ENDIF

nSuccess := oRest:SetAuthAws(oSigner)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    RETURN
ENDIF

//  For temporary AWS credentials, supply the session token from the same provider.
cSessionToken := ""
IF (cSessionToken != "")
    oRest:AddQueryParam("X-Amz-Security-Token", cSessionToken)
ENDIF

IF (nConfigureDestination == 1)
    oPath := CreateObject("Chilkat.StringBuilder")
    oPath:Append("/v2/email/configuration-sets/")
    oPath:Append(cSetName)
    oPath:Append("/event-destinations")
    oDestination := CreateObject("Chilkat.JsonObject")
    oDestination:UpdateString("EventDestinationName", "delivery-to-sns")
    oDestination:UpdateString("EventDestination.MatchingEventTypes[0]", "DELIVERY")
    oDestination:UpdateString("EventDestination.MatchingEventTypes[1]", "BOUNCE")
    oDestination:UpdateString("EventDestination.MatchingEventTypes[2]", "COMPLAINT")
    oDestination:UpdateString("EventDestination.SnsDestination.TopicArn", "arn:aws:sns:us-west-2:123456789012:ses-delivery")
    oDestination:UpdateBool("EventDestination.Enabled", 1)

    oRest:AddHeader("Content-Type", "application/json")
    cSetupResponse := oRest:FullRequestString("POST", oPath:GetAsString(), oDestination:Emit())
    IF (oRest:LastMethodSuccess != 1)
        ? oRest:LastErrorText
        oHost:destroy()
        oSigner:destroy()
        oConnection:destroy()
        oRest:destroy()
        oPath:destroy()
        oDestination:destroy()
        RETURN
    ENDIF

    IF (oRest:ResponseStatusCode != 200)
        ? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cSetupResponse
        oHost:destroy()
        oSigner:destroy()
        oConnection:destroy()
        oRest:destroy()
        oPath:destroy()
        oDestination:destroy()
        RETURN
    ENDIF

    ? "Destination created. Use the send branch to test it."
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    oPath:destroy()
    oDestination:destroy()
    RETURN
ENDIF

oRequest := CreateObject("Chilkat.JsonObject")
oRequest:UpdateString("FromEmailAddress", "sender@example.com")
oRequest:UpdateString("Destination.ToAddresses[0]", "success@simulator.amazonses.com")
oRequest:UpdateString("Content.Simple.Subject.Data", "Delivery event test")
oRequest:UpdateString("Content.Simple.Body.Text.Data", "Exercise the application-mail configuration set.")
oRequest:UpdateString("EmailTags[0].Name", "notification_ref")
oRequest:UpdateString("EmailTags[0].Value", "notify-1042")
oRequest:UpdateString("ConfigurationSetName", cSetName)

oRest:AddHeader("Content-Type", "application/json")
cResponseText := oRest:FullRequestString("POST", "/v2/email/outbound-emails", oRequest:Emit())
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    oPath:destroy()
    oDestination:destroy()
    oRequest:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode != 200)
    ? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cResponseText
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    oPath:destroy()
    oDestination:destroy()
    oRequest:destroy()
    RETURN
ENDIF

oResponse := CreateObject("Chilkat.JsonObject")
nSuccess := oResponse:Load(cResponseText)
IF (nSuccess != 1)
    ? oResponse:LastErrorText
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    oPath:destroy()
    oDestination:destroy()
    oRequest:destroy()
    oResponse:destroy()
    RETURN
ENDIF

? "SES accepted the message: " + oResponse:StringOf("MessageId")

oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oPath:destroy()
oDestination:destroy()
oRequest:destroy()
oResponse:destroy()