Self Host Wizard Docs

Mail server (stalwart)

Mail server on a Self Host Wizard box: the stalwart piece, served at mail.<apps domain>, ldap sign-in, memory limit 1024 MB.

At a glance

FieldValue
Addresshttps://mail.<apps domain>
Sign-inldap: the app checks the office password against LLDAP itself
Memory limit1024 MB (ram_mb: measured peak × 1.3)
Databasenone shared: it keeps its own data in THX_DATA
Needslldap
Needed byforgejo, freescout, glitchtip, snappymail, workspace
Containersstalwart
Image: stalwartstalwartlabs/stalwart:v1.2.0-alpine@sha256:2b7dfee66dfd116a903607826e03e4f8af8841207fae28098aa908c568ca919a
Image: stalwart-initstalwartlabs/stalwart:v1.2.0-alpine@sha256:2b7dfee66dfd116a903607826e03e4f8af8841207fae28098aa908c568ca919a (one-shot set-up step)
Public ports25/tcp, 465/tcp, 587/tcp, 993/tcp
Inputs the admin typesthx secret set stalwart relay_host, thx secret set stalwart relay_user, thx secret set stalwart relay_password
Set-up script after startscripts/post-up.sh, every 1m
Licence (from the README)AGPL-3.0
Upstream (from the README)https://github.com/stalwartlabs/stalwart

Turn it on

thx enable adds stalwart and everything it needs to the box's state; thx apply then installs it. Apps are applied only once site_domain is set.

sudo thx enable stalwart
sudo thx apply

Engineering notes (pieces/stalwart/README.md)

Mail for every company domain on the box: receives on 25, users send on 465/587 and read on 993 (and webmail, see pieces/snappymail). Users and passwords are LLDAP's; nothing is stored in Stalwart but mail. All outbound mail leaves through the cloud provider's email service (the outgoing-mail relay) on 587: the provider blocks outbound 25, and the email service signs DKIM.

Upstream: stalwartlabs/stalwart v1.2.0, AGPL-3.0 (some features are under the Stalwart Enterprise licence; none used here). Image stalwartlabs/stalwart:v1.2.0-alpine@sha256:2b7dfee6…919a, a multi-arch index with linux/arm64 (docker buildx imagetools inspect, 2026-11-24).

How it is configured

Stalwart 1.x has no config file: everything lives in its database and is set over JMAP (x:<Object>/get|set, schema in crates/registry/src/schema/ at the v1.2.0 tag). scripts/post-up.sh (→ scripts/stalwart.py converge) does it, idempotently: the first run on an empty data directory answers the bootstrap (RocksDB, LDAP directory, hostname), restarts, then converges; later runs patch only fields that differ, print changed only when they changed something, and restart Stalwart only when a setting that needs it changed (listeners and routes are read at start; Pocho saw the same).

