Sample code for 30+ languages & platforms
Zig

Attach a File to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPartFromFile method, which reads a local file, creates an attachment part, and appends it. The only argument is the file path; the content headers are chosen from the filename extension.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the one-call way to add an attachment: Chilkat reads the file, infers its Content-Type from the extension, base64-encodes binary content, and appends it as a child part — typically inside a multipart/mixed. It is the building block behind adding attachments to an email, saving you from constructing the part and setting its headers by hand.

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 Mime.AppendPartFromFile method, which reads a local file, creates an attachment
    // part, and appends it.  The only argument is the file path; the content headers are chosen from
    // the filename extension.

    const mime = try chilkat.Mime.init();
    defer mime.deinit();
    mime.newMultipartMixed() catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // A message body part.
    const body_part = try chilkat.Mime.init();
    defer body_part.deinit();
    body_part.setBodyFromPlainText("Please see the attached files.") catch {
        std.debug.print("{s}\n", .{try body_part.getLastErrorText(alloc)});
        return;
    };

    mime.appendPart(body_part) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // Attach files directly.
    mime.appendPartFromFile("qa_data/report.pdf") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    mime.appendPartFromFile("qa_data/photo.jpg") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Number of parts: {d}\n", .{mime.getNumParts()});
}