Sample code for 30+ languages & platforms
Zig

Embed Image in HTML Email

Demonstrates how to create and send an HTML email with an embedded image.

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

    var success: bool = false;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // The mailman object is used for sending and receiving email.
    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Set the SMTP server.
    mailman.setSmtpHost("smtp.comcast.net");

    // Create a new email object
    const email = try chilkat.Email.init();
    defer email.deinit();

    // Add an embedded image to the HTML email.
    const file_on_disk = "images/dude2.gif";
    const file_path_in_html = "dudeAbc.gif";

    // Embed the GIF image in the email.
    success = if (email.addRelatedFile2(file_on_disk, file_path_in_html)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    }

    // The src attribute for the image tag is set to the filePathInHtml:
    const html_body = "<html><body>Embedded Image:<br><img src=\"dudeAbc.gif\"></body></html>";

    // Set the basic email stuff: HTML body, subject, "from", "to";
    email.setHtmlBody(html_body);
    email.setSubject("Zig HTML email with an embedded image.");
    success = if (email.addTo("Admin", "admin@chilkatsoft.com")) true else |_| false;
    email.setFrom("Chilkat Support <support@chilkatsoft.com>");

    success = if (mailman.sendEmail(email)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
    } else {
        std.debug.print("Mail Sent!\n", .{});
    }
}