Sample code for 30+ languages & platforms
Zig

Add a MIME Header Field

See more MIME Examples

Demonstrates the Chilkat Mime.AddHeaderField method, which appends a new header field. The first argument is the field name and the second is the value. Existing fields with the same case-insensitive name are preserved, so this can create duplicates.

Background: MIME headers are ordered name/value lines, and some — Received, for instance — legitimately appear more than once. AddHeaderField is the tool for that: it always appends, never replacing an existing field of the same name. When you instead want exactly one field with a given name, use SetHeaderField, which replaces rather than accumulates.

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 Mime.AddHeaderField method, which appends a new header field.  The 1st argument
    // is the field name and the 2nd is the value.  Existing fields with the same (case-insensitive)
    // name are preserved, so this can create duplicate fields.

    const mime = try chilkat.Mime.init();
    defer mime.deinit();

    mime.setBodyFromPlainText("Hello.") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // Append header fields.  AddHeaderField does not replace existing fields of the same name.
    mime.addHeaderField("X-Custom-Header", "value1") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    mime.addHeaderField("X-Priority", "3") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    const headers = mime.getEntireHead(alloc) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{headers});
}