Chapter 36 / 51
Volume 5 · Extensions, Ops & Projects

Mail: the xmail family

A letter from construction to delivery and back: xmail lays the foundation (MIME parsing and building) while SMTP/POP3/IMAP field three clients that share transport and SASL.

Overview

Positioning: the capstone of protocol application layers. Prerequisite: build.bat xsmtp (or xpop3 / ximap — xmail comes along automatically). TCP/TLS discipline carries over from ch. 23/24; SASL credential discipline from ch. 44's trust layers.

36.1 Family map

Four libraries divide the work: xmail (89 symbols) is the MIME base — codecs, charsets, address/date/parameter parsing, multipart, the message tree, builders, wire format (dot-stuffing) and the shared transport; xsmtp (44) / xpop3 (48) / ximap (102) are the three protocol clients. The registry's requires carries xmail with any protocol — and in combined builds xmail compiles exactly once.

36.2 The MIME layer

Shared by send and receive: xrtMailTreeParse parses raw mail into a limits-checked message tree (defending against malicious nesting), and the xrtMailBuilder family streams multipart construction. Codecs (QP/Base64), charset-to-UTF-8, and canonical formatting of addresses and dates are standalone functions — format conversion alone needs no protocol client.

36.3 Sending via SMTP

Session-style API: xrtSmtpClientOpen connects (plaintext/STARTTLS/SSL), Capabilities negotiates, Auth runs SASL, and Mail/Rcpt/Data or BDAT chunks deliver; the higher-level xrtSmtpSubmit posts a whole letter in one step. The command and reply parsers are exposed separately — test and gateway scenarios work offline.

36.4 POP3 / IMAP retrieval

POP3 is direct: Stat/List/Uidl take inventory, Retr/Top fetch (RetrTree returns the message tree directly). IMAP is full-featured: Select picks a mailbox, BeginSearch/BeginFetch stream retrieval, BeginStore edits flags, Append archives, BeginIdle waits for push, Compress compresses the session — the data-item parsers (imap_data) are independently testable.

36.5 Contract and choosing

ConcernKey points
Ownershipclient handles are library-allocated: Close disconnects, Destroy frees; parse results are borrowed views — copy before leaving the function
CredentialsSASL secrets go through env/Custom (ch. 44 discipline), never into source
Variantsthree independently selectable protocols; scripts can probe at runtime with xsExtensionEnabled("xsmtp")
Choosingpoll-only retrieval: POP3; multi-box/search/flags/push: IMAP; sending: SMTP

Common mistakes

Carrying a parse view out of the callback
Symptom
Storing the message tree pointer from Retr and touching it later crashes.
Cause
Protocol replies are borrowed views that die on the next exchange — copy anything you keep with the MIME layer's Write/Bytes family.
Sending in the clear
Symptom
Mail and credentials travel the network unencrypted.
Cause
Security mode unconfigured — production must use STARTTLS or SSL; the connection layer comes from xrt TLS with certificate verification on by default. Don't turn it off for convenience.

Exercises

BasicParse and build

Parse a multipart sample with the MIME layer, rebuild it with the Builder, and compare bytes.

Acceptance: round-trip identical; tree depth limits enforced (over-deep samples rejected).
IntermediateLoopback send/receive

Run an SMTP session against a local test server (or debug gateway), Submit a letter, fetch it back with POP3 Retr.

Acceptance: the fetched tree matches what was sent; credentials come from the environment throughout.
ChallengeA mail routing service

Wrap send/receive as HTTP routes (POST /mail/send, GET /mail/inbox), with IMAP Idle pushing over WS.

Acceptance: full curl path; Idle pushes arrive in seconds; a slow server never blocks an engine worker.

Summary

PointConclusion
Structurexmail base + three protocol clients; requires carries it automatically
MIMElimits-checked tree parsing + streaming Builder, usable standalone
Sendingsession-style SMTP API + one-step Submit
RetrievalPOP3 polling / full-featured IMAP (search/flags/Idle/compress)
Disciplineviews are borrow-only; credentials via env; TLS on by default
Further reading on xrt
  • xrt · tls — the encryption base under mail transport