MoonMail

the guide · ten steps

Start with a letter.

One message in, one message out, then a signature, a login, a socket you own, and the servers to point it at. Every step shows what you type and what comes back.

10steps 1dependency 0sockets to parse
before you start

Each step is one letter on the desk: what you type, then what comes back. Every step but the last works on bytes you already have; only the servers at the end want a socket.

01

Add one dependency

The root package carries the message model. Protocol clients are sub-packages of the same module, so one entry is enough; moon add writes it for you.

shell
moon add moonbitstack/moonmail

That writes one entry. The protocol clients are sub-packages of the same module, so the manifest needs nothing else:

moon.mod.json
{
  "deps": {
    "moonbitstack/moonmail": "0.2.2"
  }
}

Then import the piece you need. Nothing is registered globally, so the compiler keeps you honest about which package a name came from.

moon.pkg.json
{
  "import": [
    "moonbitstack/moonmail",
    "moonbitstack/moonmail/smtp"
  ]
}
02

Read a letter

A .eml file is bytes. Message::parse turns them into headers and a tree of parts, undoing transfer encodings as it goes. The readers then hand back typed values, not strings you have to unpick.

moonbit
let message = @moonmail.Message::parse(raw[:])

println(message.subject().unwrap_or("(no subject)"))
println(message.message_id().unwrap_or("(no id)"))
println(message.text().unwrap_or(""))
println(@debug.to_string(message.from()))
out
Welcome aboard
<8f2c41@example.com>
Thanks for signing up — your first letter is inside.
[{ name: Some("Leo"), address: "leo@example.com" }]

Parsing is lenient by default: a boundary that never closes ends the parts instead of failing. Pass strict=true when you would rather see the error than a repaired letter.

03

Look inside

A part is a Message too, so walking the tree is a four-line recursion. Body has three cases and nothing else: a leaf, a multipart, or an embedded letter.

moonbit
fn walk(part : @moonmail.Message, depth : Int) -> Unit {
  let pad = "  ".repeat(depth)
  println("\{pad}\{part.content_type().essence}")
  match part.body {
    Leaf(bytes) => println("\{pad}  \{bytes.length()} bytes")
    Multipart(parts) => parts.each(fn(p) { walk(p, depth + 1) })
    Embedded(inner) => walk(inner, depth + 1)
  }
}

walk(message, 0)
out
multipart/mixed
  multipart/alternative
    text/plain
      54 bytes
    text/html
      61 bytes
  image/jpeg
    3 bytes

When you do not want to walk it yourself, message.attachments() collects every leaf that is not the body, already decoded and named.

04

Write one back

Message::new picks the nesting for you: plain text alone stays flat, text with HTML becomes alternative, and anything with an attachment becomes mixed. You name the parts, not the tree.

moonbit
let reply = @moonmail.Message::new(
  from=@moonmail.Mailbox::new("leo@example.com", name="Leo"),
  to=[Mailbox(@moonmail.Mailbox::new("you@example.com"))],
  subject="Re: Welcome aboard",
  text="Thanks — the lake photo was mine.",
  attachments=[
    @moonmail.Attachment::new("lake.jpg", lake, media_type="image/jpeg"),
  ],
)

let wire = reply.encode()
out
wire · 520 bytes
multipart/mixed
  text/plain
    35 bytes
  image/jpeg
    3 bytes

Encoding folds at 78 columns and writes CRLF. Pass utf8=true to write non-ASCII headers in place (RFC 6532) instead of as encoded words.

05

Sign it

DKIM signs a set of headers and a hash of the body. sign returns one finished DKIM-Signature line; you put it in front of the message you already have, untouched.

moonbit
let key = @ed25519.PrivateKey::of_secret(seed[:])

let signature = @dkim.sign(
  raw[:],
  Ed25519(key),
  domain="example.com",
  selector="moon",
)

let signed = @utf8.encode(signature + "\r\n" + @utf8.decode_lossy(raw[:]))

let verdicts = @dkim.verify(
  signed[:],
  name => {
    if name == "moon._domainkey.example.com" {
      Some(record)
    } else {
      None
    }
  },
  now=0,
)
out
[Pass(domain="example.com", selector="moon")]

Change one character of the body and the same call returns Fail; keep the body but rewrite a signed header and it fails too. lookup is where you do the DNS — this package never reaches for a resolver.

06

Log in

A mechanism is a value, not a global. Build the one you want and hand it to whichever client you are driving — the same object works on SMTP, IMAP and POP3.

moonbit
let mechanism = @sasl.ScramSha256::new(
  "leo@example.com",
  password,
  nonce="f3a91c7d2b",
)

// when the server offers nothing better
let plain = @sasl.Plain::new("leo@example.com", password)

println(mechanism.name())

SCRAM needs a nonce and this package will not invent one: randomness is your call, so a test can pin it. Everything else — the three round trips, the server signature check — is inside the mechanism.

one trap

