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

Fetch Full Email Given Email Header

When you fetch email headers using UIDs instead of sequence numbers, the email object (which includes only the header) will have auto-generated ckx-imap-* headers. These headers provide details like the UID and attachments. The IMAP UID is found in the ckx-imap-uid header. Additionally, the ckx-imap-isUid header indicates whether the email header was downloaded by UID, showing YES or NO. Since sequence numbers can change if emails are deleted, UIDs are essential for downloading the correct full email.

The Chilkat Email object offers a GetImapUid method to retrieve the UID from the ckx-imap-uid header. This UID can be used to fetch the full email.

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("***", "***") 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;
    };

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

    const uid = 2014;
    const is_uid = true;

    // Fetch only the email header
    imap.fetchEmail(true, @as(u32, @intCast(uid)), is_uid, email_header) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Now fetch the full email
    const uid_from_ckx_header = @as(i32, @intCast(email_header.getImapUid()));
    if (uid_from_ckx_header < 0) {
        // Failed.
        std.debug.print("No ckx-imap-uid header was found.\n", .{});
        return;
    }

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

    // OK, we have the full email, do whatever we want...

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