Rust
Rust
Amazon SES v2: Send a raw MIME email with a PDF attachment
See more Amazon SES Examples
Place invoice.pdf in the working directory. Chilkat builds the MIME message and gives the attachment the name invoice-1042.pdf. The v2 SendEmail request carries those bytes in Content.Raw.Data. For a blind copy, supply the address in Destination.BccAddresses and keep it out of the MIME headers.
Use a verified sender and grant ses:SendEmail for this v2 operation. The v2 message size limit is 40 MB including attachments after MIME encoding. For a straightforward attachment that needs no custom MIME layout, v2 also supports Content.Simple.Attachments.
Sending raw email · SES attachments · Sending quotas
Chilkat Rust Downloads
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let _ = false;
// Read the PDF before making a network request.
let pdf = chilkat::BinData::new();
if pdf.load_file("invoice.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
if pdf.num_bytes() == 0 {
println!("invoice.pdf is empty.");
return;
}
let sender = "sender+reports@example.com".to_string();
let recipient = "success@simulator.amazonses.com".to_string();
let email = chilkat::Email::new();
email.set_from(&sender);
email.set_subject("Invoice 1042");
email.set_charset("utf-8");
if email.add_to("", &recipient).is_err() {
println!("{}", email.last_error_text());
return;
}
let _ = email.add_plain_text_alternative_body("Invoice 1042 is attached.");
let _ = email.add_html_alternative_body("<p>Invoice <strong>1042</strong> is attached.</p>");
if email.add_attachment_bd("invoice-1042.pdf", &pdf, "application/pdf").is_err() {
println!("{}", email.last_error_text());
return;
}
// Get the exact MIME bytes, including the attachment's transfer encoding.
let mime = chilkat::BinData::new();
if email.get_mime_bd(&mime).is_err() {
println!("{}", email.last_error_text());
return;
}
// Base64 here transports the entire MIME message as a blob in the JSON request.
let request = chilkat::JsonObject::new();
let _ = request.update_string("FromEmailAddress", &sender);
let _ = request.update_string("Destination.ToAddresses[0]", &recipient);
let _ = request.update_string("Content.Raw.Data", &mime.get_encoded("base64").unwrap_or_default());
// 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(®ion);
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(®ion);
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);
}
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 result = chilkat::JsonObject::new();
if result.load(&response_text).is_err() {
println!("{}", result.last_error_text());
return;
}
if !result.has_member("MessageId") {
println!("SES returned no message ID.");
return;
}
println!("SES accepted the message. Message ID: {}", result.string_of("MessageId").unwrap_or_default());