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

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

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 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
}

#  LIST special-use attributes work with localized folder names. XLIST is obsolete.
set special [new_CkJsonObject]

set i 0
while {$i < [CkMailboxes_get_Count $folders]} {
    if {[CkMailboxes_IsSelectable $folders $i] == 1} then {
        puts [CkMailboxes_getName $folders $i]  [CkMailboxes_getFlags $folders $i]
        if {[CkMailboxes_HasFlag $folders $i "\\All"] == 1} then {
            CkJsonObject_UpdateString $special "allMail" [CkMailboxes_getName $folders $i]
        }

        if {[CkMailboxes_HasFlag $folders $i "\\Sent"] == 1} then {
            CkJsonObject_UpdateString $special "sent" [CkMailboxes_getName $folders $i]
        }

        if {[CkMailboxes_HasFlag $folders $i "\\Drafts"] == 1} then {
            CkJsonObject_UpdateString $special "drafts" [CkMailboxes_getName $folders $i]
        }

        if {[CkMailboxes_HasFlag $folders $i "\\Junk"] == 1} then {
            CkJsonObject_UpdateString $special "spam" [CkMailboxes_getName $folders $i]
        }

        if {[CkMailboxes_HasFlag $folders $i "\\Trash"] == 1} then {
            CkJsonObject_UpdateString $special "trash" [CkMailboxes_getName $folders $i]
        }

        if {[CkMailboxes_HasFlag $folders $i "\\Flagged"] == 1} then {
            CkJsonObject_UpdateString $special "starred" [CkMailboxes_getName $folders $i]
        }

        if {[CkMailboxes_HasFlag $folders $i "\\Important"] == 1} then {
            CkJsonObject_UpdateString $special "important" [CkMailboxes_getName $folders $i]
        }

    }

    set i [expr $i + 1]
}
CkImap_Logout $imap
CkImap_Disconnect $imap
CkJsonObject_put_EmitCompact $special 0
puts [CkJsonObject_emit $special]
#  Missing keys mean the folder was not advertised. Do not substitute an English name.

delete_CkJsonObject $token
delete_CkImap $imap
delete_CkMailboxes $folders
delete_CkJsonObject $special