Sample code for 30+ languages & platforms
Zig

Send a Bundle of Emails via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendBundle method, which sends each email in an EmailBundle. This is equivalent to calling SendEmail once for each email in the bundle. This example builds a bundle of two messages and sends them.

Background: Sending a bundle lets Chilkat reuse a single SMTP connection for all the messages, avoiding the connect/authenticate overhead of sending them one at a time — the same efficiency as the explicit open/send-loop/close pattern, in one call. It is handy for dispatching a queued batch of distinct messages (as opposed to one message to many recipients).

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

    // Demonstrates the MailMan.SendBundle method, which sends each email in an EmailBundle.
    // This is equivalent to calling SendEmail once for each email in the bundle.

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

    // Configure the SMTP server connection.
    mailman.setSmtpHost("smtp.example.com");
    mailman.setSmtpPort(465);
    mailman.setSmtpSsl(true);
    mailman.setSmtpUsername("user@example.com");
    mailman.setSmtpPassword("myPassword");

    // Build a bundle of emails to send.
    const bundle = try chilkat.EmailBundle.init();
    defer bundle.deinit();

    const email1 = try chilkat.Email.init();
    defer email1.deinit();
    email1.setSubject("Message 1");
    email1.setFrom("alice@example.com");
    email1.addTo("Bob", "bob@example.com") catch {};
    email1.setBody("This is the first message.");
    bundle.addEmail(email1) catch {};

    const email2 = try chilkat.Email.init();
    defer email2.deinit();
    email2.setSubject("Message 2");
    email2.setFrom("alice@example.com");
    email2.addTo("Carol", "carol@example.com") catch {};
    email2.setBody("This is the second message.");
    bundle.addEmail(email2) catch {};

    // Send every email in the bundle.

    mailman.sendBundle(bundle) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Sent {d} emails.\n", .{bundle.getMessageCount()});

    // Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    // connects and authenticates -- using the property settings above -- whenever a server
    // operation requires it.  Calling the explicit connect/authenticate methods can still be
    // helpful to determine whether a failure occurs while connecting or while authenticating.
}