Swift
Swift
Amazon SES v2: Send a personalized receipt from a stored template
See more Amazon SES Examples
First create and preview the OrderReceipt template in the sending Region. Supply a verified sender. Build the order as structured data, then serialize it once into the string required by TemplateData. A message ID records acceptance. Rendering failures can be reported through SES event publishing.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
let order = CkoJsonObject()!
order.updateString(jsonPath: "firstName", value: "Avery")
order.updateString(jsonPath: "orderNumber", value: "1042")
order.updateString(jsonPath: "items[0].name", value: "Notebook")
order.updateInt(jsonPath: "items[0].quantity", value: 2)
order.updateString(jsonPath: "items[1].name", value: "Pen")
order.updateInt(jsonPath: "items[1].quantity", value: 1)
let request = CkoJsonObject()!
request.updateString(jsonPath: "FromEmailAddress", value: "sender@example.com")
request.updateString(jsonPath: "Destination.ToAddresses[0]", value: "success@simulator.amazonses.com")
request.updateString(jsonPath: "Content.Template.TemplateName", value: "OrderReceipt")
request.updateString(jsonPath: "Content.Template.TemplateData", value: order.emit())
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
var region: String? = "us-west-2"
let host = CkoStringBuilder()!
host.append(value: "email.")
host.append(value: region)
host.append(value: ".amazonaws.com")
let signer = CkoAuthAws()!
// Supply the credentials from your application's credential provider.
signer.accessKey = "AWS_ACCESS_KEY_ID"
signer.secretKey = "AWS_SECRET_ACCESS_KEY"
signer.region = region
signer.serviceName = "ses"
let connection = CkoSocket()!
connection.requireSslCertVerify = true
success = connection.connect(hostname: host.getAsString(), port: 443, ssl: true, maxWaitMs: 10000)
if success != true {
print("\(connection.lastErrorText!)")
return
}
let rest = CkoRest()!
success = rest.useConnection(connection: connection, autoReconnect: false)
if success != true {
print("\(rest.lastErrorText!)")
return
}
success = rest.setAuthAws(authProvider: signer)
if success != true {
print("\(rest.lastErrorText!)")
return
}
// For temporary AWS credentials, supply the session token from the same provider.
var sessionToken: String? = ""
if sessionToken != "" {
rest.addQueryParam(name: "X-Amz-Security-Token", value: sessionToken)
}
rest.addHeader(name: "Content-Type", value: "application/json")
var responseText: String? = rest.fullRequestString(httpVerb: "POST", uriPath: "/v2/email/outbound-emails", bodyText: request.emit())
if rest.lastMethodSuccess != true {
print("\(rest.lastErrorText!)")
return
}
if rest.responseStatusCode.intValue != 200 {
print("SES returned HTTP \(rest.responseStatusCode.intValue) \(responseText!)")
return
}
let response = CkoJsonObject()!
success = response.load(json: responseText)
if success != true {
print("\(response.lastErrorText!)")
return
}
print("SES accepted the message: \(response.string(of: "MessageId")!)")
}