Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Look up the address exactly as stored, including any plus tag.
final address = 'avery+orders@example.com';
final region = 'us-west-2';
final accessKey = 'AWS_ACCESS_KEY_ID';
final secretKey = 'AWS_SECRET_ACCESS_KEY';
final sessionToken = '';
final host = CkStringBuilder();
host.append('email.');
host.append(region);
host.append('.amazonaws.com');
final mailbox = CkStringBuilder();
mailbox.append(address);
final path = CkStringBuilder();
path.append('/v2/email/suppression/addresses/');
path.append(mailbox.getEncoded('url_rfc3986', '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.
final canonicalPath = CkStringBuilder();
canonicalPath.append(path.getAsString());
var replaced = canonicalPath.replace('%', '%25');
final clock = CkDateTime();
clock.setFromCurrentSystemTime();
final stamp = CkStringBuilder();
stamp.append(clock.getAsTimestamp(false));
replaced = stamp.replace('-', '');
replaced = stamp.replace(':', '');
final date = stamp.getRange(0, 8, false);
final rest = CkRest();
rest.addHeader('Host', host.getAsString());
rest.addHeader('x-amz-date', stamp.getAsString());
final headers = CkStringBuilder();
headers.append('host:');
headers.append(host.getAsString());
headers.append('\nx-amz-date:');
headers.append(stamp.getAsString());
headers.append('\n');
var signedHeaders = 'host;x-amz-date';
if (sessionToken != '') {
rest.addHeader('x-amz-security-token', sessionToken);
headers.append('x-amz-security-token:');
headers.append(sessionToken);
headers.append('\n');
signedHeaders = 'host;x-amz-date;x-amz-security-token';
}
final crypto = CkCrypt2();
crypto.hashAlgorithm = 'sha256';
crypto.macAlgorithm = 'hmac';
crypto.encodingMode = 'hexlower';
crypto.charset = 'utf-8';
final canonical = CkStringBuilder();
canonical.append('GET\n');
canonical.append(canonicalPath.getAsString());
canonical.append('\n\n');
canonical.append(headers.getAsString());
canonical.append('\n');
canonical.append(signedHeaders);
canonical.append('\n');
canonical.append(crypto.hashStringENC(''));
final scope = CkStringBuilder();
scope.append(date);
scope.append('/');
scope.append(region);
scope.append('/ses/aws4_request');
final toSign = CkStringBuilder();
toSign.append('AWS4-HMAC-SHA256\n');
toSign.append(stamp.getAsString());
toSign.append('\n');
toSign.append(scope.getAsString());
toSign.append('\n');
toSign.append(crypto.hashStringENC(canonical.getAsString()));
// Derive the signing key one HMAC at a time. Decode each intermediate key from hex.
final seed = CkStringBuilder();
seed.append('AWS4');
seed.append(secretKey);
crypto.setMacKeyString(seed.getAsString());
var key = crypto.macStringENC(date);
crypto.setMacKeyEncoded(key, 'hex');
key = crypto.macStringENC(region);
crypto.setMacKeyEncoded(key, 'hex');
key = crypto.macStringENC('ses');
crypto.setMacKeyEncoded(key, 'hex');
key = crypto.macStringENC('aws4_request');
crypto.setMacKeyEncoded(key, 'hex');
final signature = crypto.macStringENC(toSign.getAsString());
final authorization = CkStringBuilder();
authorization.append('AWS4-HMAC-SHA256 Credential=');
authorization.append(accessKey);
authorization.append('/');
authorization.append(scope.getAsString());
authorization.append(', SignedHeaders=');
authorization.append(signedHeaders);
authorization.append(', Signature=');
authorization.append(signature);
rest.addHeader('Authorization', authorization.getAsString());
final connection = CkSocket();
connection.requireSslCertVerify = true;
try {
connection.connect(host.getAsString(), 443, true, 10000);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
rest.useConnection(connection, false);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final String responseText;
try {
responseText = rest.fullRequestNoBody('GET', path.getAsString());
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (rest.responseStatusCode == 404) {
print('No account suppression entry for this address in this Region.');
return;
}
if (rest.responseStatusCode != 200) {
print('SES returned HTTP ${rest.responseStatusCode} $responseText');
return;
}
final response = CkJsonObject();
try {
response.load(responseText);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print(response.stringOf('SuppressedDestination.EmailAddress'));
print('Reason: ${response.stringOf('SuppressedDestination.Reason')}');
print('Last updated (Unix seconds): ${response.stringOf('SuppressedDestination.LastUpdateTime')}');
}