Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false

local configureDestination = true
local setName = "application-mail"
-- Use the same AWS Region for the identity, endpoint, and Signature Version 4.
local region = "us-west-2"
local host = chilkat.StringBuilder.new()
host:append("email.")
host:append(region)
host:append(".amazonaws.com")

local signer = chilkat.AuthAws.new()
-- Supply the credentials from your application's credential provider.
signer.access_key = "AWS_ACCESS_KEY_ID"
signer.secret_key = "AWS_SECRET_ACCESS_KEY"
signer.region = region
signer.service_name = "ses"

local connection = chilkat.Socket.new()
connection.require_ssl_cert_verify = true
success = connection:connect(host:get_as_string(), 443, true, 10000)
if not success then
    print(connection.last_error_text)
    return
end

local rest = chilkat.Rest.new()
success = rest:use_connection(connection, false)
if not success then
    print(rest.last_error_text)
    return
end

success = rest:set_auth_aws(signer)
if not success then
    print(rest.last_error_text)
    return
end

-- For temporary AWS credentials, supply the session token from the same provider.
local sessionToken = ""
if sessionToken ~= "" then
    rest:add_query_param("X-Amz-Security-Token", sessionToken)
end

if configureDestination then
    local path = chilkat.StringBuilder.new()
    path:append("/v2/email/configuration-sets/")
    path:append(setName)
    path:append("/event-destinations")
    local destination = chilkat.JsonObject.new()
    destination:update_string("EventDestinationName", "delivery-to-sns")
    destination:update_string("EventDestination.MatchingEventTypes[0]", "DELIVERY")
    destination:update_string("EventDestination.MatchingEventTypes[1]", "BOUNCE")
    destination:update_string("EventDestination.MatchingEventTypes[2]", "COMPLAINT")
    destination:update_string("EventDestination.SnsDestination.TopicArn", "arn:aws:sns:us-west-2:123456789012:ses-delivery")
    destination:update_bool("EventDestination.Enabled", true)

    rest:add_header("Content-Type", "application/json")
    local setupResponse = rest:full_request_string("POST", path:get_as_string(), destination:emit())
    if not rest.last_method_success then
        print(rest.last_error_text)
        return
    end

    if rest.response_status_code ~= 200 then
        print("SES returned HTTP " .. rest.response_status_code .. " " .. setupResponse)
        return
    end

    print("Destination created. Use the send branch to test it.")
    return
end

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

rest:add_header("Content-Type", "application/json")
local responseText = rest:full_request_string("POST", "/v2/email/outbound-emails", request:emit())
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

if rest.response_status_code ~= 200 then
    print("SES returned HTTP " .. rest.response_status_code .. " " .. responseText)
    return
end

local response = chilkat.JsonObject.new()
success = response:load(responseText)
if not success then
    print(response.last_error_text)
    return
end

print("SES accepted the message: " .. response:string_of("MessageId"))