Lua
Lua
Amazon SES v2: Check an address on the account suppression list
See more Amazon SES Examples
Look up one address in the sending Region with ses:GetSuppressedDestination. The record distinguishes a bounce from a complaint. An absent record tells you only that this account suppression lookup found no entry. The example signs the exact encoded mailbox path and also works with temporary credentials.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
-- Look up the address exactly as stored, including any plus tag.
local address = "avery+orders@example.com"
local region = "us-west-2"
local accessKey = "AWS_ACCESS_KEY_ID"
local secretKey = "AWS_SECRET_ACCESS_KEY"
local sessionToken = ""
local host = chilkat.StringBuilder.new()
host:append("email.")
host:append(region)
host:append(".amazonaws.com")
local mailbox = chilkat.StringBuilder.new()
mailbox:append(address)
local path = chilkat.StringBuilder.new()
path:append("/v2/email/suppression/addresses/")
path:append(mailbox:get_encoded("url_rfc3986", "utf-8"))
-- SigV4 for SES encodes the wire path a second time in the canonical request.
-- Only the canonical copy escapes the percent signs. Send the original path over HTTPS.
local canonicalPath = chilkat.StringBuilder.new()
canonicalPath:append(path:get_as_string())
local replaced = canonicalPath:replace("%", "%25")
local clock = chilkat.DateTime.new()
clock:set_from_current_system_time()
local stamp = chilkat.StringBuilder.new()
stamp:append(clock:get_as_timestamp(false))
replaced = stamp:replace("-", "")
replaced = stamp:replace(":", "")
local date = stamp:get_range(0, 8, false)
local rest = chilkat.Rest.new()
rest:add_header("Host", host:get_as_string())
rest:add_header("x-amz-date", stamp:get_as_string())
local headers = chilkat.StringBuilder.new()
headers:append("host:")
headers:append(host:get_as_string())
headers:append("\nx-amz-date:")
headers:append(stamp:get_as_string())
headers:append("\n")
local signedHeaders = "host;x-amz-date"
if sessionToken ~= "" then
rest:add_header("x-amz-security-token", sessionToken)
headers:append("x-amz-security-token:")
headers:append(sessionToken)
headers:append("\n")
signedHeaders = "host;x-amz-date;x-amz-security-token"
end
local crypto = chilkat.Crypt2.new()
crypto.hash_algorithm = "sha256"
crypto.mac_algorithm = "hmac"
crypto.encoding_mode = "hexlower"
crypto.charset = "utf-8"
local canonical = chilkat.StringBuilder.new()
canonical:append("GET\n")
canonical:append(canonicalPath:get_as_string())
canonical:append("\n\n")
canonical:append(headers:get_as_string())
canonical:append("\n")
canonical:append(signedHeaders)
canonical:append("\n")
canonical:append(crypto:hash_string_enc(""))
local scope = chilkat.StringBuilder.new()
scope:append(date)
scope:append("/")
scope:append(region)
scope:append("/ses/aws4_request")
local toSign = chilkat.StringBuilder.new()
toSign:append("AWS4-HMAC-SHA256\n")
toSign:append(stamp:get_as_string())
toSign:append("\n")
toSign:append(scope:get_as_string())
toSign:append("\n")
toSign:append(crypto:hash_string_enc(canonical:get_as_string()))
-- Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
local seed = chilkat.StringBuilder.new()
seed:append("AWS4")
seed:append(secretKey)
crypto:set_mac_key_string(seed:get_as_string())
local key = crypto:mac_string_enc(date)
crypto:set_mac_key_encoded(key, "hex")
key = crypto:mac_string_enc(region)
crypto:set_mac_key_encoded(key, "hex")
key = crypto:mac_string_enc("ses")
crypto:set_mac_key_encoded(key, "hex")
key = crypto:mac_string_enc("aws4_request")
crypto:set_mac_key_encoded(key, "hex")
local signature = crypto:mac_string_enc(toSign:get_as_string())
local authorization = chilkat.StringBuilder.new()
authorization:append("AWS4-HMAC-SHA256 Credential=")
authorization:append(accessKey)
authorization:append("/")
authorization:append(scope:get_as_string())
authorization:append(", SignedHeaders=")
authorization:append(signedHeaders)
authorization:append(", Signature=")
authorization:append(signature)
rest:add_header("Authorization", authorization:get_as_string())
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
success = rest:use_connection(connection, false)
if not success then
print(rest.last_error_text)
return
end
local responseText = rest:full_request_no_body("GET", path:get_as_string())
if not rest.last_method_success then
print(rest.last_error_text)
return
end
if rest.response_status_code == 404 then
print("No account suppression entry for this address in this Region.")
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(response:string_of("SuppressedDestination.EmailAddress"))
print("Reason: " .. response:string_of("SuppressedDestination.Reason"))
print("Last updated (Unix seconds): " .. response:string_of("SuppressedDestination.LastUpdateTime"))