Sample code for 30+ languages & platforms
Swift

Amazon SES v2: Send a newsletter with an SES-managed topic unsubscribe link

See more Amazon SES Examples

Use an existing SES contact list named Readers with a ProductUpdates topic and a test subscriber who opted in. Replace the verified sender and subscriber address. Send to one recipient per message so SES can insert its subscription headers and personalize the footer link. When that reader opts out, SES honors the preference on future sends using the same list and topic.

SES subscription management · SendEmail

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: "Destination.ToAddresses[0]", value: "subscriber@example.com")
    request.updateString(jsonPath: "ListManagementOptions.ContactListName", value: "Readers")
    request.updateString(jsonPath: "ListManagementOptions.TopicName", value: "ProductUpdates")
    request.updateString(jsonPath: "Content.Simple.Subject.Data", value: "Product updates: November edition")
    request.updateString(jsonPath: "Content.Simple.Body.Text.Data", value: "The November catalog is available.\nManage your preferences: {{amazonSESUnsubscribeUrl}}")
    request.updateString(jsonPath: "Content.Simple.Body.Html.Data", value: "<p>The November catalog is available.</p><p><a href=\"{{amazonSESUnsubscribeUrl}}\">Manage your product update preferences</a></p>")
    //  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")!)")

}