Lua
Lua
Amazon SES v2: Attach a PDF to a simple email
See more Amazon SES Examples
Place invoice.pdf in the working directory and use a verified sender. RawContent contains the Base64 representation of the file bytes for JSON transport. ContentTransferEncoding tells SES how to encode the PDF when it assembles the outgoing MIME message. For custom MIME structure, see Send a raw MIME email.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
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 request = chilkat.JsonObject.new()
request:update_string("FromEmailAddress", "sender@example.com")
request:update_string("Destination.ToAddresses[0]", "success@simulator.amazonses.com")
request:update_string("Content.Simple.Subject.Data", "Invoice 1042")
request:update_string("Content.Simple.Body.Text.Data", "Your invoice is attached.")
request:update_string("Content.Simple.Attachments[0].FileName", "invoice-1042.pdf")
request:update_string("Content.Simple.Attachments[0].ContentType", "application/pdf")
request:update_string("Content.Simple.Attachments[0].ContentDisposition", "ATTACHMENT")
request:update_string("Content.Simple.Attachments[0].ContentTransferEncoding", "BASE64")
request:update_string("Content.Simple.Attachments[0].RawContent", pdf: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 response = chilkat.JsonObject.new()
success = response:load(responseText)
if not success then
print(response.last_error_text)
return
end
print("SES accepted the message: " .. response:string_of("MessageId"))