What you will set up
Stalwart is a mail server in a single program: it receives and sends mail, lets mail apps such as Outlook, Apple Mail and Thunderbird read it over IMAP, POP3 and JMAP, filters spam, signs outgoing mail with DKIM so receivers can check that it really comes from your domain, and has a web interface for managing domains and mailboxes. It can also serve calendars and contacts.
Here it runs as one container, under a user of its own called stalwart. The mail ports are open to the internet directly, so Stalwart sees every sender's real address, which its spam checks depend on. Its web interface sits behind Caddy from the Podman guide.
Every step below was run on a fresh Melonslab VC-P Alloy (2 vCPU, 8 GB) with Debian 13 and Stalwart 0.16.26. Attempts from the internet to pass mail through the server to other domains without logging in were refused. A test message passed SPF, reverse DNS and DKIM at an external checker, whose reply arrived over IPv6, and scored 9.4 out of 10 at mail-tester.com, losing points only because the test domain was new. IMAP, POP3 and sending on port 465 all used a Let's Encrypt certificate, and the spam filter's blocklist lookups were answered. The backup was restored onto a fresh Stalwart, an update went through with podman auto-update, and everything came back after a reboot. Stalwart used about 170 MB of memory.
If you would rather put a mail server together from Postfix and Dovecot yourself, see the Postfix guide.
Before you start
You need:
- a server set up as in the Podman guide, with Caddy running;
- a domain, such as
example.com, whose DNS records you can change, with an A record (IPv4) and an AAAA record (IPv6) formail.example.compointing at your server; - reverse DNS for both of the server's addresses set to
mail.example.com. Reverse DNS, or PTR, is the name behind an IP address, and receiving mail servers check that it matches. In the Melonslab panel, open your server's network tab and set it for both the IPv4 and the IPv6 address; the panel accepts it once the A and AAAA records exist. Step 2 of the Postfix guide shows how to check it; - either outbound port 25 opened for this server, or an account with an SMTP relay. Port 25 is how mail servers deliver to each other, and it is filtered on every server to protect our IP ranges. Direct sending is reviewed case by case under Manage Service in My Melonslab and normally only granted to established customers with a clear need. Without it you can still receive mail, and send through a relay as in the last section.
The examples use example.com, mail.example.com and 203.0.113.10 for your server's IPv4 address. Replace them with your own throughout.
You type the commands on the server, logged in over SSH as root as in the security guide. Each step says when to switch to another user. The rest happens in your browser and at your DNS provider.
1. Prepare the server
As root, let ordinary users use ports from 25 upwards, which covers the mail ports and Caddy's 80 and 443:
sed -i 's/ip_unprivileged_port_start = .*/ip_unprivileged_port_start = 25/' /etc/sysctl.d/50-unprivileged-ports.conf
sysctl --system
If you use ufw as in the security guide, open the mail ports too:
ufw allow 25,465,993,995/tcp
Stalwart checks the sender of every incoming message against blocklists such as Spamhaus. Those lists refuse lookups that arrive through large public DNS services, which is what the server uses by default, so give the server a resolver of its own:
apt install -y unbound bind9-dnsutils
systemctl restart unbound
Unbound looks names up by itself and answers only the server itself. Check it: after the restart, cat /etc/resolv.conf shows nameserver 127.0.0.1. bind9-dnsutils provides dig, for checking your DNS records later. The Unbound guide has more about it.
2. Create the user
As root, create the user Stalwart runs as, and switch to it:
useradd -m -s /bin/bash stalwart
loginctl enable-linger stalwart
machinectl shell stalwart@
Until the end of step 3, you work as stalwart. exit takes you back to root.
3. Describe the container
mkdir -p ~/.config/containers/systemd
Create ~/.config/containers/systemd/stalwart.container:
[Unit]
Description=Stalwart, a mail server
[Container]
ContainerName=stalwart
Image=docker.io/stalwartlabs/stalwart:v0.16
Volume=stalwart-etc:/etc/stalwart
Volume=stalwart-data:/var/lib/stalwart
# Mail ports, open to the internet
PublishPort=25:25
PublishPort=465:465
PublishPort=993:993
PublishPort=995:995
# Web interface, reached through Caddy only
PublishPort=127.0.0.1:8085:8080
# DNS lookups go to the server's own resolver from step 1
DNS=169.254.1.1
Environment=STALWART_PUBLIC_URL=https://mail.example.com
AutoUpdate=registry
[Service]
Restart=always
[Install]
WantedBy=default.target
Port 25 receives mail from other servers. Your mail apps use 465 to send and 993 (IMAP) or 995 (POP3) to read, all encrypted from the start. The web interface is only published on the server's loopback address, for Caddy. 169.254.1.1 is the address where Podman passes the container's DNS lookups on to the server's resolver.
Start it, and show its log:
systemctl --user daemon-reload
systemctl --user start stalwart
podman logs stalwart
The log shows a temporary password for the user admin, which you need in step 5. If it is not there yet, wait a few seconds and run podman logs stalwart again.
4. Put Caddy in front
Go back to root with exit, and switch to Caddy's user with machinectl shell caddy@. Add this block to the end of ~/Caddyfile:
mail.example.com {
reverse_proxy 127.0.0.1:8085
}
And restart Caddy, which briefly interrupts every site it serves:
systemctl --user restart caddy
Caddy gets a certificate for mail.example.com by itself, which takes a few seconds once the A and AAAA records work.
5. Run the setup wizard
Open https://mail.example.com/admin and log in as admin with the temporary password. The wizard has five steps:
- Server Identity. Replace the generated name in Server Hostname with
mail.example.com, and set Default Email Domain toexample.com. Turn off "Automatically Obtain TLS Certificate": it would use port 443, which Caddy has. Step 8 of this guide gets the certificate another way. Leave "Generate Email Signing Keys" on. - Storage. Keep the defaults.
- Account Directory. Keep the internal directory.
- Logging. Change Log Destination from Log file to Console, so the log appears in
podman logs. - Automatic DNS Management. Keep manual DNS server management, then choose Finish setup.
The last page shows your administrator's address and password once. Save them in your password manager: this is the account you manage Stalwart with. Then go back to root with exit, switch to Stalwart's user with machinectl shell stalwart@, and restart Stalwart:
systemctl --user restart stalwart
Log in at https://mail.example.com/admin with the new administrator account.
6. Tell Stalwart about Caddy
Do this straight away. Stalwart bans addresses that fail too many logins or probe for pages such as WordPress logins, and every web request reaches it through Caddy, from the server's own IPv4 address. Until Stalwart knows that, a single scanner on the internet can get the server's own address banned, and with it every visitor to the web interface. In our test, it happened within minutes.
The web interface has two sides, switched with the icons at the bottom left: Management (the screen), where you land after logging in, and Settings (the sliders). The search box at the top finds any setting by name.
- In Settings, open Network → HTTP → General, turn on Obtain remote IP from Forwarded header and save. Stalwart then bans the visitor's real address, which Caddy passes on.
- In Settings, open Security → Allowed IPs and choose Create address. Enter
203.0.113.10(your server's IPv4 address), pick today's date under Created At, which is required, and create it. The server's own address can then never be banned. - In Management, open Actions and run Server settings in the Reload section.
7. Publish the DNS records
Your domain's DNS records tell the rest of the internet where to deliver your mail, and how to check that mail from you is genuine. Stalwart lists them for you: in Management, open Domains → Domains, then the ⋯ menu of example.com, and choose View Zone File. It lists every record your domain needs:
- MX says which server receives mail for
example.com. - SPF, a TXT record on
example.com, lists the servers allowed to send mail from your domain. - DKIM, two TXT records, holds the public keys receivers use to check the signature Stalwart puts on every message you send.
- DMARC, a TXT record on
_dmarc.example.com, tells receivers what to do with mail that fails those checks, and where to send reports. - MTA-STS and TLS reporting tell other mail servers to deliver to you only over an encrypted connection with a valid certificate, and where to report problems with it.
- The rest help mail apps find their settings by themselves.
At your DNS provider, create every record in the list. If it includes records you already have, such as the A and AAAA records for mail.example.com, leave those as they are. The zone file writes names in full, ending in a dot, such as _dmarc.example.com.: many providers' forms want only the part before your domain (_dmarc), or @ for example.com itself. The RSA DKIM key is split into two quoted parts, because a single DNS string holds at most 255 characters: most providers' forms take it as one value, so join the two parts without the quotes.
Never create CAA, TLSA or _validation-persist records. They appear in the zone file only after step 8, so keep this in mind if you open it again later. CAA records like these would let only Stalwart's certificate account issue certificates for your whole domain, so Caddy could no longer renew certificates for your other sites. TLSA records pin today's certificates, and if your domain uses DNSSEC, servers that check them stop delivering to you once a certificate changes.
The DMARC record asks receivers to reject mail that fails SPF and DKIM, which suits a domain whose mail only comes from this server. It and the TLS reporting record send their reports to postmaster@example.com, which step 9 creates. If another service also sends mail from @example.com, such as a booking or invoicing system, set up SPF and DKIM for it first, following that service's instructions, or receivers will reject its mail.
Check the records from the server once your provider has published them, which usually takes a few minutes: dig +short MX example.com should print mail.example.com. with a number before it, dig +short TXT example.com a line starting with "v=spf1, and dig +short TXT _dmarc.example.com one starting with "v=DMARC1. For each DKIM record, run dig +short TXT with its name from the zone file: the answer should match the zone file. If a record does not show up, check its name at your provider and try again a little later.
Then give Caddy the other names from the zone file. Go back to root with exit, switch to Caddy's user with machinectl shell caddy@ as in step 4, and change the block from step 4 to:
mail.example.com, autoconfig.example.com, autodiscover.example.com, mta-sts.example.com, ua-auto-config.example.com {
reverse_proxy 127.0.0.1:8085
}
Restart Caddy with systemctl --user restart caddy. Mail apps fetch their settings from autoconfig and autodiscover, and other mail servers read your MTA-STS policy from mta-sts.
8. Get a certificate
Stalwart needs its own certificate for the mail ports. It gets one from Let's Encrypt through Caddy, which passes the check on port 80 on to it.
- Under Settings → TLS → ACME Providers, choose Create provider. Set the challenge type to HTTP-01, type a contact address such as
postmaster@example.comand press Enter to add it, then create the provider. - In Management, under Domains → Domains, open
example.com. Set Certificate Management to ACME TLS certificate management, pick the provider you just created, and save.
Within a minute, podman logs stalwart, run as the stalwart user, shows ACME order completed, and the mail ports use the new certificate. Stalwart renews it by itself.
9. Create a mailbox
Under Directory → Accounts, choose Create user. Enter a user name such as anna, pick example.com as the domain, and under Credentials choose Add item and set a password. Under Email Aliases, choose Add item, enter postmaster as the Local Part and pick example.com. Create the account: its address is anna@example.com, and mail to postmaster@example.com reaches it too. Every mail domain needs a postmaster address, and the reports from step 7 arrive there. Create a mailbox for everyone else the same way; only the first needs the postmaster alias.
10. Connect your mail app
Most mail apps set themselves up from just the address and password. If yours asks:
| Server | Port | Security | |
|---|---|---|---|
| Incoming (IMAP) | mail.example.com | 993 | SSL/TLS |
| Outgoing (SMTP) | mail.example.com | 465 | SSL/TLS |
The user name is the full address, anna@example.com, and the password is the one from step 9. For sending, use port 465 with SSL/TLS, also called implicit TLS: it is what the automatic setup gives your mail app, and the server does not offer port 587. If your app suggests 587 or STARTTLS, change it to 465 and SSL/TLS.
11. Check that it works
From your mail app, send a message to check-auth@verifier.port25.com. A report comes back to your inbox within a few minutes, which also shows that mail from the internet reaches you. Near the top, under Summary of Results, a good result shows SPF check: pass, "iprev" check: pass (your reverse DNS) and DKIM check: pass. It may also list a DKIM permerror with "unsupported signature algorithm": Stalwart signs with two keys, and that checker cannot read the newer Ed25519 one. Receivers use whichever signature they can check, so that line is not a problem.
For a score out of 10, send a message to the address shown at mail-tester.com and open the result there. Give it a subject and a few sentences, as an empty test message loses points. Our test server scored 9.4; a newly registered domain loses a little for being new.
If no report arrives, check that outbound port 25 has been unlocked (see Before you start), and read Troubleshooting below.
12. Keep it up to date
As the stalwart user, switch on Podman's daily updates for Stalwart:
systemctl --user enable --now podman-auto-update.timer
The v0.16 tag gets every fix within 0.16. Moving to a newer version, such as 0.17, is a change to the tag that you make yourself, after reading Stalwart's upgrade notes: its versions before 1.0 have changed how it stores its settings.
13. Back up
As the stalwart user:
umask 077
mkdir -p ~/backup
systemctl --user stop stalwart
podman volume export stalwart-data > ~/backup/stalwart-data.tar
podman volume export stalwart-etc > ~/backup/stalwart-etc.tar
systemctl --user start stalwart
That saves every mailbox, the settings, the DKIM keys and the certificate. Stalwart is stopped while it runs; other mail servers hold on to mail sent to you meanwhile and deliver it once Stalwart is back. Copy ~/backup to another machine afterwards, and keep it private.
To restore, on a server set up with steps 1 to 4, put the backup in /home/stalwart/backup, owned by stalwart, and run as stalwart:
systemctl --user stop stalwart
podman volume rm -f stalwart-data stalwart-etc
podman volume create --opt o=uid=2000,gid=2000 stalwart-data
podman volume create --opt o=uid=2000,gid=2000 stalwart-etc
podman volume import stalwart-data ~/backup/stalwart-data.tar
podman volume import stalwart-etc ~/backup/stalwart-etc.tar
systemctl --user start stalwart
uid=2000 gives the new volumes to the user Stalwart runs as inside the container. Without it, Stalwart stops at once with Permission denied. Accounts, mail and settings come back as they were, with the same DKIM keys, so your DNS records stay valid.
Troubleshooting
The web interface answers "403 Forbidden". Stalwart has banned the address you connect from, or, before step 6, the server's own address, which locks out everyone. As the stalwart user, this removes every ban; b is the ID Stalwart gives the administrator account on a new server:
podman exec stalwart curl -s -u 'admin@example.com:ADMIN_PASSWORD' http://127.0.0.1:8080/jmap/ -H 'Content-Type: application/json' -d '{"using":["urn:ietf:params:jmap:core","urn:stalwart:jmap"],"methodCalls":[["x:BlockedIp/query",{"accountId":"b"},"0"],["x:BlockedIp/set",{"accountId":"b","#destroy":{"resultOf":"0","name":"x:BlockedIp/query","path":"/ids"}},"1"],["x:Action/set",{"accountId":"b","create":{"r":{"@type":"ReloadBlockedIps"}}},"2"]]}'
Replace admin@example.com and ADMIN_PASSWORD with the administrator's address and password from step 5. Then do step 6.
Spam checks show RBL_SPAMHAUS_BLOCKED_OPENRESOLVER or other _BLOCKED results in a message's X-Spam-Result header. The blocklists refused Stalwart's lookups. Check that cat /etc/resolv.conf shows nameserver 127.0.0.1, as in step 1, and that podman exec stalwart cat /etc/resolv.conf shows only nameserver 169.254.1.1.
The certificate is not issued. Check that every name in the Caddy block has a DNS record pointing at the server, and read podman logs stalwart for lines starting with ACME.
Other mail servers reject your mail. Check reverse DNS for both addresses, and that the DNS records from step 7 are published, with dig TXT example.com and dig MX example.com.
Send through an SMTP relay
Outbound port 25 is filtered on every server to protect our IP ranges, and direct sending is only opened after review. Until then, or instead, send through an SMTP relay: a service built for delivering mail, which you reach on port 587. Stalwart keeps receiving mail on port 25 as normal. In the web admin, add the relay as a remote host with its host name, port 587, TLS and the user name and password it gives you, and set the outbound routing to deliver all mail for other domains through it. Add the relay to your SPF record as it describes, and publish the DKIM record it gives you.
These steps are for your own mail, your company's mail and transactional mail from your applications. We do not offer hosting for newsletters, bulk or cold email.