Sample code for 30+ languages & platforms
Zig

Iterate MIME Parts of an Email

See more Email Object Examples

Demonstrates how to iterate over the MIME sub-parts of an email, and retrieve the content of each MIME sub-part body.

Note: This example requires some new features added to Chilkat v9.5.0.95.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // See the following Chilkat post to Quickly Understand Email MIME

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

    email.loadEml("qa_data/eml/sample.eml") catch {
        std.debug.print("Failed to load .eml\n", .{});
        return;
    };

    const sb_content_type = try chilkat.StringBuilder.init();
    defer sb_content_type.deinit();
    const case_sensitive = false;

    // Get the total number of non-multipart MIME sub-parts.
    // (This is a simple way of iterating over all the MIME leaf parts regardless of the MIME tree structure)
    const inline_only = false;
    const exclude_attachments = false;
    const search_spec = "*/*";

    const num_parts = email.getNumPartsOfType(search_spec, inline_only, exclude_attachments);
    var i: i32 = 0;
    while (i < num_parts) {
        // What is the Content-Type of this MIME part?
        sb_content_type.append(try email.getNthContentType(alloc, i, search_spec, inline_only, exclude_attachments)) catch {};
        if (sb_content_type.startsWith("text/", case_sensitive)) {
            // Get the text body of this MIME part.
            _ = try email.getNthTextPartOfType(alloc, i, search_spec, inline_only, exclude_attachments);
            std.debug.print("Got text body for {s}\n", .{try sb_content_type.getAsString(alloc)});
        } else {
            if (sb_content_type.contentsEqual("message/rfc822", case_sensitive)) {
                // If the Content-Type is message/rfc822, then the MIME body for this part contains a full embedded MIME messages.
                // Your application could load it into a Chilkat email object and recursively process...
                const attached_email = try chilkat.Email.init();
                defer attached_email.deinit();
                const bd_mime = try chilkat.BinData.init();
                defer bd_mime.deinit();
                email.getNthBinaryPartOfTypeBd(i, search_spec, inline_only, exclude_attachments, bd_mime) catch {};
                attached_email.setFromMimeBd(bd_mime) catch {};
                // Now your app can recursively process the attachedEmail...
            } else {
                // Get the bytes of this MIME body part.
                const bd = try chilkat.BinData.init();
                defer bd.deinit();
                email.getNthBinaryPartOfTypeBd(i, search_spec, inline_only, exclude_attachments, bd) catch {};
                std.debug.print("Got binary body for {s} numBytes = {d}\n", .{ try sb_content_type.getAsString(alloc), bd.getNumBytes() });
            }
        }

        sb_content_type.clear();
        i = i + 1;
    }
}