What is IMAP?

IMAP, or Internet Message Access Protocol RFC3501, is a protocol used to retrieve and manipulate email messages on a mail server. IMAP allows users to access and manage their emails from multiple devices simultaneously. This means you can check your email on your computer, smartphone, or tablet, and any changes you make will be reflected across all devices.

IMAP operates on a client-server model. When you use an email client, it acts as a client, communicating with an IMAP server. The server stores your emails and allows the client to access and manipulate them.

The Dawn of Modern Email

Before IMAP, POP3 was the dominant protocol for retrieving emails. While POP3 served its purpose, it had a fundamental limitation: it downloaded emails to the local machine, making them accessible only from that device. This led to synchronization issues and the inability to access emails from multiple devices.

IMAP, introduced in the early 1990s, changed the game. It allowed users to access their emails from any device with an internet connection. By storing emails on the server, IMAP enabled seamless synchronization across multiple devices.

Early Adopters: Pine and Mutt

Early email clients like Pine and Mutt were pioneers in implementing IMAP. These text-based clients offered a powerful and flexible way to manage email, but their steep learning curve limited their appeal to a niche audience of tech-savvy users.

Core Concepts

The IMAP protocol is primarily line-oriented. This means that commands and responses are exchanged between the client and server in text-based lines. Each line typically contains a specific command or response, making the protocol relatively simple to parse and implement. The command/response exchange typically occurs over a long-running session per user-agent (there is no asynchronous do-this-and-wake-me-when-it’s-done functionality). A client will connect, authenticate, and select a particular mailbox to issue commands against.

  • Centralized Storage: IMAP leverages a centralized server to store emails, allowing clients to access and manipulate them remotely. This differs from POP3, which downloads emails to the client device.
  • Session-Based Communication: IMAP operates through long-running sessions between clients and servers. This enables efficient communication and allows for complex operations like searching and filtering.
  • Unique Identifiers (UIDs): IMAP assigns unique identifiers to each message, facilitating efficient management and synchronization across multiple devices. However, UIDs can change between sessions, necessitating careful handling by clients.
  • Flexible Data Model: IMAP’s data model supports hierarchical folder structures, enabling users to organize their emails effectively. Additionally, it provides metadata attributes for searching, filtering, and sorting.
  • A Retrieval Protocol, Not a Sending Protocol: While IMAP is a powerful protocol for retrieving and managing emails, it’s important to note that it doesn’t provide any capabilities for sending emails. IMAP is designed solely for accessing and manipulating emails stored on a server.

Key IMAP Commands

The CAPABILITY command is a crucial part of the IMAP protocol, allowing clients to determine the specific features and extensions supported by an IMAP server. When a client connects to an IMAP server, it typically sends a CAPABILITY command to inquire about the server’s capabilities.

The server responds with a list of capabilities, each represented by a keyword. These keywords indicate the specific features and extensions that the server supports, such as:

  • Authentication mechanisms: The server may support various authentication methods, including plain text passwords, SASL mechanisms (e.g., CRAM-MD5, DIGEST-MD5), and OAuth.
  • Message manipulation: The server may support features like searching, sorting, filtering, and flagging messages.
  • Folder management: The server may support creating, deleting, and renaming folders, as well as subscribing to and unsubscribing from folders.
  • Extensions: The server may support various extensions, such as IDLE for real-time notifications, QUOTA for mailbox size limits, and CONDSTORE for efficient message updates.

By understanding the server’s capabilities, the client can tailor its behavior and offer a more feature-rich and efficient user experience. For example, if the server supports the IDLE extension, the client can implement real-time notifications for new incoming messages.

IMAP Commands:

  • Connection Management: LOGIN, AUTHENTICATE, STARTTLS, CLOSE
  • Folder Management: LIST, CREATE, DELETE, RENAME, SUBSCRIBE, UNSUBSCRIBE
  • Message Retrieval and Manipulation: FETCH, STORE, SEARCH, DELETE, EXPUNGE

