Sample code for 30+ languages & platforms
Objective-C

Amazon SES v2: Publish delivery events to SNS and correlate a tagged send

See more Amazon SES Examples

Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.

Configure the SNS destination · SES event payload

Chilkat Objective-C Downloads

Objective-C
#import <NSString.h>
#import <CkoStringBuilder.h>
#import <CkoAuthAws.h>
#import <CkoSocket.h>
#import <CkoRest.h>
#import <CkoJsonObject.h>

BOOL success = NO;

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

BOOL configureDestination = YES;
NSString *setName = @"application-mail";
//  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
NSString *region = @"us-west-2";
CkoStringBuilder *host = [[CkoStringBuilder alloc] init];
[host Append: @"email."];
[host Append: region];
[host Append: @".amazonaws.com"];

CkoAuthAws *signer = [[CkoAuthAws alloc] init];
//  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";

CkoSocket *connection = [[CkoSocket alloc] init];
connection.RequireSslCertVerify = YES;
success = [connection Connect: [host GetAsString] port: [NSNumber numberWithInt: 443] ssl: YES maxWaitMs: [NSNumber numberWithInt: 10000]];
if (success != YES) {
    NSLog(@"%@",connection.LastErrorText);
    return;
}

CkoRest *rest = [[CkoRest alloc] init];
success = [rest UseConnection: connection autoReconnect: NO];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

success = [rest SetAuthAws: signer];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

//  For temporary AWS credentials, supply the session token from the same provider.
NSString *sessionToken = @"";
if (sessionToken != @"") {
    [rest AddQueryParam: @"X-Amz-Security-Token" value: sessionToken];
}

if (configureDestination == YES) {
    CkoStringBuilder *path = [[CkoStringBuilder alloc] init];
    [path Append: @"/v2/email/configuration-sets/"];
    [path Append: setName];
    [path Append: @"/event-destinations"];
    CkoJsonObject *destination = [[CkoJsonObject alloc] init];
    [destination UpdateString: @"EventDestinationName" value: @"delivery-to-sns"];
    [destination UpdateString: @"EventDestination.MatchingEventTypes[0]" value: @"DELIVERY"];
    [destination UpdateString: @"EventDestination.MatchingEventTypes[1]" value: @"BOUNCE"];
    [destination UpdateString: @"EventDestination.MatchingEventTypes[2]" value: @"COMPLAINT"];
    [destination UpdateString: @"EventDestination.SnsDestination.TopicArn" value: @"arn:aws:sns:us-west-2:123456789012:ses-delivery"];
    [destination UpdateBool: @"EventDestination.Enabled" value: YES];

    [rest AddHeader: @"Content-Type" value: @"application/json"];
    NSString *setupResponse = [rest FullRequestString: @"POST" uriPath: [path GetAsString] bodyText: [destination Emit]];
    if (rest.LastMethodSuccess != YES) {
        NSLog(@"%@",rest.LastErrorText);
        return;
    }

    if ([rest.ResponseStatusCode intValue] != 200) {
        NSLog(@"%@%d%@%@",@"SES returned HTTP ",[rest.ResponseStatusCode intValue],@" ",setupResponse);
        return;
    }

    NSLog(@"%@",@"Destination created. Use the send branch to test it.");
    return;
}

CkoJsonObject *request = [[CkoJsonObject alloc] init];
[request UpdateString: @"FromEmailAddress" value: @"sender@example.com"];
[request UpdateString: @"Destination.ToAddresses[0]" value: @"success@simulator.amazonses.com"];
[request UpdateString: @"Content.Simple.Subject.Data" value: @"Delivery event test"];
[request UpdateString: @"Content.Simple.Body.Text.Data" value: @"Exercise the application-mail configuration set."];
[request UpdateString: @"EmailTags[0].Name" value: @"notification_ref"];
[request UpdateString: @"EmailTags[0].Value" value: @"notify-1042"];
[request UpdateString: @"ConfigurationSetName" value: setName];

[rest AddHeader: @"Content-Type" value: @"application/json"];
NSString *responseText = [rest FullRequestString: @"POST" uriPath: @"/v2/email/outbound-emails" bodyText: [request Emit]];
if (rest.LastMethodSuccess != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d%@%@",@"SES returned HTTP ",[rest.ResponseStatusCode intValue],@" ",responseText);
    return;
}

CkoJsonObject *response = [[CkoJsonObject alloc] init];
success = [response Load: responseText];
if (success != YES) {
    NSLog(@"%@",response.LastErrorText);
    return;
}

NSLog(@"%@%@",@"SES accepted the message: ",[response StringOf: @"MessageId"]);