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

Gmail IMAP: Inspect Flags and Locate the Spam Folder

See more IMAP Examples

Inspect the current INBOX flags and discover Gmail's Spam folder through its \Junk special-use attribute. The example reads the first message's flags, if present, and makes no mailbox changes.

Client-specific Junk/NonJunk keywords are not a portable spam verdict. A mailbox's \Junk LIST attribute identifies a special folder; it is not a message flag. Use current server responses rather than a historical transcript. For Gmail label membership, see X-GM-LABELS.

First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference 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 read gmail-mail-token.json 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 "An OAuth access token is required. Authorize or refresh the account 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, token scope, expiry, and Workspace policy."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

set success [CkImap_ExamineMailbox $imap "INBOX"]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not examine INBOX."
    delete_CkJsonObject $token
    delete_CkImap $imap
    exit
}

#  Capture only the EXAMINE response, after authentication has completed.
puts [CkImap_lastResponse $imap]
#  Sample flags without marking messages read. Sequence numbers are session-local.
if {[CkImap_get_NumMessages $imap] > 0} then {
    set sequence 1
    set flags [CkImap_fetchFlags $imap $sequence 0]
    if {[CkImap_get_LastMethodSuccess $imap] != 1} then {
        CkImap_Logout $imap
        CkImap_Disconnect $imap
        puts "Could not fetch flags; the message may have changed or disappeared."
        delete_CkJsonObject $token
        delete_CkImap $imap
        exit
    }

    puts "Sequence $sequence: $flags"
}

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 folders."
    delete_CkJsonObject $token
    delete_CkImap $imap
    delete_CkMailboxes $folders
    exit
}

set i 0
while {$i < [CkMailboxes_get_Count $folders]} {
    if {[CkMailboxes_HasFlag $folders $i "\\Junk"] == 1} then {
        puts "Spam folder: [CkMailboxes_getName $folders $i]"
    }

    set i [expr $i + 1]
}
CkImap_Logout $imap
CkImap_Disconnect $imap

delete_CkJsonObject $token
delete_CkImap $imap
delete_CkMailboxes $folders