Sample code for 30+ languages & platforms
Zig

Get an Attached Message as an Email Object

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachedEmail method, which copies the Nth embedded message/rfc822 MIME part into another Email object. The index is zero-based (valid values run from 0 through NumAttachedMessages - 1). This example attaches an email and then extracts it back into its own object.

Background: When a message forwards another email as an attachment, that nested message is a complete email in its own right. GetAttachedEmail turns it back into a fully navigable Email object so you can read its subject, sender, body, and even its own attachments — essential for processing forwarded mail, or for tools that unpack reported spam/phishing to examine the original.

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 GetAttachedEmail method, which copies the Nth embedded message/rfc822
    // MIME part into another Email object.  The index is zero-based.

    // Build an inner email and attach it to an outer email.
    const inner_email = try chilkat.Email.init();
    defer inner_email.deinit();
    inner_email.setSubject("Embedded message");
    inner_email.setFrom("alice@example.com");
    inner_email.setBody("This is the embedded message.");

    const email = try chilkat.Email.init();
    defer email.deinit();
    email.setSubject("Has an attached message");
    email.attachEmail(inner_email) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // Copy the first embedded message (index 0) into its own Email object.
    const attached = try chilkat.Email.init();
    defer attached.deinit();
    email.getAttachedEmail(0, attached) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Attached email subject: {s}\n", .{try attached.getSubject(alloc)});
    std.debug.print("Attached email from: {s}\n", .{try attached.getFrom(alloc)});
}