Intro
This version is currently published under the next dist-tag on npm:
npm install cf-imap@next
# or with bun
bun add cf-imap@next
It will be promoted to latest once the stable 1.0.0 release ships.
cf-imap is an IMAPv4 library for use within the Cloudflare Workers platform.
This package was made because all of the other IMAP libraries are built for NodeJS, and Cloudflare Workers even with node compatibility turned does not support them.
Provider support
Most providers stick to the two IMAP specifications (RFC 3501 and RFC 9051), however some have their own experimental implementations.
Gmail and Outlook require OAuth2 — both providers (and Microsoft 365, Yahoo, and others) no longer accept passwords over IMAP. cf-imap supports them via the XOAUTH2 SASL mechanism: pass an OAuth 2.0 access token with auth.accessToken (or auth.getAccessToken) and connect() handles the rest — see Initialisation → OAuth 2.0. You must obtain and refresh the token yourself (the library does not run the OAuth flow).
However, if the provider you are using isn't tested, uses their own implementation of IMAP and cf-imap is throwing errors on things that should work (see: Errors), please raise an issue on GitHub so I can promptly fix it.
Features
- Connect to an IMAP server
- Get namespaces
- Get folders
- Select a folder
- Examine a folder read-only
- Close / unselect a folder
- Fetch emails (with MIME parsing, attachments & charset decoding)
- Search emails
- Store flags
- Expunge emails
- Copy emails
- Move emails
- Status of folder
- Append email
- IDLE (push updates)
- Check (IMAP4rev1 servers only)
- Capability / NOOP / ENABLE
- Logout
- Rename folder
- Delete folder
- Create folder
RFC 9051 (IMAP4rev2) compliance
cf-imap is written against the current IMAP specification, RFC 9051 (IMAP4rev2), while staying compatible with the widely deployed IMAP4rev1 (RFC 3501):
- TLS — Implicit TLS on port 993 (RFC 8314) and opportunistic TLS via
STARTTLSon other ports (RFC 9051 §6.2.1). - Authentication —
AUTHENTICATE PLAINwith SASL-IR preferred;LOGINonly as a last resort and never withLOGINDISABLED;AUTHENTICATE XOAUTH2(OAuth 2.0 access tokens) for providers that require OAuth2 (RFC 9051 §6.2.2/§6.2.3, RFC 7628-style). - IMAP4rev2 negotiation —
ENABLE IMAP4rev2is issued automatically on dual-version servers (RFC 9051 Appendix A). - Search — both
* SEARCH(rev1) and* ESEARCH(rev2) responses are parsed (RFC 9051 §6.4.4). - Mailbox names — UTF-8 (Net-Unicode) on rev2 sessions; automatic modified UTF-7 (RFC 2152) conversion on rev1-only servers.
- UID tracking —
COPYUID(copy/move) andAPPENDUID(append) response codes are surfaced. - Commands removed in RFC 9051 (
CHECK,\Recent-based search keys) are handled gracefully.
Breaking changes from v0.x.x
The v1.0.0 rewrite focused on protocol correctness: a tagged command pipeline with a literal-aware response reader, robust parsing, proper error handling, and full MIME support. As a result some public APIs changed:
- The
Emailtype was reworked —from/to/ccare nowstring[],datewas renamed tointernalDate, andbodyis now an object ({ text?, html?, raw }) plus a newattachmentsarray. fetchEmailsprops changed —folderis now optional (falls back to the selected folder),fetchBodydefaults totrue, and a newuseUidoption was added.getNamespacesnow returns a typed object ({ personal, other, shared }) instead ofstring[].selectFoldernow returns a fully typedMailboxInfoinstead of a dynamic object.expungenow takes an options object ({ range?, useUid? }), andUID EXPUNGErequires a range.copy()andmove()now return aCopyUidInfo(ornull), andappend()returns anAppendResult(ornull).- New commands:
examine(),idle(),closeMailbox(),unselect(),capability(),noop(),enable(). - New authentication mode:
authnow also accepts an OAuth 2.0 access token (auth.accessToken) or a token callback (auth.getAccessToken) forXOAUTH2— see Initialisation → OAuth 2.0. check()only works on IMAP4rev1 servers — it throws on IMAP4rev2 sessions.- The raw socket, writer, reader, encoder & decoder are no longer public (see Extendability).