UID FETCH * (FLAGS) imap example

The UID FETCH * (FLAGS) command is used in IMAP to retrieve the flags (such as \Seen, \Answered) for the last message in the currently selected mailbox. 

Command Breakdown

UID: Tells the server to use Unique Identifiers (UIDs) instead of relative message sequence numbers.
FETCH: The base command used to retrieve message data.
*: A wildcard symbol representing the largest UID currently in the mailbox (the most recent message).
(FLAGS): Specifies that you only want the message's flag data. 

Example Interaction

In a typical IMAP session, the interaction would look like this:

C: Client request
imap
A1 UID FETCH * (FLAGS) 


S: Server response
imap
* 558 FETCH (UID 1024 FLAGS (\Seen \Answered))
A1 OK FETCH completed. 


558: The sequence number of the message.
UID 1024: The actual unique ID of that message.
FLAGS (\Seen \Answered): The specific flags currently set on that message. 

Key Variations

Range Fetch: To get flags for all messages from UID 1000 to the end, use UID FETCH 1000:* (FLAGS).
Fetch All: To get flags for every message in the mailbox, you would typically use
UID FETCH 1:* (FLAGS).
Multiple Data Items: You can combine requests, such as 
UID FETCH * (FLAGS INTERNALDATE),
to get both the flags and the server's receipt timestamp.