Sample code for 30+ languages & platforms
Lua

Amazon SES v2: Send a raw MIME email with a PDF attachment

See more Amazon SES Examples

Place invoice.pdf in the working directory. Chilkat builds the MIME message and gives the attachment the name invoice-1042.pdf. The v2 SendEmail request carries those bytes in Content.Raw.Data. For a blind copy, supply the address in Destination.BccAddresses and keep it out of the MIME headers.

Use a verified sender and grant ses:SendEmail for this v2 operation. The v2 message size limit is 40 MB including attachments after MIME encoding. For a straightforward attachment that needs no custom MIME layout, v2 also supports Content.Simple.Attachments.

Sending raw email · SES attachments · Sending quotas

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local success = false

-- Read the PDF before making a network request.
local pdf = chilkat.BinData.new()
success = pdf:load_file("invoice.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

if pdf.num_bytes == 0 then
    print("invoice.pdf is empty.")
    return
end

local sender = "sender+reports@example.com"
local recipient = "success@simulator.amazonses.com"
local email = chilkat.Email.new()
email.from = sender
email.subject = "Invoice 1042"
email.charset = "utf-8"
success = email:add_to("", recipient)
if not success then
    print(email.last_error_text)
    return
end

email:add_plain_text_alternative_body("Invoice 1042 is attached.")
email:add_html_alternative_body("<p>Invoice <strong>1042</strong> is attached.</p>")
success = email:add_attachment_bd("invoice-1042.pdf", pdf, "application/pdf")
if not success then
    print(email.last_error_text)
    return
end

-- Get the exact MIME bytes, including the attachment's transfer encoding.
local mime = chilkat.BinData.new()
success = email:get_mime_bd(mime)
if not success then
    print(email.last_error_text)
    return
end

-- Base64 here transports the entire MIME message as a blob in the JSON request.
local request = chilkat.JsonObject.new()
request:update_string("FromEmailAddress", sender)
request:update_string("Destination.ToAddresses[0]", recipient)
request:update_string("Content.Raw.Data", mime:get_encoded("base64"))

-- 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-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 result = chilkat.JsonObject.new()
success = result:load(responseText)
if not success then
    print(result.last_error_text)
    return
end

if not result:has_member("MessageId") then
    print("SES returned no message ID.")
    return
end

print("SES accepted the message. Message ID: " .. result:string_of("MessageId"))