Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

local imapSrc = chilkat.Imap.new()

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

-- Connect to our source IMAP server.
imapSrc.ssl = true
imapSrc.port = 993
local success = imapSrc:connect("MY-IMAP-DOMAIN")
if not success then
    print(imapSrc.last_error_text)
    return
end

-- Login to the source IMAP server
success = imapSrc:login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD")
if not success then
    print(imapSrc.last_error_text)
    return
end

local imapDest = chilkat.Imap.new()

-- Connect to our destination IMAP server.
imapDest.ssl = true
imapDest.port = 993
success = imapDest:connect("MY-IMAP-DOMAIN2")
if not success then
    print(imapDest.last_error_text)
    return
end

-- Login to the destination IMAP server
success = imapDest:login("MY-IMAP-LOGIN2", "MY-IMAP-PASSWORD2")
if not success then
    print(imapDest.last_error_text)
    return
end

-- Select a source IMAP mailbox on the source IMAP server
success = imapSrc:select_mailbox("Inbox")
if not success then
    print(imapSrc.last_error_text)
    return
end

local fetchUids = true

-- Get the set of UIDs for all emails on the source server.
local mset = ERROR: Method not in Chilkat for Lua: Imap.Search
imapSrc:ERROR()
if not imapSrc.last_method_success then
    print(imapSrc.last_error_text)
    return
end

-- Load the complete set of UIDs that were previously copied.
-- We dont' want to copy any of these to the destination.
local fac = chilkat.FileAccess.new()
local msetAlreadyCopied = chilkat.MessageSet.new()
local strMsgSet = fac:read_entire_text_file("qa_cache/saAlreadyLoaded.txt", "utf-8")
if fac.last_method_success then
    msetAlreadyCopied:from_compact_string(strMsgSet)
end

local numUids = mset.count
local sbFlags = chilkat.StringBuilder.new()

local i = 0
while i < numUids do
    -- If this UID was not already copied...
    local uid = mset:get_id(i)
    if not msetAlreadyCopied:contains_id(uid) then
        print("copying " .. uid .. "...")

        -- Get the flags.
        local flags = imapSrc:fetch_flags(uid, true)
        if not imapSrc.last_method_success then
            print(imapSrc.last_error_text)
            return
        end

        sbFlags:set_string(flags)

        -- Get the MIME of this email from the source.
        local mimeStr = imapSrc:fetch_single_as_mime(uid, true)
        if not imapSrc.last_method_success then
            print(imapSrc.last_error_text)
            return
        end

        local seen = sbFlags:contains("\\Seen", false)
        local flagged = sbFlags:contains("\\Flagged", false)
        local answered = sbFlags:contains("\\Answered", false)
        local draft = sbFlags:contains("\\Draft", false)

        success = imapDest:append_mime_with_flags("Inbox", mimeStr, seen, flagged, answered, draft)
        if not success then
            print(imapDest.last_error_text)
            return
        end

        -- Update msetAlreadyCopied with the uid just copied.
        msetAlreadyCopied:insert_id(uid)

        -- Save at every iteration just in case there's a failure..
        strMsgSet = msetAlreadyCopied:to_compact_string()
        fac:write_entire_text_file("qa_cache/saAlreadyLoaded.txt", strMsgSet, "utf-8", false)
    end

    i = i + 1
end

-- Disconnect from the IMAP servers.
success = imapSrc:disconnect()
success = imapDest:disconnect()