Sample code for 30+ languages & platforms
Rust

Zip an Email's Attachments into One File

See more Email Object Examples

Demonstrates the Chilkat Email.ZipAttachments method, which replaces all of an email's attachments with a single Zip file attachment having the specified filename. The original attachments are removed and packed into the Zip. This example adds two attachments and zips them into one.

Background: Bundling multiple attachments into a single .zip keeps a message tidy, compresses the payload, and works around recipients or gateways that limit the number of attachments. Note that the filename here (files.zip) names the attachment inside the message — it is not a path on disk. The reverse operation is UnzipAttachments.

Chilkat Rust Downloads

Rust

// Demonstrates the ZipAttachments method, which replaces all of an email's attachments
// with a single Zip file attachment having the specified filename.  The original
// attachments are removed and packed into the Zip.

let email = chilkat::Email::new();
email.set_subject("Zip the attachments");

let _ = email.add_string_attachment("a.txt", "first attachment");
let _ = email.add_string_attachment("b.txt", "second attachment");
println!("NumAttachments before = {}", email.num_attachments());

// Replace all attachments with a single Zip attachment named "files.zip".
if email.zip_attachments("files.zip").is_err() {
    println!("{}", email.last_error_text());
    return;
}

println!("NumAttachments after = {}", email.num_attachments());