See RFC3501 for more information

LIST

This command lists all folders/mailboxes that you are entitled to list on the server, whether it be your own folders, another user’s, or publicly available folders.

This command does take two possible arguments. The first (known as the “reference name”) indicates under what folder hierarchy you’d like to limit the list to. The second argument (known as the “mailbox name”) can contain wildcards to match names under the provided hierarchy.

Here is an example from our IMAP 101 post that lists all the folders under the users INBOX:

A1 list "INBOX/" "*"
* LIST (HasNoChildren) "/" INBOX/some_other_folder
* LIST (HasNoChildren UnMarked Archive) "/" INBOX/Archive
* LIST (HasNoChildren UnMarked Sent) "/" INBOX/Sent
* LIST (HasNoChildren Marked Trash) "/" INBOX/Trash
* LIST (HasNoChildren Marked Junk) "/" INBOX/Spam
* LIST (HasNoChildren UnMarked Drafts) "/" INBOX/Drafts
A1 OK List completed (0.000 + 0.000 secs).
UID

This is an interesting command, in that it is a modifier to other commands (namely COPY, FETCH, STORE and SEARCH). It instructs the server to use UIDs as arguments or results, rather than message sequence numbers (as is the default).

A sequence number references a message in that specific IMAP session, and the sequence numbers of messages may change between sessions, whereas UIDs remain constant for every message whilst the UIDVALIDITY flag for a folder remains unchanged.

w UID STORE 10 +FLAGS (a-funny-flag)
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft some-flag a-different-flag a-funny-flag)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft some-flag a-different-flag a-funny-flag \*)] Flags permitted.
* 3 FETCH (UID 10 FLAGS (a-funny-flag))
w OK Store completed (0.004 + 0.000 + 0.003 secs).
SELECT

This command instructs the server that the client now wishes to select a particular mailbox or folder, and any commands that relate to a folder should assume this folder as the target of that command.

g21 SELECT "INBOX"
* FLAGS (Answered Flagged Deleted Seen Draft)
* OK [PERMANENTFLAGS (Answered Flagged Deleted Seen Draft *)] Flags permitted.
* 4 EXISTS
* 0 RECENT
* OK [UNSEEN 2] First unseen.
* OK [UIDVALIDITY 1536750617] UIDs valid
* OK [UIDNEXT 9] Predicted next UID
* OK [HIGHESTMODSEQ 11] Highest
g21 OK [READ-WRITE] Select completed (0.000 + 0.000 secs).
EXAMINE

This command does the exact same thing as SELECT, except that it selects the folder in read-only mode, meaning that no changes can be effected on the folder.

A932 EXAMINE blurdybloop
* 17 EXISTS
* 2 RECENT
* OK [UNSEEN 8] Message 8 is first unseen
* OK [UIDVALIDITY 3857529045] UIDs valid
* OK [UIDNEXT 4392] Predicted next UID
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS ()] No permanent flags permitted
A932 OK [READ-ONLY] EXAMINE completed
EXPUNGE

This command instructs the server to permanently delete messages that have the \Deleted flag set on them from the currently selected folder.

Note: This does not mean “move to trash”. It means to really, properly, and finally delete.

d store 2 +FLAGS (Deleted)
* 2 FETCH (FLAGS (Deleted Seen))
d OK Store completed (0.001 + 0.000 secs).
e expunge
* 2 EXPUNGE
e OK Expunge completed.

The search command is a powerful command. See RFC3501 for details.

In a nutshell, you can provide various search terms to the server and any messages that match, in the currently selected folder, will be returned. One limitation of the command is that when multiple search terms are specified, they are considered “AND” terms by default, but you can select only two terms to “OR”, and there is no complex logic grouping (i.e. bracketing of terms) – so it’s rather simple in that regard.

FETCH

