Sample code for 30+ languages & platforms
Tcl

Copy an Email from One Mailbox to Another

Copies an email from one IMAP folder to another. After running this example, copies of the email will be present in both source and destination folders.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

# This example copies an email from one mailbox to another.
set imap [new_CkImap]

# Turn on session logging:
CkImap_put_KeepSessionLog $imap 1

# Connect to an IMAP server.
# Use TLS
CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

# Login
set success [CkImap_Login $imap "***" "***"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

# Select an IMAP mailbox
set success [CkImap_SelectMailbox $imap "Inbox.testing.a"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set fetchUids 1

# Get the message IDs for all emails having "Re:" in the subject.
set messageSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "SUBJECT Re:" $fetchUids $messageSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $messageSet
    exit
}

# Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
set success [CkImap_CopyMultiple $imap $messageSet "Inbox.testing.b"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $messageSet
    exit
}

# Alternatively, loop over each message in the set and
# copy each separately:
set i 0
while {$i < [CkMessageSet_get_Count $messageSet]} {

    set msgId [CkMessageSet_GetId $messageSet $i]
    set isUid [CkMessageSet_get_HasUids $messageSet]

    set success [CkImap_Copy $imap $msgId $isUid "Inbox.testing.c"]
    if {$success == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkMessageSet $messageSet
        exit
    }

    set i [expr $i + 1]
}

# Display the session log.
puts [CkImap_sessionLog $imap]

# Disconnect from the IMAP server.
set success [CkImap_Disconnect $imap]

delete_CkImap $imap
delete_CkMessageSet $messageSet