Swift
Swift
Amazon SES v2: Check an address on the account suppression list
See more Amazon SES Examples
Look up one address in the sending Region with ses:GetSuppressedDestination. The record distinguishes a bounce from a complaint. An absent record tells you only that this account suppression lookup found no entry. The example signs the exact encoded mailbox path and also works with temporary credentials.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
// Look up the address exactly as stored, including any plus tag.
var address: String? = "avery+orders@example.com"
var region: String? = "us-west-2"
var accessKey: String? = "AWS_ACCESS_KEY_ID"
var secretKey: String? = "AWS_SECRET_ACCESS_KEY"
var sessionToken: String? = ""
let host = CkoStringBuilder()!
host.append(value: "email.")
host.append(value: region)
host.append(value: ".amazonaws.com")
let mailbox = CkoStringBuilder()!
mailbox.append(value: address)
let path = CkoStringBuilder()!
path.append(value: "/v2/email/suppression/addresses/")
path.append(value: mailbox.getEncoded(encoding: "url_rfc3986", charset: "utf-8"))
// SigV4 for SES encodes the wire path a second time in the canonical request.
// Only the canonical copy escapes the percent signs. Send the original path over HTTPS.
let canonicalPath = CkoStringBuilder()!
canonicalPath.append(value: path.getAsString())
var replaced: Int = canonicalPath.replace(value: "%", replacement: "%25").intValue
let clock = CkoDateTime()!
clock.setFromCurrentSystemTime()
let stamp = CkoStringBuilder()!
stamp.append(value: clock.get(asTimestamp: false))
replaced = stamp.replace(value: "-", replacement: "").intValue
replaced = stamp.replace(value: ":", replacement: "").intValue
var date: String? = stamp.getRange(startIndex: 0, numChars: 8, removeFlag: false)
let rest = CkoRest()!
rest.addHeader(name: "Host", value: host.getAsString())
rest.addHeader(name: "x-amz-date", value: stamp.getAsString())
let headers = CkoStringBuilder()!
headers.append(value: "host:")
headers.append(value: host.getAsString())
headers.append(value: "\nx-amz-date:")
headers.append(value: stamp.getAsString())
headers.append(value: "\n")
var signedHeaders: String? = "host;x-amz-date"
if sessionToken != "" {
rest.addHeader(name: "x-amz-security-token", value: sessionToken)
headers.append(value: "x-amz-security-token:")
headers.append(value: sessionToken)
headers.append(value: "\n")
signedHeaders = "host;x-amz-date;x-amz-security-token"
}
let crypto = CkoCrypt2()!
crypto.hashAlgorithm = "sha256"
crypto.macAlgorithm = "hmac"
crypto.encodingMode = "hexlower"
crypto.charset = "utf-8"
let canonical = CkoStringBuilder()!
canonical.append(value: "GET\n")
canonical.append(value: canonicalPath.getAsString())
canonical.append(value: "\n\n")
canonical.append(value: headers.getAsString())
canonical.append(value: "\n")
canonical.append(value: signedHeaders)
canonical.append(value: "\n")
canonical.append(value: crypto.hashStringENC(str: ""))
let scope = CkoStringBuilder()!
scope.append(value: date)
scope.append(value: "/")
scope.append(value: region)
scope.append(value: "/ses/aws4_request")
let toSign = CkoStringBuilder()!
toSign.append(value: "AWS4-HMAC-SHA256\n")
toSign.append(value: stamp.getAsString())
toSign.append(value: "\n")
toSign.append(value: scope.getAsString())
toSign.append(value: "\n")
toSign.append(value: crypto.hashStringENC(str: canonical.getAsString()))
// Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
let seed = CkoStringBuilder()!
seed.append(value: "AWS4")
seed.append(value: secretKey)
crypto.setMacKeyString(key: seed.getAsString())
var key: String? = crypto.macStringENC(inText: date)
crypto.setMacKeyEncoded(key: key, encoding: "hex")
key = crypto.macStringENC(inText: region)
crypto.setMacKeyEncoded(key: key, encoding: "hex")
key = crypto.macStringENC(inText: "ses")
crypto.setMacKeyEncoded(key: key, encoding: "hex")
key = crypto.macStringENC(inText: "aws4_request")
crypto.setMacKeyEncoded(key: key, encoding: "hex")
var signature: String? = crypto.macStringENC(inText: toSign.getAsString())
let authorization = CkoStringBuilder()!
authorization.append(value: "AWS4-HMAC-SHA256 Credential=")
authorization.append(value: accessKey)
authorization.append(value: "/")
authorization.append(value: scope.getAsString())
authorization.append(value: ", SignedHeaders=")
authorization.append(value: signedHeaders)
authorization.append(value: ", Signature=")
authorization.append(value: signature)
rest.addHeader(name: "Authorization", value: authorization.getAsString())
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
}
success = rest.useConnection(connection: connection, autoReconnect: false)
if success != true {
print("\(rest.lastErrorText!)")
return
}
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 account suppression entry for this address in this Region.")
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("\(response.string(of: "SuppressedDestination.EmailAddress")!)")
print("Reason: \(response.string(of: "SuppressedDestination.Reason")!)")
print("Last updated (Unix seconds): \(response.string(of: "SuppressedDestination.LastUpdateTime")!)")
}