Sample code for 30+ languages & platforms
Zig

Remove a Single Attached Message from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachedMessage method, which removes the Nth message/rfc822 sub-part (an attached email) from the message. Indexing begins at 0. This example attaches an email, removes it, and prints the attached-message count before and after.

Background: Attached messages (forwarded emails carried as message/rfc822 parts) are counted and indexed separately from ordinary file attachments and related items. RemoveAttachedMessage targets just one nested email by index — useful, for example, when trimming a bundled digest or stripping a forwarded original before re-sending.

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 RemoveAttachedMessage method, which removes the Nth message/rfc822
    // sub-part (attached email) of the email.  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");

    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;
    };

    std.debug.print("NumAttachedMessages before = {d}\n", .{email.getNumAttachedMessages()});

    // Remove the first attached message (index 0).
    email.removeAttachedMessage(0);

    std.debug.print("NumAttachedMessages after = {d}\n", .{email.getNumAttachedMessages()});
}