Sample code for 30+ languages & platforms
Swift

Amazon SES v2: Send private appointment reminders and inspect each result

See more Amazon SES Examples

Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.

SendBulkEmail · Per-entry outcomes

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false

    let request = CkoJsonObject()!
    request.updateString(jsonPath: "FromEmailAddress", value: "sender@example.com")
    request.updateString(jsonPath: "DefaultContent.Template.TemplateContent.Subject", value: "Appointment reminder")
    request.updateString(jsonPath: "DefaultContent.Template.TemplateContent.Text", value: "Hello {{name}}, your appointment is {{slot}}.")
    request.updateString(jsonPath: "DefaultContent.Template.TemplateData", value: "{\"name\":\"there\",\"slot\":\"in your booking confirmation\"}")

    let firstReminder = CkoJsonObject()!
    firstReminder.updateString(jsonPath: "name", value: "Avery")
    firstReminder.updateString(jsonPath: "slot", value: "Tuesday at 9 AM Central")
    request.updateString(jsonPath: "BulkEmailEntries[0].Destination.ToAddresses[0]", value: "avery@example.com")
    request.updateString(jsonPath: "BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", value: firstReminder.emit())

    let secondReminder = CkoJsonObject()!
    secondReminder.updateString(jsonPath: "name", value: "Robin")
    secondReminder.updateString(jsonPath: "slot", value: "Wednesday at 2 PM Central")
    request.updateString(jsonPath: "BulkEmailEntries[1].Destination.ToAddresses[0]", value: "robin@example.com")
    request.updateString(jsonPath: "BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", value: secondReminder.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-bulk-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
    }

    var count: Int = response.size(ofArray: "BulkEmailEntryResults").intValue
    if count != request.size(ofArray: "BulkEmailEntries").intValue {
        print("Incomplete results. Reconcile the original batch before retrying.")
        return
    }

    var i: Int = 0
    while (i < count) {
        response.i = i
        request.i = i
        var recipient: String? = request.string(of: "BulkEmailEntries[i].Destination.ToAddresses[0]")
        var status: String? = response.string(of: "BulkEmailEntryResults[i].Status")
        if status == "SUCCESS" {
            print("\(recipient!) accepted: \(response.string(of: "BulkEmailEntryResults[i].MessageId")!)")
        }
        else {
            if (status == "ACCOUNT_THROTTLED") || (status == "TRANSIENT_FAILURE") {
                print("\(recipient!) retry later: \(status!)")
            }
            else {
                print("\(recipient!) review before retry: \(status!) \(response.string(of: "BulkEmailEntryResults[i].Error")!)")
            }

        }

        i = i + 1
    }


}