Sample code for 30+ languages & platforms
Zig

Add File Attachments to an Email

Demonstrates how to add one or more file attachments to an email.

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();

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

    email.setSubject("This is a test");
    email.setBody("This is a test");
    email.setFrom("support@chilkatsoft.com");
    try email.addTo("Chilkat Admin", "admin@chilkatsoft.com");

    // To add file attachments to an email, call AddFileAttachment
    // once for each file to be attached.  The method returns
    // the content-type of the attachment if successful, otherwise
    // returns cknull
    var content_type: [:0]const u8 = "";

    content_type = email.addFileAttachment(alloc, "something.pdf") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    content_type = email.addFileAttachment(alloc, "something.xml") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    content_type = email.addFileAttachment(alloc, "something.zip") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    email.saveEml("email.eml") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Saved EML!\n", .{});
}