Sample code for 30+ languages & platforms
Zig

Copy Email from one IMAP Account to Another

See more IMAP Examples

Demonstrates how to copy the email in a mailbox from one account to another.

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

    const imap_src = try chilkat.Imap.init();
    defer imap_src.deinit();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Connect to our source IMAP server.
    imap_src.setSsl(true);
    imap_src.setPort(993);
    imap_src.connect("MY-IMAP-DOMAIN") catch {
        std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
        return;
    };

    // Login to the source IMAP server
    imap_src.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD") catch {
        std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
        return;
    };

    const imap_dest = try chilkat.Imap.init();
    defer imap_dest.deinit();

    // Connect to our destination IMAP server.
    imap_dest.setSsl(true);
    imap_dest.setPort(993);
    imap_dest.connect("MY-IMAP-DOMAIN2") catch {
        std.debug.print("{s}\n", .{try imap_dest.getLastErrorText(alloc)});
        return;
    };

    // Login to the destination IMAP server
    imap_dest.login("MY-IMAP-LOGIN2", "MY-IMAP-PASSWORD2") catch {
        std.debug.print("{s}\n", .{try imap_dest.getLastErrorText(alloc)});
        return;
    };

    // Select a source IMAP mailbox on the source IMAP server
    imap_src.selectMailbox("Inbox") catch {
        std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
        return;
    };

    // Get the set of UIDs for all emails on the source server.
ERROR: Method not in the Zig package: Imap.Search
ERROR: Method not in the Zig package: Imap.Search
    const mset = imap_src.ERROR();
    if (!imap_src.getLastMethodSuccess()) {
        std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
        return;
    }

    // Load the complete set of UIDs that were previously copied.
    // We dont' want to copy any of these to the destination.
    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    const mset_already_copied = try chilkat.MessageSet.init();
    defer mset_already_copied.deinit();
    var str_msg_set: [:0]const u8 = fac.readEntireTextFile(alloc, "qa_cache/saAlreadyLoaded.txt", "utf-8") catch "";
    if (fac.getLastMethodSuccess()) {
        mset_already_copied.fromCompactString(str_msg_set) catch {};
    }

    const num_uids = mset.getCount();
    const sb_flags = try chilkat.StringBuilder.init();
    defer sb_flags.deinit();

    var i: i32 = 0;
    while (i < num_uids) {
        // If this UID was not already copied...
        const uid = @as(i32, @intCast(mset.getId(i)));
        if (!mset_already_copied.containsId(@as(u32, @intCast(uid)))) {
            std.debug.print("copying {d}...\n", .{uid});

            // Get the flags.
            const flags = imap_src.fetchFlags(alloc, @as(u32, @intCast(uid)), true) catch {
                std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
                return;
            };

            sb_flags.setString(flags) catch {};

            // Get the MIME of this email from the source.
            const mime_str = imap_src.fetchSingleAsMime(alloc, @as(u32, @intCast(uid)), true) catch {
                std.debug.print("{s}\n", .{try imap_src.getLastErrorText(alloc)});
                return;
            };

            const seen = sb_flags.contains("\\Seen", false);
            const flagged = sb_flags.contains("\\Flagged", false);
            const answered = sb_flags.contains("\\Answered", false);
            const draft = sb_flags.contains("\\Draft", false);

            imap_dest.appendMimeWithFlags("Inbox", mime_str, seen, flagged, answered, draft) catch {
                std.debug.print("{s}\n", .{try imap_dest.getLastErrorText(alloc)});
                return;
            };

            // Update msetAlreadyCopied with the uid just copied.
            mset_already_copied.insertId(@as(u32, @intCast(uid)));

            // Save at every iteration just in case there's a failure..
            str_msg_set = try mset_already_copied.toCompactString(alloc);
            fac.writeEntireTextFile("qa_cache/saAlreadyLoaded.txt", str_msg_set, "utf-8", false) catch {};
        }

        i = i + 1;
    }

    // Disconnect from the IMAP servers.
    try imap_src.disconnect();
    try imap_dest.disconnect();
}