Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Forward an Email using CreateForward

See more Email Object Examples

Reads an email from an IMAP server, creates a forward version of the email using the CreateForward method, and sends the email to another recipient.

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.

    // Read the 1st (most recent) email in an Inbox.
    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    // Connect to an IMAP server.
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Login
    imap.login("myLogin", "myPassword") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Select an IMAP mailbox
    imap.selectMailbox("Inbox") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    const num_emails = imap.getNumMessages();

    // Fetch the email at the last sequence number.
    // (We are assuming the Inbox has at least 1 email)

    const email = try chilkat.Email.init();
    defer email.deinit();
    imap.fetchEmail(false, @as(u32, @intCast(num_emails)), false, email) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Disconnect from the IMAP server.
    imap.disconnect() catch {};

    std.debug.print("{s}\n", .{try email.getSubject(alloc)});

    const e_forward = try chilkat.Email.init();
    defer e_forward.deinit();
    email.toForward(e_forward) catch {
        std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
        return;
    };

    // The eForward email has no To or CC recipients yet.
    // Add one or more..
    e_forward.addTo("Joe", "joe@example.com") catch {};

    // We also need to specify the From name/address.
    e_forward.setFromAddress("matt@someMailServer.com");
    e_forward.setFromName("Matt");

    // If we wish to add text at the start of the email body:
    const sb_html_body = try chilkat.StringBuilder.init();
    defer sb_html_body.deinit();
    if (e_forward.hasHtmlBody()) {
        sb_html_body.append(try e_forward.getHtmlBody(alloc)) catch {};
        sb_html_body.prepend("<p>Hello, this is an email I'm forwarding to you...</p>") catch {};
        e_forward.setHtmlBody(try sb_html_body.getAsString(alloc));
    }

    const sb_pt_body = try chilkat.StringBuilder.init();
    defer sb_pt_body.deinit();
    if (e_forward.hasPlainTextBody()) {
        sb_pt_body.append(try e_forward.getPlainTextBody(alloc)) catch {};
        sb_pt_body.prepend("Hello, this is an email I'm forwarding to you...\r\n\r\n") catch {};
        e_forward.setTextBody(try sb_pt_body.getAsString(alloc), "text/plain");
    }

    // We could save the .eml, then double-click on it to view in our mail program, such as Outlook or Thunderbird..
    e_forward.saveEml("qa_output/forward.eml") catch {};

    // We could send (forward) the email..
    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    mailman.setSmtpHost("smtp.example.com");
    mailman.setSmtpUsername("myLogin");
    mailman.setSmtpPassword("myPassword");
    mailman.setSmtpPort(587);
    mailman.setStartTLS(true);

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

    mailman.closeSmtpConnection() catch {
        std.debug.print("Connection to SMTP server not closed cleanly.\n", .{});
    };

    std.debug.print("Mail Sent!\n", .{});
}