Unified, read-only public API over mail sources.
A source is created from a plain config map via open, which
dispatches on :type (:imap or :maildir).
Every source exposes the same operations — list-folders,
messages, by-id, by-ids, list-ids, by-id-range,
watch, watch-async, close — regardless of its backend.
Every source owns an explicit :folders map from logical folder
name to backend-specific location. The public API only accepts
logical folder names; passing an unknown name throws ex-info with
{:type ::unknown-folder}.
Example:
(with-source [src {:type :imap
:host "imap.example.com"
:user "me@example.com"
:password "secret"
:folders {"INBOX" "INBOX"
"Clojure" "Lists/clojure"}}]
(mailseq/messages src "INBOX" {:limit 10}))
Unified, read-only public API over mail sources.
A *source* is created from a plain config map via `open`, which
dispatches on `:type` (`:imap` or `:maildir`).
Every source exposes the same operations — `list-folders`,
`messages`, `by-id`, `by-ids`, `list-ids`, `by-id-range`,
`watch`, `watch-async`, `close` — regardless of its backend.
Every source owns an explicit `:folders` map from logical folder
name to backend-specific location. The public API only accepts
logical folder names; passing an unknown name throws `ex-info` with
`{:type ::unknown-folder}`.
Example:
(with-source [src {:type :imap
:host "imap.example.com"
:user "me@example.com"
:password "secret"
:folders {"INBOX" "INBOX"
"Clojure" "Lists/clojure"}}]
(mailseq/messages src "INBOX" {:limit 10}))(by-id src folder-name id)(by-id src folder-name id opts)Fetch a single message map (or nil) from folder-name by its
backend-specific stable id. For IMAP, id is a UID (long or its
string representation). For Maildir, id is the filename prefix
before the :2, flag suffix.
opts takes the parsing options of messages only.
Fetch a single message map (or nil) from `folder-name` by its backend-specific stable id. For IMAP, `id` is a UID (long or its string representation). For Maildir, `id` is the filename prefix before the `:2,` flag suffix. `opts` takes the parsing options of `messages` only.
(by-id-range src folder-name start-id end-id)(by-id-range src folder-name start-id end-id opts)Fetch messages whose ids fall in the range [start-id, end-id].
For IMAP, delegates to UID-range fetch — this is the most efficient
way to implement incremental fetching with a UID watermark. Pass
nil as end-id to fetch from start-id to the latest message.
For Maildir, filters list-ids by lexicographic comparison and
fetches the matching messages.
Messages come back in arrival order. opts takes the parsing
options of messages only.
Example (IMAP watermark pattern): (by-id-range src "INBOX" (str (inc last-uid)) nil)
Fetch messages whose ids fall in the range [start-id, end-id]. For IMAP, delegates to UID-range fetch — this is the most efficient way to implement incremental fetching with a UID watermark. Pass nil as `end-id` to fetch from `start-id` to the latest message. For Maildir, filters `list-ids` by lexicographic comparison and fetches the matching messages. Messages come back in arrival order. `opts` takes the parsing options of `messages` only. Example (IMAP watermark pattern): (by-id-range src "INBOX" (str (inc last-uid)) nil)
(by-ids src folder-name ids)(by-ids src folder-name ids opts)Fetch message maps for a collection of ids in a single pass.
Messages come back in arrival order, whatever the order of ids;
unknown ids are skipped. opts takes the parsing options of
messages only.
More efficient than repeated by-id calls: for Maildir, one
directory scan reads only the matching files; for IMAP, one
server round-trip fetches all requested UIDs.
Typical incremental workflow:
(list-ids src folder) → all ids(by-ids src folder new-ids) → only the new messagesFetch message maps for a collection of ids in a single pass. Messages come back in arrival order, whatever the order of `ids`; unknown ids are skipped. `opts` takes the parsing options of `messages` only. More efficient than repeated `by-id` calls: for Maildir, one directory scan reads only the matching files; for IMAP, one server round-trip fetches all requested UIDs. Typical incremental workflow: 1. `(list-ids src folder)` → all ids 2. diff against previously seen ids → new-ids 3. `(by-ids src folder new-ids)` → only the new messages
(close src)Release the resources held by src. Idempotent.
Release the resources held by `src`. Idempotent.
(list-folders src)Return the logical folder names known to src.
Return the logical folder names known to `src`.
(list-ids src folder-name)Return a vector of stable id strings for every message in
folder-name, in arrival order, without reading or parsing message
content.
For IMAP, ids are UID strings. For Maildir, ids are the filename
prefixes before the :2, flag suffix.
This is the cheapest possible operation: for Maildir it reads only
directory listings; for IMAP it fetches only UIDs. Use it to diff
against a set of already-seen ids, then call by-ids to fetch
only the new ones.
Return a vector of stable id strings for every message in `folder-name`, in arrival order, without reading or parsing message content. For IMAP, ids are UID strings. For Maildir, ids are the filename prefixes before the `:2,` flag suffix. This is the cheapest possible operation: for Maildir it reads only directory listings; for IMAP it fetches only UIDs. Use it to diff against a set of already-seen ids, then call `by-ids` to fetch only the new ones.
(messages src folder-name)(messages src folder-name opts)Fetch messages from folder-name in src.
Selection options: :since - messages sent on/after this date (Date or string) :before - messages sent strictly before this date
Date strings: see mailseq.filter/->date.
:limit - keep the last N matching messages to arrive
Messages are returned in arrival order, oldest first.
Parsing options (passed through to mailseq.parse):
:headers? - include the full header map (default: true)
:body? - parse body content (default: true)
:attachments? - include attachment byte data (default: true)
On Maildir, :since also skips files whose filename timestamp is
more than a day older; see mailseq.maildir/messages.
mailseq is read-only and intentionally exposes no substring search on subject/addressees/message-id: if you need that, filter the returned sequence yourself.
Returns a vector of parsed message maps.
Fetch messages from `folder-name` in `src`. Selection options: :since - messages sent on/after this date (Date or string) :before - messages sent strictly before this date Date strings: see `mailseq.filter/->date`. :limit - keep the last N matching messages to arrive Messages are returned in arrival order, oldest first. Parsing options (passed through to `mailseq.parse`): :headers? - include the full header map (default: true) :body? - parse body content (default: true) :attachments? - include attachment byte data (default: true) On Maildir, `:since` also skips files whose filename timestamp is more than a day older; see `mailseq.maildir/messages`. mailseq is read-only and intentionally exposes no substring search on subject/addressees/message-id: if you need that, filter the returned sequence yourself. Returns a vector of parsed message maps.
(open cfg)Open a mail source from a config map. Returns a record implementing
mailseq.source/MailSource and java.io.Closeable.
The config must contain a :type key (:imap or :maildir) and a
:folders map from logical folder name to backend location.
Open a mail source from a config map. Returns a record implementing `mailseq.source/MailSource` and `java.io.Closeable`. The config must contain a `:type` key (`:imap` or `:maildir`) and a `:folders` map from logical folder name to backend location.
(uid-validity src folder-name)Return the UIDVALIDITY of the given folder as a long, or nil for non-IMAP sources.
UIDVALIDITY is an IMAP concept (RFC 3501): the server assigns a value per mailbox that changes when the UID sequence is no longer valid (mailbox recreation, server migration). Callers persisting UIDs as watermarks should persist the UIDVALIDITY seen at write time and discard the watermark when it no longer matches — otherwise a silent mismatch can cause every subsequent fetch to return nothing.
Example: (let [uv-now (uid-validity src "INBOX") uv-stored (read-stored-uv)] (when (not= uv-now uv-stored) ;; UIDVALIDITY changed — reset the watermark before fetching. ))
Return the UIDVALIDITY of the given folder as a long, or nil for
non-IMAP sources.
UIDVALIDITY is an IMAP concept (RFC 3501): the server assigns a value
per mailbox that changes when the UID sequence is no longer valid
(mailbox recreation, server migration). Callers persisting UIDs as
watermarks should persist the UIDVALIDITY seen at write time and
discard the watermark when it no longer matches — otherwise a silent
mismatch can cause every subsequent fetch to return nothing.
Example:
(let [uv-now (uid-validity src "INBOX")
uv-stored (read-stored-uv)]
(when (not= uv-now uv-stored)
;; UIDVALIDITY changed — reset the watermark before fetching.
))(underlying-conn src)Return the underlying IMAP connection map from an IMAP source, or nil for non-IMAP sources.
The returned map contains :store (a jakarta.mail.Store) and
:session (a jakarta.mail.Session), suitable for passing to
low-level functions in mailseq.imap.fetch, mailseq.imap.idle,
and mailseq.imap.folder.
This is a stable part of the public API — use it when you need IMAP-specific operations that the unified API does not cover.
Return the underlying IMAP connection map from an IMAP source, or nil for non-IMAP sources. The returned map contains `:store` (a `jakarta.mail.Store`) and `:session` (a `jakarta.mail.Session`), suitable for passing to low-level functions in `mailseq.imap.fetch`, `mailseq.imap.idle`, and `mailseq.imap.folder`. This is a stable part of the public API — use it when you need IMAP-specific operations that the unified API does not cover.
(watch src folder-name on-message)(watch src folder-name on-message opts)Watch folder-name for new messages, calling on-message with each
parsed message map. Blocks the current thread.
For IMAP, delegates to IMAP IDLE. For Maildir, delegates to
java.nio.file.WatchService.
Options (passed through to the backend): :parse-opts - options for message->map (default: {}) :on-error - function called with Exception on errors :heartbeat-ms - IMAP only: interval between NOOP heartbeats (default: 1200000) :settle-ms - Maildir only: delay after file creation (default: 50)
Returns nil when the watch ends: when src is closed, or on Maildir
when the thread is interrupted. watch-async returns a stop object
instead.
Watch `folder-name` for new messages, calling `on-message` with each
parsed message map. Blocks the current thread.
For IMAP, delegates to IMAP IDLE. For Maildir, delegates to
`java.nio.file.WatchService`.
Options (passed through to the backend):
:parse-opts - options for message->map (default: {})
:on-error - function called with Exception on errors
:heartbeat-ms - IMAP only: interval between NOOP heartbeats (default: 1200000)
:settle-ms - Maildir only: delay after file creation (default: 50)
Returns nil when the watch ends: when `src` is closed, or on Maildir
when the thread is interrupted. `watch-async` returns a stop object
instead.(watch-async src folder-name on-message)(watch-async src folder-name on-message opts)Like watch, in a new daemon thread. Returns a java.io.Closeable:
.close stops the watch and waits up to 5 s for the thread.
Example: (def w (watch-async src "INBOX" (fn [msg] (println (:subject msg))))) ;; Later: (.close w)
Like `watch`, in a new daemon thread. Returns a `java.io.Closeable`: `.close` stops the watch and waits up to 5 s for the thread. Example: (def w (watch-async src "INBOX" (fn [msg] (println (:subject msg))))) ;; Later: (.close w)
(with-source [sym cfg] & body)Bind sym to (open cfg) for the extent of body, ensuring
close runs on exit.
Bind `sym` to `(open cfg)` for the extent of `body`, ensuring `close` runs on exit.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |