Swift
Swift
Amazon SES v2: Publish delivery events to SNS and correlate a tagged send
See more Amazon SES Examples
Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.
Configure the SNS destination · SES event payload
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
var configureDestination: Bool = true
var setName: String? = "application-mail"
// 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)
}
if configureDestination == true {
let path = CkoStringBuilder()!
path.append(value: "/v2/email/configuration-sets/")
path.append(value: setName)
path.append(value: "/event-destinations")
let destination = CkoJsonObject()!
destination.updateString(jsonPath: "EventDestinationName", value: "delivery-to-sns")
destination.updateString(jsonPath: "EventDestination.MatchingEventTypes[0]", value: "DELIVERY")
destination.updateString(jsonPath: "EventDestination.MatchingEventTypes[1]", value: "BOUNCE")
destination.updateString(jsonPath: "EventDestination.MatchingEventTypes[2]", value: "COMPLAINT")
destination.updateString(jsonPath: "EventDestination.SnsDestination.TopicArn", value: "arn:aws:sns:us-west-2:123456789012:ses-delivery")
destination.updateBool(jsonPath: "EventDestination.Enabled", value: true)
rest.addHeader(name: "Content-Type", value: "application/json")
var setupResponse: String? = rest.fullRequestString(httpVerb: "POST", uriPath: path.getAsString(), bodyText: destination.emit())
if rest.lastMethodSuccess != true {
print("\(rest.lastErrorText!)")
return
}
if rest.responseStatusCode.intValue != 200 {
print("SES returned HTTP \(rest.responseStatusCode.intValue) \(setupResponse!)")
return
}
print("Destination created. Use the send branch to test it.")
return
}
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.Simple.Subject.Data", value: "Delivery event test")
request.updateString(jsonPath: "Content.Simple.Body.Text.Data", value: "Exercise the application-mail configuration set.")
request.updateString(jsonPath: "EmailTags[0].Name", value: "notification_ref")
request.updateString(jsonPath: "EmailTags[0].Value", value: "notify-1042")
request.updateString(jsonPath: "ConfigurationSetName", value: setName)
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")!)")
}