Sample code for 30+ languages & platforms
Lua

Fetch the Flags Set on an IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchFlags method, which returns the flags currently set on a single message as a space-separated string. The first argument is the message ID and the second (bUid) selects UID vs sequence number. This example fetches and prints the flags for one message identified by UID.

Background: The returned string is the server's current flag list for the message, such as \Seen \Flagged. This is a quick way to check a single message's state — for example, to decide whether it still needs processing — without downloading the message itself. To read the same information from a message you already have as an Email object, use GetMailFlag instead.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Imap.FetchFlags method, which returns the flags currently set on a single
-- message as a space-separated string.  The 1st argument is the message ID and the 2nd (bUid)
-- selects UID vs sequence number.

local imap = chilkat.Imap.new()

imap.ssl = true
imap.port = 993

local success = imap:connect("imap.example.com")
if not success then
    print(imap.last_error_text)
    return
end

success = imap:login("user@example.com", "myPassword")
if not success then
    print(imap.last_error_text)
    return
end

success = imap:select_mailbox("Inbox")
if not success then
    print(imap.last_error_text)
    return
end

local msgSet = chilkat.MessageSet.new()
success = imap:query_mbx("ALL", true, msgSet)
if not success then
    print(imap.last_error_text)
    return
end

if msgSet.count > 0 then
    local uid = msgSet:get_id(0)
    -- FetchFlags returns a string, so assign it to a string variable and check for success.
    local flags = imap:fetch_flags(uid, true)
    if not imap.last_method_success then
        print(imap.last_error_text)
        return
    end

    print("UID " .. uid .. " flags: " .. flags)
end

success = imap:disconnect()
if not success then
    print(imap.last_error_text)
    return
end