Zig Requires Chilkat v11.0.0+
Zig
Forward by Attaching the Existing Email to a New Email
See more Email Object Examples
Demonstrates how to forward an email by attaching the email to a new email.This example reads an email from an IMAP server, attaches the email to a new email, and sends the new email.
Chilkat Zig Downloads
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)});
// Create a new email. The email we just read will be attached to this email.
const e_forward = try chilkat.Email.init();
defer e_forward.deinit();
e_forward.addTo("Joe", "joe@example.com") catch {};
e_forward.setFromAddress("matt@somewhere.com");
e_forward.setFromName("Matt");
e_forward.setSubject("This is an email with another email attached.");
e_forward.setHtmlBody("<p>Hello, this is an email I'm forwarding to you. See the attached email.</p>");
// Attach the email.
e_forward.attachEmail(email) catch {};
// 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", .{});
}