Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Boolean iConfigureDestination
    String sSetName
    String sRegion
    Handle hoHost
    Variant vSigner
    Handle hoSigner
    Variant vConnection
    Handle hoConnection
    Handle hoRest
    String sSessionToken
    Handle hoPath
    Handle hoDestination
    String sSetupResponse
    Handle hoRequest
    String sResponseText
    Handle hoResponse
    String sTemp1
    String sTemp2
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Move False To iSuccess

    Move True To iConfigureDestination
    Move "application-mail" To sSetName
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    Move "us-west-2" To sRegion
    Get Create (RefClass(cComChilkatStringBuilder)) To hoHost
    If (Not(IsComObjectCreated(hoHost))) Begin
        Send CreateComObject of hoHost
    End
    Get ComAppend Of hoHost "email." To iSuccess
    Get ComAppend Of hoHost sRegion To iSuccess
    Get ComAppend Of hoHost ".amazonaws.com" To iSuccess

    Get Create (RefClass(cComChilkatAuthAws)) To hoSigner
    If (Not(IsComObjectCreated(hoSigner))) Begin
        Send CreateComObject of hoSigner
    End
    //  Supply the credentials from your application's credential provider.
    Set ComAccessKey Of hoSigner To "AWS_ACCESS_KEY_ID"
    Set ComSecretKey Of hoSigner To "AWS_SECRET_ACCESS_KEY"
    Set ComRegion Of hoSigner To sRegion
    Set ComServiceName Of hoSigner To "ses"

    Get Create (RefClass(cComChilkatSocket)) To hoConnection
    If (Not(IsComObjectCreated(hoConnection))) Begin
        Send CreateComObject of hoConnection
    End
    Set ComRequireSslCertVerify Of hoConnection To True
    Get ComGetAsString Of hoHost To sTemp1
    Get ComConnect Of hoConnection sTemp1 443 True 10000 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoConnection To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Get pvComObject of hoConnection to vConnection
    Get ComUseConnection Of hoRest vConnection False To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoSigner to vSigner
    Get ComSetAuthAws Of hoRest vSigner To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  For temporary AWS credentials, supply the session token from the same provider.
    Move "" To sSessionToken
    If (sSessionToken <> "") Begin
        Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sSessionToken To iSuccess
    End

    If (iConfigureDestination = True) Begin
        Get Create (RefClass(cComChilkatStringBuilder)) To hoPath
        If (Not(IsComObjectCreated(hoPath))) Begin
            Send CreateComObject of hoPath
        End
        Get ComAppend Of hoPath "/v2/email/configuration-sets/" To iSuccess
        Get ComAppend Of hoPath sSetName To iSuccess
        Get ComAppend Of hoPath "/event-destinations" To iSuccess
        Get Create (RefClass(cComChilkatJsonObject)) To hoDestination
        If (Not(IsComObjectCreated(hoDestination))) Begin
            Send CreateComObject of hoDestination
        End
        Get ComUpdateString Of hoDestination "EventDestinationName" "delivery-to-sns" To iSuccess
        Get ComUpdateString Of hoDestination "EventDestination.MatchingEventTypes[0]" "DELIVERY" To iSuccess
        Get ComUpdateString Of hoDestination "EventDestination.MatchingEventTypes[1]" "BOUNCE" To iSuccess
        Get ComUpdateString Of hoDestination "EventDestination.MatchingEventTypes[2]" "COMPLAINT" To iSuccess
        Get ComUpdateString Of hoDestination "EventDestination.SnsDestination.TopicArn" "arn:aws:sns:us-west-2:123456789012:ses-delivery" To iSuccess
        Get ComUpdateBool Of hoDestination "EventDestination.Enabled" True To iSuccess

        Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess
        Get ComGetAsString Of hoPath To sTemp1
        Get ComEmit Of hoDestination To sTemp2
        Get ComFullRequestString Of hoRest "POST" sTemp1 sTemp2 To sSetupResponse
        Get ComLastMethodSuccess Of hoRest To bTemp1
        If (bTemp1 <> True) Begin
            Get ComLastErrorText Of hoRest To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComResponseStatusCode Of hoRest To iTemp1
        If (iTemp1 <> 200) Begin
            Get ComResponseStatusCode Of hoRest To iTemp1
            Showln "SES returned HTTP " iTemp1 " " sSetupResponse
            Procedure_Return
        End

        Showln "Destination created. Use the send branch to test it."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoRequest
    If (Not(IsComObjectCreated(hoRequest))) Begin
        Send CreateComObject of hoRequest
    End
    Get ComUpdateString Of hoRequest "FromEmailAddress" "sender@example.com" To iSuccess
    Get ComUpdateString Of hoRequest "Destination.ToAddresses[0]" "success@simulator.amazonses.com" To iSuccess
    Get ComUpdateString Of hoRequest "Content.Simple.Subject.Data" "Delivery event test" To iSuccess
    Get ComUpdateString Of hoRequest "Content.Simple.Body.Text.Data" "Exercise the application-mail configuration set." To iSuccess
    Get ComUpdateString Of hoRequest "EmailTags[0].Name" "notification_ref" To iSuccess
    Get ComUpdateString Of hoRequest "EmailTags[0].Value" "notify-1042" To iSuccess
    Get ComUpdateString Of hoRequest "ConfigurationSetName" sSetName To iSuccess

    Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess
    Get ComEmit Of hoRequest To sTemp1
    Get ComFullRequestString Of hoRest "POST" "/v2/email/outbound-emails" sTemp1 To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "SES returned HTTP " iTemp1 " " sResponseText
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoResponse
    If (Not(IsComObjectCreated(hoResponse))) Begin
        Send CreateComObject of hoResponse
    End
    Get ComLoad Of hoResponse sResponseText To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoResponse To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoResponse "MessageId" To sTemp1
    Showln "SES accepted the message: " sTemp1


End_Procedure