Zig Requires Chilkat v11.0.0+
Zig
Search IMAP Mailbox for Email Matching Criteria
Searching an IMAP mailbox for messages that match search criteria.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.
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;
// Here are examples of different search criteria:
// Return all messages.
// Search for already-answered emails.
// Search for messages on a specific date.
// The date string is DD-Month-YYYY where Month is
// Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, or Dec.
// Search for messages between two dates. SENTBEFORE
// finds emails sent before a date, and SENTSINCE finds
// email sent on or after a date. The "AND" operation
// is implied by joining criteria, separated by spaces.
// Another example of AND: find all unanswered emails
// sent after 04-Mar-2007 with "Problem" in the subject:
_ = "UNANSWERED SENTSINCE 04-Mar-2007 Subject \"Problem\"";
// Find messages with a specific string in the body:
_ = "BODY \"problem solved\"";
// Using OR. The syntax is OR <criteria1> <criteria2>.
// The "OR" comes first, followed by each criteria.
// For example, to match all emails with "Help" or "Question" in the subject.
// You'll notice that literal strings may be quoted or unquoted.
// If a literal contains SPACE characters, quote it:
const or_search = "OR SUBJECT Help SUBJECT Question";
// ----------------------------------------------
// Strings are case-insensitive when searching....
// ----------------------------------------------
// Find all emails sent from yahoo.com addresses:
// Find all emails sent from anyone with "John" in their name:
// Find emails with the RECENT flag set:
// Find emails that don't have the recent flag set:
// This is synonymous with "OLD":
// Find all emails marked for deletion:
// Find all emails having a specified header field with a value
// containing a substring:
// Find any emails having a specific header field. If the
// 2nd argument to the "HEADER" criteria is an empty string,
// any email having the header field is returned regardless
// of the header field's content.
// Find any emails with a DomainKey-Signature field:
_ = "HEADER DomainKey-Signature \"\"";
// Find NEW emails: these are emails that have the RECENT flag
// set, but not the SEEN flag:
// Find emails larger than a certain number of bytes:
// Find emails marked as seen or not already seen:
// Find emails having a given substring in the TO header field:
// A more long-winded way to do the same thing:
// Find emails smaller than a size in bytes:
// Find emails that have a substring anywhere in the header
// or body:
_ = "TEXT \"Zip Component\"";
// Pass any of the above strings here to test a search:
const message_set = try chilkat.MessageSet.init();
defer message_set.deinit();
imap.queryMbx(or_search, fetch_uids, message_set) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Fetch the email headers into a bundle object:
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;
};
// Display the Subject and From of each email.
const email = try chilkat.Email.init();
defer email.deinit();
var i: i32 = 0;
while (i < bundle.getMessageCount()) {
bundle.emailAt(i, email) catch {};
std.debug.print("{s}\n", .{try email.getHeaderField(alloc, "Date")});
std.debug.print("{s}\n", .{try email.getSubject(alloc)});
std.debug.print("{s}\n", .{try email.getFrom(alloc)});
std.debug.print("--\n", .{});
i = i + 1;
}
// Disconnect from the IMAP server.
try imap.disconnect();
}