Skip to content

Lesson 11 of 14 · 18m

Running Your Own MTA

Self-hosting mail means the server is 20% of the job — ports, TLS renew hooks, config reload semantics, and backups are where the work actually lives.

What Actually Happens

A self-hosted mail server needs five things wired together: SMTP listeners (25 relay, 587/465 submission), an IMAP or JMAP endpoint for clients, a TLS certificate that renews and reloads the daemon when it does, DNS that agrees with itself (MX, PTR, SPF, DKIM, DMARC, MTA-STS), and storage you can restore. The daemon install is the easy quarter.

The software decision is a resource decision. Mailcow is the batteries-included stack — but it's ten-plus docker services and wants roughly 6 GB of RAM to itself, which does not fit on a shared 7 GB VPS with a blog, three apps, and a database already living there. Stalwart is a single Rust binary with JMAP, IMAP, Sieve, spam filtering, and a web admin built in — it fits next to everything else. Postfix + Dovecot is the classic split: the most documentation on earth, and the most config surface to get wrong. None of these is "best"; each trades ops burden for control differently, and on a small shared box the single binary wins on arithmetic alone.

Stalwart's layout on a real box: docker under /opt/stalwart, admin WebUI and JMAP on 127.0.0.1:8090 behind nginx answering as mailadmin.example.org, webmail (SnappyMail) on 127.0.0.1:8095 as mail.example.org. Admin access goes through an SSH tunnel plus basic auth — stalwart-cli targets STALWART_URL=http://127.0.0.1:8090. Nothing administrative faces the public internet, which is the correct default for a service whose compromise reads like a credential dump.

The Wrong Mental Model

"Self-hosting mail is apt install and DNS." The daemon is maybe 20% of the work. The other 80% is deliverability and the discipline of keeping every stated fact about your infrastructure true: PTR matching EHLO, SPF enumerating real senders, DKIM selectors rotated, renewal hooks actually restarting the server. mail-tester.com exists because people keep learning this the hard way.

The Mechanics That Bite

On Stalwart specifically, several production details are the kind you only learn by breaking them. stalwart-cli speaks a typed JSON dialect — list fields take a map {"0": {...}}, not an array; a password credential is {"@type":"Password","secret":...}; a snapshot of Account needs --allow-unresolved Domain,PublicKey,Role,Tenant. And edits are not live until you run stalwart-cli create Action --json '{"@type":"ReloadSettings"}' — a config that looks saved but hasn't been reloaded is the "why isn't it working" generator. TLS comes from Let's Encrypt via a webroot at /var/www/html, and the deploy hook at /etc/letsencrypt/renewal-hooks/deploy/stalwart.sh copies fresh certs into /opt/stalwart/tls and restarts the container — renewal without reload serves expired certs for ninety days minus one.

Backups are a cron that tars the docker volumes (~/sudo-ops/mail-backup.sh on the real box) — which requires briefly stopping the server, because copying a live database is how you get corrupt restores. The auto-ban trap from the inbound-pipeline lesson lives here too: proxied connections all arrive from the docker gateway 172.28.0.1, so AllowedIp plus clientIpSource=xForwardedFor must land before the ban feature turns on, and STALWART_RECOVERY_MODE is the escape hatch when you get it wrong.

Evidence

Port25 publishes a verifier (check-auth@verifier.port25.com) specifically because "does my outbound authenticate" is not answerable by reading configs — you send a message and read the report. The entire mail-tester/check-auth ecosystem exists because mail failures are silent: nothing logs "Gmail put you in spam," you just stop getting replies.

Correct Practice

Pick the smallest stack that fits the box; you will maintain all of it forever. Snapshot before config changes. Test outbound against port25's verifier and mail-tester before sending real mail. Keep the TLS renew hook and the reload action wired. And put the data volumes under a real backup before the first mailbox exists — mail you can't restore is mail you don't have.

Keep reading

Related articles from the SudoSecurity blog.