Retrieving headers from a mailbox looks trivial until a message vanishes from someone else’s view. The usual culprit is not deletion. It is the \Seen flag, set silently by a plain FETCH of BODY[HEADER] or by any client that opens the message to read it. IMAP BODY.PEEK exists to prevent exactly that side effect, and using it correctly is the difference between safe inspection and quietly marking mail as read across every tool that shares the account.
Why read status mutates during header inspection
The Internet Message Access Protocol stores message state on the server, and one of the most consequential pieces of that state is the \Seen flag. When a client issues a normal FETCH for BODY[HEADER], BODY[TEXT], or BODY[], the server is permitted to set \Seen on the message as a side effect. The same happens when a user opens a message in a graphical client. In a single-user, single-device setup this is harmless. In a shared account, it is a data integrity problem.
BODY.PEEK is a prefix that tells the server to return the requested section without changing the \Seen flag. The syntax is FETCH 1:10 (BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE)]). The server returns the header block exactly as it would for a normal fetch, but the message remains unread. That distinction matters because IMAP synchronizes folders and read states across devices. One careless fetch from a script can make a message appear read in a webmail client, a mobile app, and a triage tool simultaneously, and any workflow that filters on unread status will then skip it.
This is a subtle and frequently misdiagnosed cause of “missing email.” Consider a support inbox where a monitoring script polls headers to log new tickets, a human agent reads messages in a desktop client, and a second script archives anything older than seven days. If the monitoring script uses a plain FETCH, every message it inspects becomes read before the agent sees it. The agent’s unread filter shows nothing. The archiving script, if it keys on read status, may sweep messages that were never actually handled. The mail did not disappear. The read state was overwritten by a tool that never intended to change it.
POP3 does not have this problem in the same form, because POP3 downloads mail to a single client and the notion of server-side flags is minimal to nonexistent. That is one reason POP3 for SEO automation is common: a script pulls the inbox, processes it, and the server state is not shared with other consumers in any meaningful way. But POP3 supports inbox only, so it cannot inspect a specific folder, and it cannot preserve read state for a human who works the same account. When a shared mailbox or folder-level inspection is required, IMAP with BODY.PEEK is the correct tool.
Building a safe header fetch against the server
Start by confirming the connection parameters. IMAP uses encrypted port 993 with implicit TLS, and IMAP4rev2 is defined by RFC 9051, which supersedes the older RFC 3501 behavior while remaining backward compatible in practice. If your server offers only STARTTLS on port 143, upgrade the connection before authenticating. POP3 uses encrypted port 995, and its base protocol is RFC 1939. The protocol choice dictates the ports, and mixing them up is the most common cause of connection failures during a first attempt.
After login, select the mailbox in read-only mode where possible. The SELECT command accepts a read-only variant, EXAMINE, which prevents the server from making permanent changes to the mailbox during the session. Even with EXAMINE, some servers still set \Seen on a plain body fetch, so BODY.PEEK remains necessary. Use EXAMINE for pure inspection and SELECT only when you intend to modify flags deliberately. This two-layer defense, read-only mailbox plus PEEK, is what makes header inspection safe in a multi-tool environment.
Construct the fetch with a narrow header field list. Requesting BODY.PEEK[HEADER.FIELDS (FROM TO SUBJECT DATE MESSAGE-ID)] returns only the fields you need and avoids pulling large headers or bodies. The response arrives as a literal, a length-prefixed block that must be read in full before the next response line. Parse the literal, extract the fields, and close the connection with LOGOUT. If you need to mark a message read later, do it explicitly with STORE +FLAGS (\Seen) so the state change is intentional and logged rather than an invisible side effect of inspection.
Catch-all email domains make this discipline more important, not less. A catch-all receives mail for every address at a domain, including typos and probe traffic, so a header inspection pass over a catch-all inbox touches a high volume of messages. AllMail provides catch-all domains for projects, and when those inboxes feed multiple consumers such as a verification script, a human reviewer, and an archive job, a single non-PEEK fetch can mark hundreds of messages read in one pass. Scoping the fetch with a UID range or a SEARCH on UNSEEN keeps the blast radius small.
Choosing between IMAP and POP3 for header work
The choice comes down to whether read state is shared. If one script owns the mailbox and no human or other tool reads it, POP3 is simpler: it downloads mail to a single client, the inbox is the only folder, and there are no flags to preserve. If any other consumer reads the same account, or if you need to inspect folders other than the inbox, IMAP is required because it synchronizes folders and read states across devices. The incoming mail server protocol is not interchangeable once multiple readers exist.
For sending, SMTP handles sending mail regardless of which retrieval protocol you chose, so the incoming and outgoing decisions are independent. A common architecture pairs POP3 retrieval with SMTP sending for a single-owner automation account, and IMAP retrieval with SMTP for anything shared. The tell is whether a human ever opens the same mailbox in a client. If yes, use IMAP and use BODY.PEEK for every automated header read.
Two operational checks catch most problems before they cause missing email. First, verify after a fetch that the message still lacks \Seen by issuing FETCH on FLAGS and asserting the absence of the flag. Second, run the automation against a test mailbox with a second client open, and confirm the test client still shows the message as unread. If it shows read, a non-PEEK fetch slipped into the code path. The AllMail blog and the RFC 9051 and RFC 1939 specifications are the reference points to verify command syntax when a server behaves unexpectedly.
Treat BODY.PEEK as the default for any automated header read and reserve plain FETCH for the rare case where marking a message read is the actual goal. Pair it with read-only mailbox selection, narrow header field lists, and an explicit \Seen assertion in your test suite. That combination keeps read state under deliberate control, so a monitoring script can inspect every header it needs without hiding messages from the people and tools that share the account.