Sample code for 30+ languages & platforms
Zig

Get an Alternative Body by Content-Type

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyByContentType method, which returns an alternative body selected by its Content-Type — such as text/plain, text/html, or text/xml — instead of by numeric index. This example retrieves both the plain-text and HTML alternatives by content type.

Background: Selecting by Content-Type is often more convenient than by index, because you usually know which representation you want (the HTML, say) but not its position in the alternative list. This mirrors how a mail client works — it looks up the best matching type to display — and avoids assumptions about ordering, which can vary between messages.

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 GetAlternativeBodyByContentType method, which returns an alternative
    // body selected by its Content-Type (such as text/plain, text/html, or text/xml) rather
    // than by index.

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

    // Create an email with plain-text and HTML alternatives.
    email.setTextBody("This is the plain-text alternative.", "text/plain");
    email.addHtmlAlternativeBody("<html><body>This is the HTML alternative.</body></html>") catch {};

    // Get the plain-text alternative by its content type.
    const plain = try email.getAlternativeBodyByContentType(alloc, "text/plain");
    std.debug.print("text/plain body: {s}\n", .{plain});

    // Get the HTML alternative by its content type.
    const html_body = try email.getAlternativeBodyByContentType(alloc, "text/html");
    std.debug.print("text/html body: {s}\n", .{html_body});
}