Sample code for 30+ languages & platforms
Zig

Send HTML Email with Image Downloaded from URL

Demonstrates how to compose an HTML email with an embedded image where the image data is downloaded from a URL.

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

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

    // First download the image we'll be adding to the HTML "img" tag.
    const bd_jpg = try chilkat.BinData.init();
    defer bd_jpg.deinit();
    const http = try chilkat.Http.init();
    defer http.deinit();
    http.quickGetBd("https://www.chilkatsoft.com/images/starfish.jpg", bd_jpg) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Use your SMTP server..
    mailman.setSmtpHost("smtp.yourserver.com");
    mailman.setSmtpPort(587);
    mailman.setStartTLS(true);

    // Set the SMTP login/password
    mailman.setSmtpUsername("my_login");
    mailman.setSmtpPassword("my_password");

    // Create an HTML email.
    const email = try chilkat.Email.init();
    defer email.deinit();
    email.setSubject("HTML Email with Image");
    email.setFrom("Dave <somebody@mydomain.com>");
    email.addTo("Chilkat", "info@chilkatsoft.com") catch {};

    const html = "<html><body><p>This is an HTML email with an embedded image.</p><p><img src=\"starfish.jpg\" /></p></body></html>";
    email.setHtmlBody(html);

    // Note: The "starfish.jpg" here must match the name in the "img" tag's "src" attribute in the HTML above.
    email.addRelatedBd2(bd_jpg, "starfish.jpg") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    email.saveEml("qa_output/out.eml") catch {};

    // success = mailman.SendEmail(email);
    // if (success == ckfalse) {
    //     println mailman.LastErrorText;
    //     return;
    // }
    //
    // ignore = mailman.CloseSmtpConnection();

    std.debug.print("Success.\n", .{});
}