Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-mail-token.json")
if not success then
    print("Could not load the protocol token from the private working directory.")
    return
end

if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
    print("Authorize or refresh the mailbox first.")
    return
end

local mailbox = "user@example.com"
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
imap.require_ssl_cert_verify = true
imap.auth_method = "XOAUTH2"
success = imap:connect("imap.gmail.com")
if not success then
    print("Could not establish the Gmail IMAP TLS connection.")
    return
end

success = imap:login(mailbox, token:string_of("access_token"))
if not success then
    imap:disconnect()
    print("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
    return
end

local folders = chilkat.Mailboxes.new()
success = imap:mbx_list(false, "", "*", folders)
if not success then
    imap:logout()
    imap:disconnect()
    print("Could not list Gmail folders.")
    return
end

-- LIST special-use attributes work with localized folder names. XLIST is obsolete.
local special = chilkat.JsonObject.new()
local i = 0
while i < folders.count do
    if folders:is_selectable(i) then
        print(folders:get_name(i) .. "  " .. folders:get_flags(i))
        if folders:has_flag(i, "\\All") then
            special:update_string("allMail", folders:get_name(i))
        end

        if folders:has_flag(i, "\\Sent") then
            special:update_string("sent", folders:get_name(i))
        end

        if folders:has_flag(i, "\\Drafts") then
            special:update_string("drafts", folders:get_name(i))
        end

        if folders:has_flag(i, "\\Junk") then
            special:update_string("spam", folders:get_name(i))
        end

        if folders:has_flag(i, "\\Trash") then
            special:update_string("trash", folders:get_name(i))
        end

        if folders:has_flag(i, "\\Flagged") then
            special:update_string("starred", folders:get_name(i))
        end

        if folders:has_flag(i, "\\Important") then
            special:update_string("important", folders:get_name(i))
        end
    end

    i = i + 1
end

imap:logout()
imap:disconnect()
special.emit_compact = false
print(special:emit())
-- Missing keys mean the folder was not advertised. Do not substitute an English name.