Sample code for 30+ languages & platforms
Swift

Amazon SES v2: Verify a sending domain with Easy DKIM

See more Amazon SES Examples

Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.

CreateEmailIdentity · GetEmailIdentity · Easy DKIM records

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    var domainName: String? = "example.com"
    var checkExisting: Bool = false
    //  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? = ""
    if checkExisting == true {
        let path = CkoStringBuilder()!
        path.append(value: "/v2/email/identities/")
        path.append(value: domainName)
        responseText = rest.fullRequestNoBody(httpVerb: "GET", uriPath: path.getAsString())
    }
    else {
        let identity = CkoJsonObject()!
        identity.updateString(jsonPath: "EmailIdentity", value: domainName)
        rest.addHeader(name: "Content-Type", value: "application/json")
        responseText = rest.fullRequestString(httpVerb: "POST", uriPath: "/v2/email/identities", bodyText: identity.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("DKIM status: \(response.string(of: "DkimAttributes.Status")!)")
    print("Verified for sending: \(response.bool(of: "VerifiedForSendingStatus"))")
    var count: Int = response.size(ofArray: "DkimAttributes.Tokens").intValue
    if count < 1 {
        print("No Easy DKIM tokens were returned. Check this identity's DKIM configuration.")
        return
    }

    if response.hasMember(jsonPath: "DkimAttributes.SigningHostedZone") != true {
        print("Find the CNAME targets for this identity in the SES console.")
        return
    }

    var zone: String? = response.string(of: "DkimAttributes.SigningHostedZone")
    var i: Int = 0
    while (i < count) {
        response.i = i
        var token: String? = response.string(of: "DkimAttributes.Tokens[i]")
        print("\(token!)._domainkey.\(domainName!) CNAME \(token!).\(zone!)")
        i = i + 1
    }


}