Sample code for 30+ languages & platforms
Zig

Create multipart/alternative MIME

See more MIME Examples

Demonstrates how to create a multipart/alternative MIME structure using the NewMultipartAlternative method.

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.

    // Start with an empty MIME object...
    const mime = try chilkat.Mime.init();
    defer mime.deinit();

    // Initialize it as multipart/alternative.
    mime.newMultipartAlternative() catch {};

    // Create a text/plain part.
    const pt_mime = try chilkat.Mime.init();
    defer pt_mime.deinit();
    pt_mime.setBodyFromPlainText("This is the plain text body.") catch {};
    std.debug.print("{s}\n", .{try pt_mime.getMime(alloc)});
    std.debug.print("****\n", .{});

    // The text/plain part looks like this:

    //     Content-Type: text/plain
    //     Content-Transfer-Encoding: 7bit
    //
    //     This is the plain text body.

    // Create a text/HTML part.
    const html_mime = try chilkat.Mime.init();
    defer html_mime.deinit();
    html_mime.setBodyFromHtml("<html><body><p>This is the <b>HTML body</b></p></body></html>") catch {};
    std.debug.print("{s}\n", .{try html_mime.getMime(alloc)});
    std.debug.print("****\n", .{});

    // The text/html part looks like this:

    //     Content-Type: text/html
    //     Content-Transfer-Encoding: 7bit
    //
    //     <html><body><p>This is the <b>HTML body</b></p></body></html>

    // Add both parts as children of the multipart/alternative
    mime.appendPart(pt_mime) catch {};
    mime.appendPart(html_mime) catch {};
    std.debug.print("{s}\n", .{try mime.getMime(alloc)});

    // This the result.  The boundary string was automatically generaetd.

    //     Content-Type: multipart/alternative; boundary="------------080803090906030708030908"
    //
    //     --------------080803090906030708030908
    //     Content-Type: text/plain
    //     Content-Transfer-Encoding: 7bit
    //
    //     This is the plain text body.
    //     --------------080803090906030708030908
    //     Content-Type: text/html
    //     Content-Transfer-Encoding: 7bit
    //
    //     <html><body><p>This is the <b>HTML body</b></p></body></html>
    //     --------------080803090906030708030908--
}