Sample code for 30+ languages & platforms
Lua

Amazon SES v2: Verify a sending domain with Easy DKIM

See more Amazon SES Examples

Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.

CreateEmailIdentity · GetEmailIdentity · Easy DKIM records

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local domainName = "example.com"
local checkExisting = false
-- 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

local responseText = ""
if checkExisting then
    local path = chilkat.StringBuilder.new()
    path:append("/v2/email/identities/")
    path:append(domainName)
    responseText = rest:full_request_no_body("GET", path:get_as_string())
else
    local identity = chilkat.JsonObject.new()
    identity:update_string("EmailIdentity", domainName)
    rest:add_header("Content-Type", "application/json")
    responseText = rest:full_request_string("POST", "/v2/email/identities", identity:emit())
end

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("DKIM status: " .. response:string_of("DkimAttributes.Status"))
print("Verified for sending: " .. tostring(response:bool_of("VerifiedForSendingStatus")))
local count = response:size_of_array("DkimAttributes.Tokens")
if count < 1 then
    print("No Easy DKIM tokens were returned. Check this identity's DKIM configuration.")
    return
end

if not response:has_member("DkimAttributes.SigningHostedZone") then
    print("Find the CNAME targets for this identity in the SES console.")
    return
end

local zone = response:string_of("DkimAttributes.SigningHostedZone")
local i = 0
while i < count do
    response.i = i
    local token = response:string_of("DkimAttributes.Tokens[i]")
    print(token .. "._domainkey." .. domainName .. " CNAME " .. token .. "." .. zone)
    i = i + 1
end