Sample code for 30+ languages & platforms
Zig

Create a multipart/alternative MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.NewMultipartAlternative method, which initializes the object as an empty multipart/alternative entity. It takes no arguments; the alternative representations are then appended.

Background: multipart/alternative holds several renderings of the same content and lets the client pick the best one it can display — the standard way to send an email as both plain text and HTML. Order matters: parts go from least to most preferred, so the plain-text version comes first and the HTML last, and a client shows the last one it understands.

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.NewMultipartAlternative method, which initializes the object as an empty
    // multipart/alternative entity, clearing any existing content.  It takes no arguments.

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

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

    // multipart/alternative holds two representations of the SAME content; the client displays the
    // last one it can render.  Add a plain-text part, then an HTML part.
    const text_part = try chilkat.Mime.init();
    defer text_part.deinit();
    text_part.setBodyFromPlainText("Hello (plain text version).") catch {
        std.debug.print("{s}\n", .{try text_part.getLastErrorText(alloc)});
        return;
    };

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

    const html_part = try chilkat.Mime.init();
    defer html_part.deinit();
    html_part.setBodyFromHtml("<html><body><b>Hello</b> (HTML version).</body></html>") catch {
        std.debug.print("{s}\n", .{try html_part.getLastErrorText(alloc)});
        return;
    };

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

    std.debug.print("Number of alternatives: {d}\n", .{mime.getNumParts()});
}