Sample code for 30+ languages & platforms
Zig

Iterate Email Headers

Demonstrates how to iterate over the email header fields.

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

    // First, load an email from a file.
    // Note: an email object may be loaded from a file, or
    // downloaded directly from a POP3 or IMAP server...

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

    // How many header fields?
    const n = email.getNumHeaderFields();
    if (n > 0) {
        // Display the name and value of each header:
        var i: i32 = 0;
        var name: [:0]const u8 = "";
        var value: [:0]const u8 = "";
        i = 0;
        while (i < n) : (i += 1) {
            name = try email.getHeaderFieldName(alloc, i);
            value = try email.getHeaderFieldValue(alloc, i);
            std.debug.print("{d}\n", .{i});
            std.debug.print("{s}\n", .{name});
            std.debug.print("{s}\n", .{value});
        }
    }
}