Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Scan for Emails with Attachments and Save Attachments to Files

Scan for emails with attachments and save attachments.

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    // Connect to an IMAP server.
    // Use TLS
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Login
    imap.login("myLogin", "myPassword") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Select an IMAP mailbox
    imap.selectMailbox("Inbox") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // We can choose to fetch UIDs or sequence numbers.
    const fetch_uids = true;

    // Get the message IDs of all the emails in the mailbox
    const message_set = try chilkat.MessageSet.init();
    defer message_set.deinit();
    imap.queryMbx("ALL", fetch_uids, message_set) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Fetch the email headers into a bundle object:
    const bundle = try chilkat.EmailBundle.init();
    defer bundle.deinit();
    const headers_only = true;
    imap.fetchMsgSet(headers_only, message_set, bundle) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Scan for emails with attachments, and save the attachments
    // to a sub-directory.
    const full_email = try chilkat.Email.init();
    defer full_email.deinit();
    const email_header = try chilkat.Email.init();
    defer email_header.deinit();
    var i: i32 = 0;
    while (i < bundle.getMessageCount()) {
        // The bundle contains email headers..
        bundle.emailAt(i, email_header) catch {};

        // Does this email have attachments?
        // Use GetMailNumAttach because the attachments
        // are not actually in the email object because
        // we only downloaded headers.
        const num_attach = imap.getMailNumAttach(email_header);

        if (num_attach > 0) {
            // Download the entire email and save the
            // attachments. (Remember, we
            // need to download the entire email because
            // only the headers were previously downloaded.

            // The ckx-imap-uid header field is added when
            // headers are downloaded.  This makes it possible
            // to get the UID from the email object.
            const uid_str = try email_header.getHeaderField(alloc, "ckx-imap-uid");
            const uid = (std.fmt.parseInt(i32, uid_str, 10) catch 0);

            imap.fetchEmail(false, @as(u32, @intCast(uid)), true, full_email) catch {
                std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
                return;
            };

            try full_email.saveAllAttachments("attachmentsDir");

            var j: i32 = 0;
            j = 0;
            while (j < num_attach) : (j += 1) {
                const filename = try imap.getMailAttachFilename(alloc, email_header, j);
                std.debug.print("{s}\n", .{filename});
            }
        }

        i = i + 1;
    }

    // Disconnect from the IMAP server.
    try imap.disconnect();
}