Sample code for 30+ languages & platforms
Zig

Send HTML Email with Image to iPhone

Demonstrates how to compose an HTML email with an embedded image that will display correctly on an iPhone. This example produces an email that looks like this on the iPhone:

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

    // Use your SMTP server hostname.  This example uses office365, but it could be any SMTP server..
    mailman.setSmtpHost("outlook.office365.com");
    mailman.setSmtpPort(587);
    mailman.setStartTLS(true);

    // Set the SMTP login/password
    mailman.setSmtpUsername("OFFICE365-SMTP-LOGIN");
    mailman.setSmtpPassword("OFFICE365-SMTP-PASSWORD");

    // Create a new email object
    const email = try chilkat.Email.init();
    defer email.deinit();
    email.setSubject("Testing for Chilkat SMTP API...");
    email.setBody("Testing for Chilkat SMTP API...");
    email.setFrom("Chilkat Support <my-office365-user@mydomain.com>");
    email.addTo("Chilkat Support", "support@chilkatsoft.com") catch {};

    // For whatever reason, the iPhone's email program requires
    // images in HTML to be referenced by Content-ID.  Therefore,
    // we must add the image like this:
    const content_id_starfish = email.addRelatedFile(alloc, "qa_data/jpg/starfish.jpg") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // The src attribute for the image tag is set to the contentIdStarfish:
    const sb_html = try chilkat.StringBuilder.init();
    defer sb_html.deinit();
    sb_html.append("<html><body><p>This is an HTML email with an embedded image.</p>") catch {};
    sb_html.append("<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>") catch {};
    _ = sb_html.replace("CONTENT_ID_STARFISH", content_id_starfish);

    email.setHtmlBody(try sb_html.getAsString(alloc));

    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", .{});
    }
}