Sample code for 30+ languages & platforms
Zig

Append Part to MIME

See more MIME Examples

Demonstrates the AppendPart method for adding sub-parts to MIME.

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.

    // 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 ptMime contains:
    // ---------------------------------------
    //     Content-Type: text/plain
    //     Content-Transfer-Encoding: 7bit
    //
    //     This is the plain text body
    // ---------------------------------------

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

    // Initialize it to a multipart Content-Type.  We'll use multipart/mixed.
    top_level_mime.newMultipartMixed() catch {};

    // Append the ptMime as a child.
    top_level_mime.appendPart(pt_mime) catch {};
    std.debug.print("{s}\n", .{try top_level_mime.getMime(alloc)});
    std.debug.print("****\n", .{});

    // The topLevelMime contains:
    // --------------------------
    //     Content-Type: multipart/mixed; boundary="------------010907090104040508040404"
    //
    //     --------------010907090104040508040404
    //     Content-Type: text/plain
    //     Content-Transfer-Encoding: 7bit
    //
    //     This is the plain text body
    //     --------------010907090104040508040404--
    //

    // IMPORTANT: A copy of the MIME passed in AppendPart is appended.
    // Therefore, in this case, changing ptMime does not change the text/plain sub-part in topLevelMime.
    // For example:
    pt_mime.setBody("this is the new plain text body.");
    // topLevelMime is unchanged.
    std.debug.print("{s}\n", .{try top_level_mime.getMime(alloc)});
    std.debug.print("****\n", .{});

    // To change the content in topLevelMime, we must get the text/plain part.
    // For example:

    const pt_mime2 = try chilkat.Mime.init();
    defer pt_mime2.deinit();
    top_level_mime.partAt(0, pt_mime2) catch {};

    pt_mime2.setBody("this is the new plain text body.");
    // Now topLevelMime is changed.
    std.debug.print("{s}\n", .{try top_level_mime.getMime(alloc)});
}