Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0

set configureDestination 1
set setName "application-mail"
#  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
set region "us-west-2"
set host [new_CkStringBuilder]

CkStringBuilder_Append $host "email."
CkStringBuilder_Append $host $region
CkStringBuilder_Append $host ".amazonaws.com"

set signer [new_CkAuthAws]

#  Supply the credentials from your application's credential provider.
CkAuthAws_put_AccessKey $signer "AWS_ACCESS_KEY_ID"
CkAuthAws_put_SecretKey $signer "AWS_SECRET_ACCESS_KEY"
CkAuthAws_put_Region $signer $region
CkAuthAws_put_ServiceName $signer "ses"

set connection [new_CkSocket]

CkSocket_put_RequireSslCertVerify $connection 1
set success [CkSocket_Connect $connection [CkStringBuilder_getAsString $host] 443 1 10000]
if {$success != 1} then {
    puts [CkSocket_lastErrorText $connection]
    delete_CkStringBuilder $host
    delete_CkAuthAws $signer
    delete_CkSocket $connection
    exit
}

set rest [new_CkRest]

set success [CkRest_UseConnection $rest $connection 0]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkStringBuilder $host
    delete_CkAuthAws $signer
    delete_CkSocket $connection
    delete_CkRest $rest
    exit
}

set success [CkRest_SetAuthAws $rest $signer]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkStringBuilder $host
    delete_CkAuthAws $signer
    delete_CkSocket $connection
    delete_CkRest $rest
    exit
}

#  For temporary AWS credentials, supply the session token from the same provider.
set sessionToken ""
if {$sessionToken != ""} then {
    CkRest_AddQueryParam $rest "X-Amz-Security-Token" $sessionToken
}

if {$configureDestination == 1} then {
    set path [new_CkStringBuilder]

    CkStringBuilder_Append $path "/v2/email/configuration-sets/"
    CkStringBuilder_Append $path $setName
    CkStringBuilder_Append $path "/event-destinations"
    set destination [new_CkJsonObject]

    CkJsonObject_UpdateString $destination "EventDestinationName" "delivery-to-sns"
    CkJsonObject_UpdateString $destination "EventDestination.MatchingEventTypes[0]" "DELIVERY"
    CkJsonObject_UpdateString $destination "EventDestination.MatchingEventTypes[1]" "BOUNCE"
    CkJsonObject_UpdateString $destination "EventDestination.MatchingEventTypes[2]" "COMPLAINT"
    CkJsonObject_UpdateString $destination "EventDestination.SnsDestination.TopicArn" "arn:aws:sns:us-west-2:123456789012:ses-delivery"
    CkJsonObject_UpdateBool $destination "EventDestination.Enabled" 1

    CkRest_AddHeader $rest "Content-Type" "application/json"
    set setupResponse [CkRest_fullRequestString $rest "POST" [CkStringBuilder_getAsString $path] [CkJsonObject_emit $destination]]
    if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
        puts [CkRest_lastErrorText $rest]
        delete_CkStringBuilder $host
        delete_CkAuthAws $signer
        delete_CkSocket $connection
        delete_CkRest $rest
        delete_CkStringBuilder $path
        delete_CkJsonObject $destination
        exit
    }

    if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
        puts "SES returned HTTP [CkRest_get_ResponseStatusCode $rest] $setupResponse"
        delete_CkStringBuilder $host
        delete_CkAuthAws $signer
        delete_CkSocket $connection
        delete_CkRest $rest
        delete_CkStringBuilder $path
        delete_CkJsonObject $destination
        exit
    }

    puts "Destination created. Use the send branch to test it."
    delete_CkStringBuilder $host
    delete_CkAuthAws $signer
    delete_CkSocket $connection
    delete_CkRest $rest
    delete_CkStringBuilder $path
    delete_CkJsonObject $destination
    exit
}

set request [new_CkJsonObject]

CkJsonObject_UpdateString $request "FromEmailAddress" "sender@example.com"
CkJsonObject_UpdateString $request "Destination.ToAddresses[0]" "success@simulator.amazonses.com"
CkJsonObject_UpdateString $request "Content.Simple.Subject.Data" "Delivery event test"
CkJsonObject_UpdateString $request "Content.Simple.Body.Text.Data" "Exercise the application-mail configuration set."
CkJsonObject_UpdateString $request "EmailTags[0].Name" "notification_ref"
CkJsonObject_UpdateString $request "EmailTags[0].Value" "notify-1042"
CkJsonObject_UpdateString $request "ConfigurationSetName" $setName

CkRest_AddHeader $rest "Content-Type" "application/json"
set responseText [CkRest_fullRequestString $rest "POST" "/v2/email/outbound-emails" [CkJsonObject_emit $request]]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkStringBuilder $host
    delete_CkAuthAws $signer
    delete_CkSocket $connection
    delete_CkRest $rest
    delete_CkStringBuilder $path
    delete_CkJsonObject $destination
    delete_CkJsonObject $request
    exit
}

if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
    puts "SES returned HTTP [CkRest_get_ResponseStatusCode $rest] $responseText"
    delete_CkStringBuilder $host
    delete_CkAuthAws $signer
    delete_CkSocket $connection
    delete_CkRest $rest
    delete_CkStringBuilder $path
    delete_CkJsonObject $destination
    delete_CkJsonObject $request
    exit
}

set response [new_CkJsonObject]

set success [CkJsonObject_Load $response $responseText]
if {$success != 1} then {
    puts [CkJsonObject_lastErrorText $response]
    delete_CkStringBuilder $host
    delete_CkAuthAws $signer
    delete_CkSocket $connection
    delete_CkRest $rest
    delete_CkStringBuilder $path
    delete_CkJsonObject $destination
    delete_CkJsonObject $request
    delete_CkJsonObject $response
    exit
}

puts "SES accepted the message: [CkJsonObject_stringOf $response MessageId]"

delete_CkStringBuilder $host
delete_CkAuthAws $signer
delete_CkSocket $connection
delete_CkRest $rest
delete_CkStringBuilder $path
delete_CkJsonObject $destination
delete_CkJsonObject $request
delete_CkJsonObject $response