Lua
Lua
Amazon SES v2: Send private appointment reminders and inspect each result
See more Amazon SES Examples
Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.
SendBulkEmail · Per-entry outcomes
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local request = chilkat.JsonObject.new()
request:update_string("FromEmailAddress", "sender@example.com")
request:update_string("DefaultContent.Template.TemplateContent.Subject", "Appointment reminder")
request:update_string("DefaultContent.Template.TemplateContent.Text", "Hello {{name}}, your appointment is {{slot}}.")
request:update_string("DefaultContent.Template.TemplateData", "{\"name\":\"there\",\"slot\":\"in your booking confirmation\"}")
local firstReminder = chilkat.JsonObject.new()
firstReminder:update_string("name", "Avery")
firstReminder:update_string("slot", "Tuesday at 9 AM Central")
request:update_string("BulkEmailEntries[0].Destination.ToAddresses[0]", "avery@example.com")
request:update_string("BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", firstReminder:emit())
local secondReminder = chilkat.JsonObject.new()
secondReminder:update_string("name", "Robin")
secondReminder:update_string("slot", "Wednesday at 2 PM Central")
request:update_string("BulkEmailEntries[1].Destination.ToAddresses[0]", "robin@example.com")
request:update_string("BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", secondReminder:emit())
-- 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
rest:add_header("Content-Type", "application/json")
local responseText = rest:full_request_string("POST", "/v2/email/outbound-bulk-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
local count = response:size_of_array("BulkEmailEntryResults")
if count ~= request:size_of_array("BulkEmailEntries") then
print("Incomplete results. Reconcile the original batch before retrying.")
return
end
local i = 0
while i < count do
response.i = i
request.i = i
local recipient = request:string_of("BulkEmailEntries[i].Destination.ToAddresses[0]")
local status = response:string_of("BulkEmailEntryResults[i].Status")
if status == "SUCCESS" then
print(recipient .. " accepted: " .. response:string_of("BulkEmailEntryResults[i].MessageId"))
elseif (status == "ACCOUNT_THROTTLED") or (status == "TRANSIENT_FAILURE") then
print(recipient .. " retry later: " .. status)
else
print(recipient .. " review before retry: " .. status .. " " .. response:string_of("BulkEmailEntryResults[i].Error"))
end
i = i + 1
end