Run passwords through @sasl.prep before you build a mechanism. It normalises the string and refuses the characters the spec forbids, which is exactly what a server in another locale will do.

07

Send it

The client is a state machine: you queue commands, and next_write hands back bytes only when the protocol allows them. With no PIPELINING announced that is strict turn-taking; with it, a whole envelope goes out at once.

moonbit
let client = @smtp.Client::new()
ignore(client.feed(b"220 mail.example.com ESMTP\r\n"))
client.ehlo("laptop.local")
ignore(client.next_write())
ignore(client.feed(b"250-mail.example.com\r\n250-8BITMIME\r\n250 SIZE 10240000\r\n"))

client.send("leo@example.com", ["you@example.com"], wire)
out
MAIL FROM:<leo@example.com> SIZE=208 BODY=8BITMIME
RCPT TO:<you@example.com>
DATA

send is the shortcut: it fills in SIZE, BODY=8BITMIME and, when the payload needs it, SMTPUTF8 — but only for extensions the server actually announced at EHLO.

08

Drive it yourself

Every protocol package speaks the same three-call contract — feed, next_write, and a way to see what is still pending. That is why a test needs no socket: you feed recorded bytes and assert on the bytes it wants to write back.

moonbit
let client = @smtp.Client::new()
ignore(client.feed(b"220 mail.example.com ESMTP\r\n"))
client.ehlo("test.local")

match client.next_write() {
  Some(out) => println(@utf8.decode_lossy(out))
  None => ()
}
println(client.pending())
out
EHLO test.local
1

IMAP adds tags — every command returns its number and every Done carries it back. POP3 adds pending, which is all the back-pressure that protocol needs. moonmail/net is the only package that owns a socket; everything above it is pure.

09

When there is no socket to open

A page cannot open a TCP connection — no Web API does it — so SMTP, IMAP and POP3 are out of reach of the tab a letter is written in. JMAP is the way round: the same three jobs as JSON documents over HTTPS, which a page fetches like any other resource.

The package is the whole of RFC 8620 and RFC 8621: the five standard shapes — get, changes, set, query, queryChanges — plus copy, and the methods a type adds on top. Twenty-seven method names, one builder each, and a Kind in place of a string so that a misspelled type is a compile error rather than an answer from the server.

moonbit
let request = @jmap.Request::new()
let boxes = request.mailbox_get("u-1")
let newest = request.email_query("u-1", "mb-in", 25)
let body = request.to_string()        // POST this to the session's apiUrl

let answer = @jmap.Response::parse(reply)
for mailbox in @jmap.mailboxes(answer, boxes) {
  println(mailbox.name + ": " + mailbox.unread.to_string() + " unread")
}

The objects are written down once, as moonmodel descriptions: what each property is called, what type it holds, whether it may be null. That one table answers the property list, projects to JSON Schema for documentation, and is what @moonmail/jmap/validate checks a document against — so an object RFC 8621 prints passes, and a number where a string belongs does not.

out
Inbox: 3 unread
Drafts: 0 unread
Sent: 0 unread

A message becomes the object a server answers Email/get with, body structure and preview and all, which is how an importer or a server of your own builds one: @jmap.email_from_message(raw, mailbox_ids=["mb-in"]).

10

Point it at a real server

A test can feed recorded bytes, but a reply nobody wrote down is the one that teaches you something. The repository carries a harness that starts a Maildir, a Dovecot that serves it over IMAP and POP3, and a Postfix that delivers into it — on ports of their own, under their own configuration, so nothing already running on the machine is touched.

shell
sudo scripts/interop/services.sh start
MOONMAIL_LIVE=127.0.0.1:1143:1993:1111:1995:2525:2465:2526:1110:8025

MOONMAIL_LIVE=$MOONMAIL_LIVE moon test -p moonbitstack/moonmail/net
sudo scripts/interop/services.sh stop

Those numbers are the clear and the TLS port of each protocol — IMAP, POP3, SMTP — and then the sink's three: its SMTP, its POP3 and its HTTP API. The sink is Mailpit, which takes a message by SMTP, lists it over HTTP and hands it back by POP3, so one letter can be followed across three protocols. Set, every one of them has to answer — a missing service fails the test instead of passing it quietly. Unset, the tests that need a server do nothing and the rest of the suite is unchanged.

With a server up, a whole letter can be followed: the SMTP client hands it to Postfix, local delivery writes the Maildir, and the IMAP and POP3 clients read the same octets back out of Dovecot. The IMAP side is also filled by imaptest, the Dovecot project's own client, and the headers it wrote are fetched back with this library.

out
* OK [CAPABILITY IMAP4rev1 ID ENABLE IDLE SASL-IR LITERAL+ AUTH=PLAIN AUTH=LOGIN] moonmail interop ready

Two limits worth knowing before you point it at your own provider: the bundled TLS client speaks TLS 1.3 only, and a search for non-ASCII text has to name its charset — CHARSET UTF-8 SUBJECT "日本語" — because eight-bit octets cannot travel in an IMAP atom.