Zig
Zig
Save All Email Attachments to a Directory
See more Email Object Examples
Demonstrates the Chilkat Email.SaveAllAttachments method, which saves all of the email's attachments to a directory. If the directory or any of its components do not exist, Chilkat creates them automatically. This example adds two attachments and saves them.
Background: Each attachment is written using its own filename. Because those filenames come from the sender and may collide, consider the
OverwriteExisting property (which can auto-generate unique names) and RemoveAttachmentPaths (which strips embedded path info) to keep saved files safe and non-clobbering — important when processing untrusted incoming mail.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates the SaveAllAttachments method, which saves all attachments to a directory.
// If the directory (or any of its components) does not exist, Chilkat creates it.
const email = try chilkat.Email.init();
defer email.deinit();
email.setSubject("Save all attachments");
email.addStringAttachment("a.txt", "first attachment") catch {};
email.addStringAttachment("b.txt", "second attachment") catch {};
// Save every attachment into the specified directory.
email.saveAllAttachments("qa_output/attachments") catch {
std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
return;
};
std.debug.print("Saved all attachments.\n", .{});
// Note: The path "qa_output/..." is a relative local filesystem path,
// relative to the current working directory of the running application.
}