Zig Requires Chilkat v11.0.0+
Zig
Process New Email by Scanning for Senders
Scan email and save application-selected emails to EML files with unique filenames.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 assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// Connect to an IMAP server.
// Use TLS
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;
};
// We can choose to fetch UIDs or sequence numbers.
const fetch_uids = true;
// Fetch messages from the mailbox using a search criteria.
// This example finds NEW emails: these are emails that have the RECENT flag set, but not the SEEN flag:
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
imap.queryMbx("NEW", fetch_uids, message_set) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// This example will download headers, and then download
// the full email for those emails sent from a contact
// in our database.
// When downloading headers, each email object contains
// (obviously) the headers, but the body will be missing.
// Also, attachments will not be included. However, it is
// possible to get information about the attachments
// as well as the complete size of the email.
const bundle = try chilkat.EmailBundle.init();
defer bundle.deinit();
const headers_only = true;
imap.fetchMsgSet(headers_only, message_set, bundle) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Loop over the email objects...
const email_header = try chilkat.Email.init();
defer email_header.deinit();
const full_email = try chilkat.Email.init();
defer full_email.deinit();
var i: i32 = 0;
const num_emails = bundle.getMessageCount();
while (i < num_emails) {
bundle.emailAt(i, email_header) catch {};
// The sender's email address and name are available
// in the From, FromAddress, and FromName properties.
// If the sender is "Chilkat Support <support@chilkatsoft.com",
// then the From property will hold the entire string.
// the FromName property contains"Chilkat Support",
// and the FromAddress property contains "support@chilkatsoft.com";
std.debug.print("{s}\n", .{try email_header.getFrom(alloc)});
std.debug.print("{s}\n", .{try email_header.getFromAddress(alloc)});
std.debug.print("{s}\n", .{try email_header.getFromName(alloc)});
// Assume at this point your code checks to see if the sender
// is one in your contacts database. If so, this is
// the code you would write to download the entire
// email and save it to a file.
// The ckx-imap-uid header field is added when
// headers are downloaded. This makes it possible
// to get the UID from the email object.
const uid_str = try email_header.getHeaderField(alloc, "ckx-imap-uid");
const uid = (std.fmt.parseInt(i32, uid_str, 10) catch 0);
imap.fetchEmail(false, @as(u32, @intCast(uid)), true, full_email) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// You can use the GenerateFilename method to
// generate a unique filename...
const filename = try full_email.generateFilename(alloc);
// SaveEml saves the entire email, including attachments.
try full_email.saveEml(filename);
std.debug.print("--\n", .{});
i = i + 1;
}
// Disconnect from the IMAP server.
try imap.disconnect();
}