WhatValue
DirectoryLLDAP over THX_LDAP_*; login by uid or mail; recipients by mail or mailalias1…mailalias16
Adminthx-admin = STALWART_RECOVERY_ADMIN with THX_SECRET_ADMIN_PASSWORD (LDAP has no Stalwart admin; post-up needs one)
Domainsone per THX_DOMAINS entry; catch-all off; DKIM/DNS/certificates "manual". Removed domains are logged, never deleted
AliasesLLDAP user attributes mailalias1…mailalias16 (ALIAS_SLOTS, one address each): a user's other addresses, on any served domain. Received and sent as. Not a list attribute: LLDAP 0.6.3 matches a list by equality only while it holds exactly one value (lldap#1133), so a second alias made every alias bounce 550 (FDFRETES 2026-11-27)
Listenerssmtp 25, submissions 465 (implicit TLS), submission 587, imaps 993, http 8080 (127.0.0.1 only). Defaults pop3s/sieve/https removed
Outboundevery non-local recipient → route ocirelay: THX_INPUT_RELAY_HOST:587, STARTTLS required, certificate verified, AUTH THX_INPUT_RELAY_USER / THX_INPUT_RELAY_PASSWORD (referenced by env-var name, never stored)
Internal relayport 25 without AUTH relays only when the client is on a piece network (10.213.0.0/16, never a gateway .1, i.e. not the host's or the internet's published port) and the sender is THX_MAIL_FROM; such mail skips the spam filter
EHLOport 25 refuses a non-FQDN greeting except from those same piece-network clients: apps greet with their container id or localhost
Certificatescopied from Caddy's storage (see below)
WebUIunpackDirectory = /var/lib/stalwart/webui (Pocho: /tmp is cleaned and the admin UI 404s days later). It is downloaded from GitHub on first start: outbound HTTPS needed
Master usersthx-<piece>@<apps_domain> for each piece in IMPERSONATORS (snappymail, freescout) that is in THX_PIECES: a local account record holding the Impersonate permission, matched to the LLDAP service user of that name (its password = the piece's ldap_password secret; core keeps the user). Login <target>%thx-<piece>@<apps_domain> opens the target's mailbox. A piece that is disabled loses the permission (Inherit) at the next post-up
Helpdeskwith freescout enabled, per company: local account thx-help-<org>@<first domain> and mailing list support@<first domain> (aliases support@ on its other domains) delivering to it. A directory domain refuses mail to the local account's own address, so it is reachable only through the list. MtaStageAuth.mustMatchSender lets that account send as its support@ addresses and nothing else (needs a restart, which post-up does)
BansStalwart's defaults stay (100 failed logins or 35 unknown recipients a UTC day ban an address for good) for everyone but piece containers: each piece network stalwart is on, minus its gateway, is an AllowedIp (reason thx: piece network, never banned), and bans already on those addresses are lifted. A piece retries every scheduler tick, so minutes of wrong credentials banned freescout for good (FDFRETES 2026-11-27). The gateway stays bannable: the host and, through docker-proxy, the internet arrive from it
Cachescapped at 64 MiB (messages) / 16 MiB (contacts, events, files) / 8 MiB (exchange); the defaults add up to ~1.6 GB, above the memory limit. Directory recipients 30 s (see "Removed addresses")

Certificates: Caddy obtains, Stalwart serves

Caddy already holds 80/443 and runs ACME for every other host, so it also gets mail.<domain> (the core lane adds a mail.<domain> site block per domain, proxied to the admin UI). stalwart.py certs reads <caddy storage>/certificates/*/mail.<d>/mail.<d>.{crt,key} (newest issuer wins) and installs each one in Stalwart over JMAP, then reloads TLS without a restart. Reasons: one ACME client on the box; Stalwart's own ACME would need 80 or 443, which Caddy holds (or DNS-provider credentials on the box); Caddy's keys are 0600 and owned by caddy, so mounting them into a uid-2000 container would mean loosening permissions. A missing certificate is a warning, not a failure (first apply, before DNS). Renewal: Caddy renews ~30 days before expiry; stalwart.py certs must run daily (a timer; see the report).

Loud failure when the relay refuses a sender

The relay answers 535 at MAIL FROM when the From address is not an Approved Sender. Stalwart treats it as permanent: the message bounces to the sender (a DSN naming the 535), and the log gets a delivery.dsn-perm-fail line. python3 -I scripts/stalwart.py relay-check exits 1 and prints those lines when any appeared in the last 24 h; thx verify should run it.

Gap: noreply@<apps_domain> has no mailbox, so its bounces have nowhere to go; only relay-check shows them.

Removed addresses

Stalwart copies an LLDAP user into an Account record of its own the first time it meets them (a login, or mail to one of their addresses), and every later sync only adds aliases (v1.2.0, crates/common/src/cache/directory.rs, sync_account). No setting changes that. Measured on the test stack before this fix (2026-11-27, aliaslag lane): after mailalias1 was taken from bob in LLDAP, bob still sent as bob@a.test and kept its JMAP identity for good (12 min, end of the probe), and RCPT accepted it for 10 min (directoryRecipientsTtl, default 10 min); given to alice, mail to it still went to bob's mailbox. A user deleted in LLDAP: the own address and aliases took mail for 10 min. LLDAP groups carry nothing into Stalwart (they have no mail), so a group change moves no address.

So post_up makes each record hold exactly the aliases LLDAP gives its address: it reads every user's mail and mailalias* (LLDAP's GraphQL API as the bind user, from inside the stalwart container as listmonk does), removes the rest (all of them for a user LLDAP no longer has), then runs InvalidateNegativeCaches, which also forgets the directory's "this address exists" answers. It never deletes an account or mail. LLDAP being empty fails post_up instead of stripping every alias. directoryRecipientsTtl is 30 s, so a deleted user's own address stops taking mail within 30 s with nothing to drop.

When it runs: post_up_every = "1m" (a timer, so within ~2 min of any LLDAP edit, its web UI included); thx user alias add|rm and thx user rm re-run it right after LLDAP. test.sh asserts a 60 s bound from the LLDAP edit, post_up run included (measured 2026-11-27): removed alias refused at RCPT, not sendable, identity gone in 8 s; reassigned, it reaches the new holder only; a deleted user with an alias, both addresses and the login refused in 13 s; a deleted user without aliases, refused in 32 s with no post_up at all.

Memory, OOM, logs

  • mem_limit 1024 MB = 788 MB × 1.3: the peak Pocho measured on his production Stalwart over 56 days (230 MB at rest, 695 MB active). The test run here peaks at 146–176 MB, which says nothing about production load.
  • oom_score_adj: -500, as on Pocho's box: mail outlives a spike elsewhere.
  • Logs go to stdout (tracer Stdout, level info) and Docker keeps json-file 10 MB × 3, so there are no log files to prune. At Pocho's ~1 MB/day that is about 30 days.

What LLDAP cannot revoke

LLDAP decides who logs in, but Stalwart 1.2.0 lets each person make things of their own over JMAP or its web UI that never ask LLDAP again: app passwords and API keys log in to IMAP, SMTP and JMAP (erin, 2026-11-28: after her LLDAP delete her app password still opened IMAP and SMTP), and **masked addresses take mail. Passkeys** Stalwart already refuses for directory accounts. And a person deleted in LLDAP left a Stalwart account with all their mail, which whoever LLDAP next gave the address logged in to: Stalwart matches by name.

So post_up, every run:

  1. Roles. The default user and group roles (Authentication.defaultUserRoleIds and defaultGroupRoleIds; every account directory sync makes has role User) get sysAppPasswordCreate, sysApiKeyCreate, sysPasskeyCreate and sysMaskedEmailCreate out of enabledPermissions and into disabledPermissions (a disabled permission beats every role, and Stalwart's upgrades never re-add one). Names from crates/registry/src/schema/enums.rs at v1.2.0. Creating one answers forbidden.
  2. Sweep. Every app password, API key, passkey and masked address on a person's account is deleted. A person is a User account whose name does not start with thx-; the box's own accounts (master users thx-<piece>, helpdesk thx-help-<org>, set-aside mailboxes) and the recovery admin thx-admin (no account record; its password is an environment variable) are never touched, and the outgoing-mail relay's credential is not in Stalwart at all. The log names the address and the count per kind, never a secret (Stalwart returns none).
  3. Trust check. LLDAP's answer (GraphQL, as for "Removed addresses") must have people in it, and must not lack more than half of the people with a mailbox when it lacks more than one: a reset or wrong LLDAP still has its admin. Otherwise post_up fails, changing no alias and no mailbox. When that many really were removed: python3 -I scripts/stalwart.py converge --set-aside-many, once.
  4. Aliases (see "Removed addresses").
  5. Deleted users. A person whose address LLDAP no longer has is set aside, never deleted: the account is renamed thx-removed-<UTC date YYYYMMDD>-<name> on the same domain (thx-removed-<date>.<n>-<name> if that is taken), keeps no address, gets permissions Merge with authenticate disabled (no login of any kind, even for someone LLDAP gives the new name), and keeps every message. The description says thx: set aside <date>, was <address>. The address is free: its next holder gets a new, empty mailbox.

thx user rm runs post_up at once; the timer catches deletes made in LLDAP's web UI within ~2 min.

Commands below run on the box with the piece's environment, as post_up does: sudo bash -c 'cd /srv/thx/pieces/stalwart && set -a && . ./.env && set +a && COMPOSE_PROJECT_NAME=thx-stalwart COMPOSE_FILE=$PWD/compose.yaml python3 -I scripts/stalwart.py <command>'.

Restore: create the LLDAP user with the old address again, then stalwart.py restore thx-removed-<date>-<name>@<domain>. It renames the account back, permissions Inherit (every directory account's), and its LLDAP aliases come back at the person's next login. It refuses when LLDAP has no user with that address (the next post_up would set it aside again), and when the person already logged in and got a new mailbox: --swap sets that new one aside first (it becomes thx-removed-<date>.<n>-<name>; restore that one the same way to undo).

Dry run: stalwart.py plan prints what the steps above would do right now (roles patched, credentials deleted per address and kind, aliases dropped, mailboxes set aside, or the trust check failing) on stderr and sends nothing.

Measured (dev PC, amd64, test.sh, 2026-11-25)

stalwart 146–176 MB peak (three runs), lldap 3–16, Mailpit relay 8–12, Mailpit impostor 7–15
stalwart 169 MB peak with master users + helpdesk (mail lane run)
stalwart 160, lldap 55, relay 16, impostor 9 MB peak with the removed-address tests (aliaslag, 2026-11-27)
stalwart 161, lldap 56, relay 15, impostor 9 MB peak with the self-made credential and set-aside tests (mailcreds, 2026-11-28)

test.sh

test/stack.sh (shared with snappymail) makes a test CA, fake Caddy storage for mail.a.test and mail.b.test, a throwaway LLDAP with alice@a.test (aliases alice@b.test, alice@c.test, hello@a.test) and bob@b.test, and Mailpit as the outgoing-mail relay (STARTTLS + AUTH required, certificate from the test CA); runs post-up.sh twice (first prints changed, second nothing). test.sh then proves: IMAP login and refusal; local delivery; delivery to each of alice's three aliases and sending as each (not as bob); relay with AUTH as the relay user; port-25 rules from a container on stalwart's network and on another piece network (both 10.213/16, as on the box), an outside network and the host; a 535 bouncing to the sender and relay-check failing; certificate renewal served without a restart; a relay with an untrusted certificate receiving nothing; bans: both piece networks allowed except their gateways, a hand-made ban on a piece client lifted by post_up (one on a gateway kept), 101 failed logins from a piece container leave its login open and unbanned, the same from an outside network ban it.

Removed addresses (check.py unalias, userrm): an alias given to bob in LLDAP takes mail, is sendable and a JMAP identity; taken away, within 60 s it is refused at RCPT, not sendable and no identity; given to alice, it reaches her INBOX only and only she sends as it. A user deleted with no post_up is refused within 60 s; one with an alias, deleted then post_up, has both addresses refused and the login refused.

What LLDAP cannot revoke (check.py selfmade, setaside, distrust). With Stalwart's default role put back, alice makes an app password, an API key and a masked address: the app password logs in to IMAP and SMTP, the key opens JMAP, the masked address takes mail. After post_up (changed, then quiet) the app password is refused on both, the key gets 401, the masked address is refused at RCPT; creating any of the four kinds answers forbidden; her LDAP password still logs in to IMAP, SMTP (and sends) and JMAP. frank (alias frank@c.test) gets mail and is deleted in LLDAP: post_up sets him aside as thx-removed-<date>-frank@a.test (no address, authenticate disabled, his mail still there), every address refused at RCPT, an LLDAP user given the set-aside address refused at login; frank2, given frank@a.test, opens an empty INBOX. restore is refused while frank2's mailbox holds the address, --swap sets that one aside and frank@a.test shows the old mail again; a restore without the LLDAP user is refused. Six users deleted at once out of ten: post_up fails and changes no mailbox or alias; converge --set-aside-many then sets them aside, and post_up is quiet after.

Master users and helpdesk. stack.sh puts the LLDAP users thx-snappymail and thx-freescout in place, as core would. THX_DOMAINS gives acme two domains (a.test and c.test) and beta one (b.test). The tests prove:

  • Both master users open bob's INBOX and submit as him.
  • A master login is refused with a wrong master password, with the target's own password, with alice as master, and with the LDAP bind user as master.
  • With snappymail and freescout dropped from THX_PIECES, post-up prints changed and every master login is refused. Re-enabled, they work again, and the run after that is quiet.
  • Customer mail to support@a.test and support@c.test reaches thx-help-acme, mail to support@b.test reaches thx-help-beta, and nothing crosses between them. The account's own address is not a recipient.
  • thx-help-acme sends as support@a.test and support@c.test through the relay.
  • Refused senders: acme's account as alice or as support@b.test; alice as support@a.test; bob through the master as support@b.test.

The recovery admin thx-admin can also impersonate: it is a superuser.

It prints PASS stalwart ram_peak_mb=<n>. Mutations that make it fail (2026-11-25): a sender rule without the sender check (a forged send as alice was accepted), and no aliases (support@c.test answered 550). Mutations 2026-11-27 (mail fix lane): aliases as one LLDAP list attribute, alice with three: 550 5.1.1 Mailbox does not exist for alice@b.test, the box's error; no allowances: alice's login is banned, want open after 101 failed logins from another piece network; gateways allowed too: the gateway 10.213.131.1 of 10.213.131.0/24 is allowed; attrEmailAlias naming only the first slot: alias alice@c.test did not reach alice's INBOX; ALIAS_SLOTS changed here alone: go test fails (TestStalwartAgrees). Mutations 2026-11-28 (mailcreds lane), each on its own stack (.lead/mailcreds/mutate.sh): roles left alone: alice may still make ['AppPassword', 'ApiKey', 'MaskedEmail', 'Passkey']; no sweep: after post_up still working: ['app password IMAP', 'app password SMTP', 'API key JMAP', 'masked address RCPT']; no set-aside: frank@a.test is still an account after post_up; set aside without NO_LOGIN: permissions {'@type': 'Inherit'}; no trust check: post-up with six of ten people gone exits 0 and sets them all aside. Mutations 2026-11-27 (aliaslag lane): no alias pruning in post_up: post-up after the alias went printed '', want 'changed'; pruning kept but no cache flush and the default 10 min recipients TTL: still {'rcpt'} 62 s after LLDAP lost it (bound 60 s).

Known gaps

  • Bounces of inbound mail to external senders go through the relay with an empty MAIL FROM; the relay may refuse them (not testable without the real relay).
  • RocksDB writes its own LOG in the data directory (Pocho: 72 MB); not pruned here.
  • org-* groups are not mapped to anything; mail does not need them.
  • Customer mail from domains without SPF/DKIM lands in the helpdesk account's Junk Mail; FreeScout fetches that folder too.
  • A company removed from THX_DOMAINS keeps its helpdesk list and account; post-up only logs them.
  • No backup in this piece; THX_DATA is in the box backup.
  • A person whose LLDAP mail changes gets a new, empty mailbox at their next login (Stalwart's own behaviour); the old one is set aside like a deleted person's. restore cannot put it under the new address; the admin can rename it by hand.
  • Not verified: OAuth sessions of Stalwart's own web UI. A refresh token issued before an LLDAP delete or thx user suspend may renew until the account is set aside (delete) or not at all (suspend).
  • post_up runs from the timer and from thx user rm or thx user alias add|rm with no lock between them; two at once can make the second fail on an object the first just changed (loud, the next run converges).

Facts on this page come from these files in the selfhostwizard repository:

  • pieces/stalwart/piece.toml
  • pieces/stalwart/compose.yaml
  • pieces/stalwart/README.md
  • internal/piece/piece.go

This page is generated from the code at build time, so it changes when the code does.