Swift
Swift
Amazon SES v2: Look up delivery history for a message ID
See more Amazon SES Examples
Enable Virtual Deliverability Manager for the sending account. Use the SES MessageId from a recent eligible send to a real test mailbox in the same Region. Mailbox simulator sends are excluded from VDM. Events are grouped by destination. Timestamps in the REST response are Unix epoch seconds.
GetMessageInsights · VDM dashboard
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
var messageId: String? = "YOUR_SES_MESSAGE_ID"
let path = CkoStringBuilder()!
path.append(value: "/v2/email/insights/")
path.append(value: messageId)
path.append(value: "/")
// 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)
}
var responseText: String? = rest.fullRequestNoBody(httpVerb: "GET", uriPath: path.getAsString())
if rest.lastMethodSuccess != true {
print("\(rest.lastErrorText!)")
return
}
if rest.responseStatusCode.intValue == 404 {
print("No insights found. Check the Region, message ID, and VDM coverage.")
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
}
var i: Int = 0
var recipients: Int = response.size(ofArray: "Insights").intValue
if recipients < 1 {
print("No recipient history is available yet.")
}
while (i < recipients) {
response.i = i
print("\(response.string(of: "Insights[i].Destination")!)")
var j: Int = 0
var count: Int = response.size(ofArray: "Insights[i].Events").intValue
while (j < count) {
response.j = j
print("\(response.string(of: "Insights[i].Events[j].Timestamp")!) \(response.string(of: "Insights[i].Events[j].Type")!)")
if response.hasMember(jsonPath: "Insights[i].Events[j].Details.Bounce.DiagnosticCode") == true {
print("\(response.string(of: "Insights[i].Events[j].Details.Bounce.DiagnosticCode")!)")
}
if response.hasMember(jsonPath: "Insights[i].Events[j].Details.Complaint.ComplaintFeedbackType") == true {
print("\(response.string(of: "Insights[i].Events[j].Details.Complaint.ComplaintFeedbackType")!)")
}
j = j + 1
}
i = i + 1
}
}