Sample code for 30+ languages & platforms
Zig

Get the Number of IMAP Attachments

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailNumAttach method, which returns the number of ordinary attachments on a message. The only argument is the Email. This example fetches only the headers and reports the attachment count.

Background: A header-only fetch downloads no attachment bodies, so the Email object's own downloaded-attachment count can be 0. GetMailNumAttach instead reads the ckx-imap-numAttach metadata, giving the true count — which is what you want when building a message list that shows whether each message has 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();

    // Demonstrates the Imap.GetMailNumAttach method, which returns the number of ordinary
    // attachments on a message.  The only argument is the Email.

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

    imap.setSsl(true);
    imap.setPort(993);

    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    imap.login("user@example.com", "myPassword") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

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

    // Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
    // metadata describing the attachments is present, so the attachment info methods still work.
    const headers_only = true;
    const use_uid = false;
    const seq_num = 1;
    const email = try chilkat.Email.init();
    defer email.deinit();
    imap.fetchEmail(headers_only, @as(u32, @intCast(seq_num)), use_uid, email) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    const num_attach = imap.getMailNumAttach(email);
    std.debug.print("This message has {d} attachment(s).\n", .{num_attach});

    imap.disconnect() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };
}