Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

// Assumes Chilkat is unlocked. See Global Unlock Sample.
let _ = false;

let configure_destination = true;
let set_name = "application-mail".to_string();
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
let region = "us-west-2".to_string();
let host = chilkat::StringBuilder::new();
let _ = host.append("email.");
let _ = host.append(&region);
let _ = host.append(".amazonaws.com");

let signer = chilkat::AuthAws::new();
// Supply the credentials from your application's credential provider.
signer.set_access_key("AWS_ACCESS_KEY_ID");
signer.set_secret_key("AWS_SECRET_ACCESS_KEY");
signer.set_region(&region);
signer.set_service_name("ses");

let connection = chilkat::Socket::new();
connection.set_require_ssl_cert_verify(true);
if connection.connect(&host.get_as_string().unwrap_or_default(), 443, true, 10000).is_err() {
    println!("{}", connection.last_error_text());
    return;
}

let rest = chilkat::Rest::new();
if rest.use_connection(&connection, false).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

if rest.set_auth_aws(&signer).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// For temporary AWS credentials, supply the session token from the same provider.
let session_token = String::new();
if session_token != "" {
    let _ = rest.add_query_param("X-Amz-Security-Token", &session_token);
}

if configure_destination {
    let path = chilkat::StringBuilder::new();
    let _ = path.append("/v2/email/configuration-sets/");
    let _ = path.append(&set_name);
    let _ = path.append("/event-destinations");
    let destination = chilkat::JsonObject::new();
    let _ = destination.update_string("EventDestinationName", "delivery-to-sns");
    let _ = destination.update_string("EventDestination.MatchingEventTypes[0]", "DELIVERY");
    let _ = destination.update_string("EventDestination.MatchingEventTypes[1]", "BOUNCE");
    let _ = destination.update_string("EventDestination.MatchingEventTypes[2]", "COMPLAINT");
    let _ = destination.update_string("EventDestination.SnsDestination.TopicArn", "arn:aws:sns:us-west-2:123456789012:ses-delivery");
    let _ = destination.update_bool("EventDestination.Enabled", true);

    let _ = rest.add_header("Content-Type", "application/json");
    let Ok(setup_response) = rest.full_request_string("POST", &path.get_as_string().unwrap_or_default(), &destination.emit().unwrap_or_default()) else {
        println!("{}", rest.last_error_text());
        return;
    };

    if rest.response_status_code() != 200 {
        println!("SES returned HTTP {} {}", rest.response_status_code(), setup_response);
        return;
    }

    println!("Destination created. Use the send branch to test it.");
    return;
}

let request = chilkat::JsonObject::new();
let _ = request.update_string("FromEmailAddress", "sender@example.com");
let _ = request.update_string("Destination.ToAddresses[0]", "success@simulator.amazonses.com");
let _ = request.update_string("Content.Simple.Subject.Data", "Delivery event test");
let _ = request.update_string("Content.Simple.Body.Text.Data", "Exercise the application-mail configuration set.");
let _ = request.update_string("EmailTags[0].Name", "notification_ref");
let _ = request.update_string("EmailTags[0].Value", "notify-1042");
let _ = request.update_string("ConfigurationSetName", &set_name);

let _ = rest.add_header("Content-Type", "application/json");
let Ok(response_text) = rest.full_request_string("POST", "/v2/email/outbound-emails", &request.emit().unwrap_or_default()) else {
    println!("{}", rest.last_error_text());
    return;
};

if rest.response_status_code() != 200 {
    println!("SES returned HTTP {} {}", rest.response_status_code(), response_text);
    return;
}

let response = chilkat::JsonObject::new();
if response.load(&response_text).is_err() {
    println!("{}", response.last_error_text());
    return;
}

println!("SES accepted the message: {}", response.string_of("MessageId").unwrap_or_default());