Sample code for 30+ languages & platforms
Zig

Send HTML Email with External CSS as Related Item

See more SMTP Examples

Demonstrates how to compose an HTML email with an external CSS file included as a related item and referenced by CID (Content-ID).

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("HTML Email with embedded CSS");
    email.setFrom("Chilkat Support <my-office365-user@mydomain.com>");
    email.addTo("Chilkat Support", "support@chilkatsoft.com") catch {};

    const sb_css = try chilkat.StringBuilder.init();
    defer sb_css.deinit();
    const b_crlf = true;
    sb_css.appendLine("body {", b_crlf) catch {};
    sb_css.appendLine("    background-color: powderblue;", b_crlf) catch {};
    sb_css.appendLine("}", b_crlf) catch {};
    sb_css.appendLine("h1 {", b_crlf) catch {};
    sb_css.appendLine("    color: blue;", b_crlf) catch {};
    sb_css.appendLine("}", b_crlf) catch {};
    sb_css.appendLine("p {", b_crlf) catch {};
    sb_css.appendLine("    color: red;", b_crlf) catch {};
    sb_css.appendLine("}", b_crlf) catch {};

    // It's possible to add a CSS file directly by calling AddRelatedFile.
    // This example will add the CSS from a string.
    const filename_in_html = "styles.css";
    const content_id_css = email.addRelatedString(alloc, filename_in_html, try sb_css.getAsString(alloc), "utf-8") catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // The src attribute for the image tag is set to the contentIdCss:
    const sb_html = try chilkat.StringBuilder.init();
    defer sb_html.deinit();
    sb_html.appendLine("<!DOCTYPE html>", b_crlf) catch {};
    sb_html.appendLine("<html>", b_crlf) catch {};
    sb_html.appendLine("<head>", b_crlf) catch {};
    sb_html.appendLine("  <link rel=\"stylesheet\" href=\"cid:CONTENT_ID_CSS\">", b_crlf) catch {};
    sb_html.appendLine("</head>", b_crlf) catch {};
    sb_html.appendLine("<body>", b_crlf) catch {};
    sb_html.appendLine("", b_crlf) catch {};
    sb_html.appendLine("<h1>This is a heading</h1>", b_crlf) catch {};
    sb_html.appendLine("<p>This is a paragraph.</p>", b_crlf) catch {};
    sb_html.appendLine("", b_crlf) catch {};
    sb_html.appendLine("</body>", b_crlf) catch {};
    sb_html.appendLine("</html>", b_crlf) catch {};

    _ = sb_html.replace("CONTENT_ID_CSS", content_id_css);

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