Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
long li_Success
long li_ConfigureDestination
string ls_SetName
string ls_Region
oleobject loo_Host
oleobject loo_Signer
oleobject loo_Connection
oleobject loo_Rest
string ls_SessionToken
oleobject loo_Path
oleobject loo_Destination
string ls_SetupResponse
oleobject loo_Request
string ls_ResponseText
oleobject loo_Response

li_Success = 0

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

li_ConfigureDestination = 1
ls_SetName = "application-mail"
//  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
ls_Region = "us-west-2"
loo_Host = create oleobject
li_rc = loo_Host.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_Host
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Host.Append("email.")
loo_Host.Append(ls_Region)
loo_Host.Append(".amazonaws.com")

loo_Signer = create oleobject
li_rc = loo_Signer.ConnectToNewObject("Chilkat.AuthAws")

//  Supply the credentials from your application's credential provider.
loo_Signer.AccessKey = "AWS_ACCESS_KEY_ID"
loo_Signer.SecretKey = "AWS_SECRET_ACCESS_KEY"
loo_Signer.Region = ls_Region
loo_Signer.ServiceName = "ses"

loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
li_Success = loo_Connection.Connect(loo_Host.GetAsString(),443,1,10000)
if li_Success <> 1 then
    Write-Debug loo_Connection.LastErrorText
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    return
end if

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

li_Success = loo_Rest.SetAuthAws(loo_Signer)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

//  For temporary AWS credentials, supply the session token from the same provider.
ls_SessionToken = ""
if ls_SessionToken <> "" then
    loo_Rest.AddQueryParam("X-Amz-Security-Token",ls_SessionToken)
end if

if li_ConfigureDestination = 1 then
    loo_Path = create oleobject
    li_rc = loo_Path.ConnectToNewObject("Chilkat.StringBuilder")

    loo_Path.Append("/v2/email/configuration-sets/")
    loo_Path.Append(ls_SetName)
    loo_Path.Append("/event-destinations")
    loo_Destination = create oleobject
    li_rc = loo_Destination.ConnectToNewObject("Chilkat.JsonObject")

    loo_Destination.UpdateString("EventDestinationName","delivery-to-sns")
    loo_Destination.UpdateString("EventDestination.MatchingEventTypes[0]","DELIVERY")
    loo_Destination.UpdateString("EventDestination.MatchingEventTypes[1]","BOUNCE")
    loo_Destination.UpdateString("EventDestination.MatchingEventTypes[2]","COMPLAINT")
    loo_Destination.UpdateString("EventDestination.SnsDestination.TopicArn","arn:aws:sns:us-west-2:123456789012:ses-delivery")
    loo_Destination.UpdateBool("EventDestination.Enabled",1)

    loo_Rest.AddHeader("Content-Type","application/json")
    ls_SetupResponse = loo_Rest.FullRequestString("POST",loo_Path.GetAsString(),loo_Destination.Emit())
    if loo_Rest.LastMethodSuccess <> 1 then
        Write-Debug loo_Rest.LastErrorText
        destroy loo_Host
        destroy loo_Signer
        destroy loo_Connection
        destroy loo_Rest
        destroy loo_Path
        destroy loo_Destination
        return
    end if

    if loo_Rest.ResponseStatusCode <> 200 then
        Write-Debug "SES returned HTTP " + string(loo_Rest.ResponseStatusCode) + " " + ls_SetupResponse
        destroy loo_Host
        destroy loo_Signer
        destroy loo_Connection
        destroy loo_Rest
        destroy loo_Path
        destroy loo_Destination
        return
    end if

    Write-Debug "Destination created. Use the send branch to test it."
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Path
    destroy loo_Destination
    return
end if

loo_Request = create oleobject
li_rc = loo_Request.ConnectToNewObject("Chilkat.JsonObject")

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

loo_Rest.AddHeader("Content-Type","application/json")
ls_ResponseText = loo_Rest.FullRequestString("POST","/v2/email/outbound-emails",loo_Request.Emit())
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Path
    destroy loo_Destination
    destroy loo_Request
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "SES returned HTTP " + string(loo_Rest.ResponseStatusCode) + " " + ls_ResponseText
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Path
    destroy loo_Destination
    destroy loo_Request
    return
end if

loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Response.Load(ls_ResponseText)
if li_Success <> 1 then
    Write-Debug loo_Response.LastErrorText
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Path
    destroy loo_Destination
    destroy loo_Request
    destroy loo_Response
    return
end if

Write-Debug "SES accepted the message: " + loo_Response.StringOf("MessageId")


destroy loo_Host
destroy loo_Signer
destroy loo_Connection
destroy loo_Rest
destroy loo_Path
destroy loo_Destination
destroy loo_Request
destroy loo_Response