Sample code for 30+ languages & platforms
Lua

Amazon SES v2: Create and preview a receipt template

See more Amazon SES Examples

Create the regional OrderReceipt template once. Set createTemplate to false to preview it with sample order data. To exercise the missing-field case, omit orderNumber from the preview data and inspect the rendering error. A successful preview returns MIME content without sending a message.

CreateEmailTemplate · TestRenderEmailTemplate

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local createTemplate = true
-- 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 createTemplate then
    local request = chilkat.JsonObject.new()
    request:update_string("TemplateName", "OrderReceipt")
    request:update_string("TemplateContent.Subject", "Receipt for {{orderNumber}}")
    request:update_string("TemplateContent.Text", "Thank you, {{firstName}}. Order {{orderNumber}}: {{#each items}}{{quantity}} x {{name}}. {{/each}}")
    request:update_string("TemplateContent.Html", "<p>Thank you, {{firstName}}.</p><ul>{{#each items}}<li>{{quantity}} x {{name}}</li>{{/each}}</ul>")

    rest:add_header("Content-Type", "application/json")
    local setupResponse = rest:full_request_string("POST", "/v2/email/templates", 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 .. " " .. setupResponse)
        return
    end

    print("Template created. Set createTemplate to false to preview it.")
    return
end

local order = chilkat.JsonObject.new()
order:update_string("firstName", "Avery")
order:update_string("orderNumber", "1042")
order:update_string("items[0].name", "Notebook")
order:update_int("items[0].quantity", 2)
order:update_string("items[1].name", "Pen")
order:update_int("items[1].quantity", 1)
-- Remove this key to try the missing-field case.
local tryMissingField = false
if tryMissingField then
    order:delete("orderNumber")
end

local preview = chilkat.JsonObject.new()
preview:update_string("TemplateData", order:emit())

rest:add_header("Content-Type", "application/json")
local responseText = rest:full_request_string("POST", "/v2/email/templates/OrderReceipt/render", preview: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(response:string_of("RenderedTemplate"))