Sample code for 30+ languages & platforms
Zig

Get an Attachment's Bytes into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentBd method, which copies an attachment's binary data into a BinData object. The first attachment is at index 0. This example adds an attachment and copies its bytes into a BinData, printing the byte count.

Background: This is the safe, binary way to extract an attachment — the counterpart to the text-oriented GetAttachmentString. Because attachments are often binary (PDFs, images, archives), copying the raw bytes into a BinData preserves them exactly, ready to write to a file, hash, or pass to another API without any charset conversion that could corrupt the data.

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 GetAttachmentBd method, which copies an attachment's binary data into a
    // BinData object.  The first attachment is at index 0.

    const email = try chilkat.Email.init();
    defer email.deinit();
    email.setSubject("GetAttachmentBd example");

    email.addStringAttachment("notes.txt", "Some notes stored in the attachment.") catch {};

    // Copy the first attachment's binary data into a BinData object.
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    email.getAttachmentBd(0, bd) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Attachment size (bytes) = {d}\n", .{bd.getNumBytes()});
}