Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.1.0+

Gmail IMAP: Archive a Message or Move It to Trash

See more GMail SMTP/IMAP/POP Examples

This example changes one message in INBOX. Choose archive to move it to the discovered All Mail folder, or trash to move it to the discovered Trash folder. It resolves a decimal Gmail message ID to a mailbox UID and uses MoveMessages with the server's MOVE capability.

Archiving removes a message from INBOX while retaining it in All Mail. Moving it to Trash is a different action; Gmail normally permanently deletes messages after 30 days there. This example does not empty Trash or permanently delete mail. A message can retain other custom labels when archived.

Folder names are discovered by special-use attributes. Avoid a generic delete/EXPUNGE sequence whose Gmail outcome can depend on account settings and which can affect unrelated messages. If a connection fails during a move, inspect the source and destination before retrying. A concurrent move or deletion can make the UID disappear.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token [new_CkJsonObject]

set success [CkJsonObject_LoadFile $token "gmail-mail-token.json"]
if {$success != 1} then {
    puts "Could not load the protocol token from the private working directory."
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "Authorize or refresh the mailbox first."
    delete_CkJsonObject $token
    exit
}

set mailbox "user@example.com"
set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
CkImap_put_RequireSslCertVerify $imap 1
CkImap_put_AuthMethod $imap "XOAUTH2"
set success [CkImap_Connect $imap "imap.gmail.com"]
if {$success != 1} then {
    puts "Could not establish the Gmail IMAP TLS connection."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap $mailbox [CkJsonObject_stringOf $token "access_token"]]
if {$success != 1} then {
    CkImap_Disconnect $imap
    puts "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

set capabilities [CkImap_capability $imap]
if {expr [[CkImap_get_LastMethodSuccess $imap] != 1]  ||  [[CkImap_HasCapability $imap "X-GM-EXT-1" $capabilities] != 1]} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Gmail IMAP extensions are unavailable."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

#  Choose archive or trash. This example changes one message's location.
set action "archive"
if {expr [$action != "archive"]  &&  [$action != "trash"]} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Choose archive or trash."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

if {[CkImap_HasCapability $imap "MOVE" $capabilities] != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "The server must support IMAP MOVE."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

set folders [new_CkMailboxes]

set success [CkImap_MbxList $imap 0 "" "*" $folders]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not list Gmail folders."
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    exit
}

set role "\\All"
if {$action == "trash"} then {
    set role "\\Trash"
}

set destination ""
set count 0
set i 0
while {$i < [CkMailboxes_get_Count $folders]} {
    if {expr [[CkMailboxes_HasFlag $folders $i $role] == 1]  &&  [[CkMailboxes_IsSelectable $folders $i] == 1]} then {
        set destination [CkMailboxes_getName $folders $i]
        set count [expr $count + 1]
    }

    set i [expr $i + 1]
}
if {$count != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not identify the destination folder uniquely."
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    exit
}

set success [CkImap_SelectMailbox $imap "INBOX"]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not open INBOX for the move."
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    exit
}

#  Obtain this decimal string from the Gmail message/thread ID example.
set gmailMessageId "1278455344230334865"
set idText [new_CkStringBuilder]

CkStringBuilder_Append $idText $gmailMessageId
set validation [new_CkJsonObject]

set matches [CkStringBuilder_RegexMatch $idText "\\A[1-9][0-9]{0,19}\\z" $validation 1000]
if {$matches != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Expected a decimal Gmail message ID string."
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    delete_CkStringBuilder $idText
    delete_CkJsonObject $validation
    exit
}

set target [new_CkMessageSet]

set joined1 [new_CkStringBuilder]

CkStringBuilder_Append $joined1 "X-GM-MSGID "
CkStringBuilder_Append $joined1 $gmailMessageId
set success [CkImap_QueryMbx $imap [CkStringBuilder_getAsString $joined1] 1 $target]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not find the Gmail message in the selected mailbox."
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    delete_CkStringBuilder $idText
    delete_CkJsonObject $validation
    delete_CkMessageSet $target
    delete_CkStringBuilder $joined1
    exit
}

if {[CkMessageSet_get_Count $target] != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Expected one message in the selected mailbox. Matches: [CkMessageSet_get_Count $target]"
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    delete_CkStringBuilder $idText
    delete_CkJsonObject $validation
    delete_CkMessageSet $target
    delete_CkStringBuilder $joined1
    exit
}

set uid [CkMessageSet_toCompactString $target]
#  MoveMessages uses UID MOVE and maintains the Imap object's mailbox state.
#  No broad EXPUNGE is issued, and unrelated deleted flags are not committed.
set success [CkImap_MoveMessages $imap $target $destination]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Move failed; verify the mailbox state before retrying after a connection loss."
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    delete_CkStringBuilder $idText
    delete_CkJsonObject $validation
    delete_CkMessageSet $target
    delete_CkStringBuilder $joined1
    exit
}

puts "Completed $action for Gmail message $gmailMessageId"
CkImap_Logout $imap
CkImap_Disconnect $imap

delete_CkJsonObject $token
delete_CkImap $imap
delete_CkMailboxes $folders
delete_CkStringBuilder $idText
delete_CkJsonObject $validation
delete_CkMessageSet $target
delete_CkStringBuilder $joined1