Sample code for 30+ languages & platforms
Zig

Save Email Attachments to Filesystem

Saves email attachments to a directory.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const email = try chilkat.Email.init();
    defer email.deinit();

    // Load an email object containing attachments.
    // This .eml can be downloaded from:
    // http://www.example-code.com/testData/HtmlEmail.eml

    email.loadEml("HtmlEmail.eml") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // If OverwriteExisting is turned on, files with the same
    // name are overwritten.  If turned off, new/unique filenames
    // are automatically generated.  The filenames actually saved
    // are accessible via the GetAttachmentFilename method.
    email.setOverwriteExisting(true);

    // Save all attachments to the "myAttachments" subdirectory
    // found under the calling process's current working directory.
    // This directory is automatically created if it does not already
    // exist.
    email.saveAllAttachments("myAttachments") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // List the attachment filenames:
    var i: i32 = 0;
    i = 0;
    while (i < email.getNumAttachments()) : (i += 1) {
        std.debug.print("{s}\n", .{try email.getAttachmentFilename(alloc, i)});
    }
}