This command allows a client to fetch messages from a folder, and it is one of the most widely used IMAP commands. See RFC3501 for details.

You may specify which messages you wish to fetch, and even which messages parts; you do not have to fetch the entire message. This one command is probably the “killer feature” of IMAP, one of the main reasons it exists; it allows clients to interact in a sophisticated way with the IMAP server.

UID Instability: A Synchronization Challenge

IMAP relies on Unique Identifiers (UIDs) for messages. However, these UIDs can change between sessions, causing a major headache for synchronization.

Imagine a user reads a message on their phone and wants to mark it as read on their computer. The phone’s UID for this message might no longer be valid on the computer! Developers must not only track new/updated messages but also detect UID resets and rebuild their entire ID database. This instability is why some providers offer alternative, global ID schemes.

IMAP partially addresses this with the UIDVALIDITY value, which indicates when a folder’s UIDs have been reset. Clients must monitor this to ensure accurate synchronization. Essentially, UID instability adds another layer of complexity to the process.

To efficiently synchronize changes between the client and server, IMAP provides extensions like CONDSTORE and QRESYNC. These extensions allow clients to track changes in mailboxes using modification sequences, reducing the need to re-download entire mailboxes.

Email Threading: A Complex Challenge

While IMAP provides a robust framework for email management, it doesn’t have a standardized way to directly identify email threads. Email clients typically rely on message headers like Message-ID, In-Reply-To, and References to infer relationships between messages.

Some IMAP servers, especially those from major providers, offer server-side threading capabilities. This can improve performance and consistency, but it’s not universally available.

Storing Deleted and Purging Expunged Messages

When a user deletes a message in an IMAP client, they’re typically performing a soft delete. This means the message is marked with the DELETED flag and moved to a “Trash” or “Deleted” folder. The actual deletion is done using the STORE command.

To permanently remove these deleted messages, the client can use the EXPUNGE command. However, it’s important to note that the CLOSE command also implicitly expunges messages, so developers should be mindful of this behavior.

Whether the server retains a copy of expunged messages is left up to the server implementation.

IMAP doesn’t mandate a specific “Trash” or “Deleted” folder. Servers handle this differently. Some treat it as a virtual folder, displaying messages marked as deleted.

Notifications: A Balancing Act

IMAP offers a few mechanisms for receiving notifications about new messages. However, the optimal approach depends on the specific use case and the scale of the operation.

Polling: The simplest method is polling, where the client periodically checks the server for new messages. While effective for small-scale operations, it can be resource-intensive for large-scale deployments with thousands of mailboxes. IMAP extensions like CONDSTORE and QRESYNC allow clients to track changes in mailboxes in a more optimal way.

IMAP Extensions:

  • IDLE: This extension allows the server to “push” notifications to the client in real-time. However, it requires a persistent connection, which can be resource-intensive for large-scale deployments.
  • NOTIFY: A more efficient alternative to IDLE, NOTIFY enables clients to subscribe to specific folders and events, reducing the overhead of maintaining multiple persistent connections.

While IDLE and NOTIFY are powerful tools for receiving real-time notifications from IMAP servers, they present significant challenges when managing thousands of mailboxes. Maintaining a persistent connection for each mailbox can consume significant system resources, including network bandwidth, CPU, and memory. This can lead to scalability issues and performance degradation, especially under heavy load.

Conclusion

IMAP, while a powerful protocol for email management, presents a complex set of challenges for developers integrating it into modern applications. The dynamic nature of UIDs, the lack of standardized threading, and the resource-intensive nature of real-time notifications through IDLE and NOTIFY make it a complex beast to tame.

Developers must carefully consider these challenges and employ strategies such as efficient synchronization mechanisms, intelligent polling, and strategic use of IMAP extensions to build robust and scalable email applications. As the email landscape continues to evolve, understanding the intricacies of IMAP remains essential for building modern, user-friendly email experiences.