Email server setup
Running docker-mailserver: ports, DNS records, SpamAssassin, DKIM, DMARC, MTA-STS and BIMI.
docker-mailserver simplifies secure mail server setup with a containerized, production-ready solution (SMTP, IMAP, anti-spam, anti-virus). It uses only configuration files, ensuring simplicity and version control without SQL databases.
References
Prerequisites
- Podman running on a Linux Server
- A domain name with the ability to change DNS entries
Ports
# - "25:25" # SMTP (used for MTA-to-MTA relaying, supports STARTTLS; AUTH usually disabled — don't use for submission)# - "110:110" # POP3 (unencrypted or STARTTLS upgrade; use 995 for implicit TLS instead)# - "143:143" # IMAP (unencrypted or STARTTLS upgrade; use 993 for implicit TLS instead)# - "465:465" # SMTP Submission (implicit TLS — alternative to 587, supported by some clients)- "587:587" # SMTP Submission (STARTTLS — standard for client-to-server authenticated mail sending)- "993:993" # IMAPS (implicit TLS — secure IMAP from start; preferred over 143 + STARTTLS)# - "995:995" # POP3S (implicit TLS — secure POP3 from start; preferred over 110 + STARTTLS)docker-mailserver Setup
Get the certificate using standalone method
# Change `mail.example.com` below to your own FQDN.# Requires access to port 80 from the internet, adjust your firewall if needed.mkdir -p ./certbot/certsmkdir -p ./certbot/logssudo podman run --rm -it \ -v "${PWD}/certbot/certs/:/etc/letsencrypt/" \ -v "${PWD}/certbot/logs/:/var/log/letsencrypt/" \ -p 80:80 \ docker.io/certbot/certbot certonly --standalone -d mail.example.comor using DNS method
sudo podman run --rm -it \ -v "${PWD}/certbot/certs/:/etc/letsencrypt/" \ docker.io/certbot/certbot certonly --manual --preferred-challenges dns -d mail.example.comAfter the certificate is generated
cp ./certbot/certs/live/mail.example.com/fullchain.pem ./dms/ssl/public.crtcp ./certbot/certs/live/mail.example.com/privkey.pem ./dms/ssl/private.keyUse this SSL ready compose.yaml
services: mailserver: image: ghcr.io/docker-mailserver/docker-mailserver:latest container_name: mailserver hostname: mail.example.com environment: - LOG_LEVEL=info - SSL_TYPE=letsencrypt - ENABLE_MTA_STS=1 - ENABLE_CLAMAV=1 - ENABLE_SPAMASSASSIN=1 - SPAMASSASSIN_SPAM_TO_INBOX=1 - MOVE_SPAM_TO_JUNK=1 ports: - "25:25" # SMTP (explicit TLS => STARTTLS, Authentication is DISABLED => use port 465/587 instead) - "143:143" # IMAP4 (explicit TLS => STARTTLS) - "465:465" # ESMTP (implicit TLS) - "587:587" # ESMTP (explicit TLS => STARTTLS) - "993:993" # IMAP4 (implicit TLS) volumes: - ./docker-data/certbot/certs/:/etc/letsencrypt - ./docker-data/dms/mail-data/:/var/mail/ - ./docker-data/dms/mail-state/:/var/mail-state/ - ./docker-data/dms/mail-logs/:/var/log/mail/ - ./docker-data/dms/config/:/tmp/docker-mailserver/ - ./docker-data/dms/cron/sa-learn:/etc/cron.d/sa-learn - /etc/localtime:/etc/localtime:ro restart: always stop_grace_period: 1m healthcheck: test: "ss --listening --tcp | grep -P 'LISTEN.+:smtp' || exit 1" timeout: 3s retries: 0or download the compose.yaml file for docker-mailserver and adjust hostname according to your FQDN.
curl -o compose.yaml https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master/compose.yamlDownload the mailserver.env and adjust the file as needed.
curl -o mailserver.env https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master/mailserver.envIf you want to set your timezone add
TZ=Europe/Istanbul#orTZ=GMT
ENABLE_MTA_STS=1SSL_TYPE=letsencryptENABLE_CLAMAV=1Now you can start the docker-mailserver
podman compose up -dTo create email addresses
podman exec -it mailserver setup email add postmaster@example.com very-secret-passwordTo create alias addresses. All emails sent to hello@example.com will be then forwarded to your.name@example.com
podman exec -it mailserver setup alias add hello@example.com your.name@example.comTo create a catch-all account
podman exec -it mailserver setup alias add @example.com your.name@example.comIt will create a catch-all account by adding the following to ~/docker-data/dms/config/postfix-virtual.cf:
@example.com your.name@example.comSpamAssassin settings
services: mailserver: environment: - ENABLE_SPAMASSASSIN=1 - SPAMASSASSIN_SPAM_TO_INBOX=1 - MOVE_SPAM_TO_JUNK=1 volumes: - ./docker-data/dms/cron/sa-learn:/etc/cron.d/sa-learn ...NOTE: This example assumes you have a /var/mail-state volume mounted.
Create a system cron file:
# in the compose.yaml root directorymkdir -p ./docker-data/dms/crontouch ./docker-data/dms/cron/sa-learnchown root:root ./docker-data/dms/cron/sa-learnchmod 0644 ./docker-data/dms/cron/sa-learnEdit the system cron file nano ./docker-data/dms/cron/sa-learn, and set an appropriate configuration:
# '> /dev/null' to send error notifications from 'stderr' to 'postmaster@example.com'## m h dom mon dow user command## Everyday 2:00AM, learn spam from a specific user# spam: junk directory0 2 * * * root sa-learn --spam /var/mail/example.com/username/.Junk --dbpath /var/mail-state/lib-amavis/.spamassassin > /dev/null# ham: archive directories15 2 * * * root sa-learn --ham /var/mail/example.com/username/.Archive* --dbpath /var/mail-state/lib-amavis/.spamassassin > /dev/null# ham: inbox subdirectories30 2 * * * root sa-learn --ham /var/mail/example.com/username/cur* --dbpath /var/mail-state/lib-amavis/.spamassassin > /dev/null## Everyday 3:00AM, learn spam from all users of a domain# spam: junk directory0 3 * * * root sa-learn --spam /var/mail/not-example.com/*/.Junk --dbpath /var/mail-state/lib-amavis/.spamassassin > /dev/null# ham: archive directories15 3 * * * root sa-learn --ham /var/mail/not-example.com/*/.Archive* --dbpath /var/mail-state/lib-amavis/.spamassassin > /dev/null# ham: inbox subdirectories30 3 * * * root sa-learn --ham /var/mail/not-example.com/*/cur* --dbpath /var/mail-state/lib-amavis/.spamassassin > /dev/nullTesting spam (GTUBE test pattern)
The GTUBE (Generic Test for Unsolicited Bulk Email) is a standardized 68-byte string designed to test anti-spam filters, especially those using SpamAssassin. When detected, SpamAssassin assigns it a spam score of 1000 by default—enough to trigger a spam flag in any properly configured system.
To verify that your spam filter is functioning correctly, send a test email containing the GTUBE string in the body. If SpamAssassin is active, it will detect and flag the message as spam.
Include the following GTUBE string in the email body:
XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X# Send a mail for testing spam (GTUBE test pattern):podman compose exec dms swaks \ --server mail.remote.test \ --port 25 \ --from john.doe@example.test \ --to jane.doe@example.test \ --body 'XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X'Testing anti-virus (EICAR test pattern)
Download Anti Malware Testfile - EICAR
Instead of using real malware, which could cause real damage, this test file allows people to test anti-virus software without having to use a real computer virus.
Create a file with the following content and attached it to the email.
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*# Send a mail for testing anti-virus (EICAR test pattern):podman compose exec dms swaks \ --server mail.remote.test \ --port 25 \ --from john.doe@example.test \ --to jane.doe@example.test \ --attach 'X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*'Mail Encryption
Generate your global Elliptic Curve (EC) Key. In order to generate an Elliptic Curve (EC) Key, you must first choose a curve from the output of this command:
openssl ecparam -list_curvesIf you choose the curve prime256v1, generate an EC key with the command:
openssl ecparam -name prime256v1 -genkey | openssl pkey -out ecprivkey.pemThen generate a public key out of your private EC key
openssl pkey -in ecprivkey.pem -pubout -out ecpubkey.pemCreate 10-custom.conf and populate it with the following
# Enables mail_crypt for all services (imap, pop3, etc)mail_plugins = $mail_plugins mail_cryptplugin { mail_crypt_global_private_key = </certs/ecprivkey.pem mail_crypt_global_public_key = </certs/ecpubkey.pem mail_crypt_save_version = 2}While you’re editing the compose.yaml, add the configuration file:
services: mailserver: image: ghcr.io/docker-mailserver/docker-mailserver:latest volumes: . . . - ./config/dovecot/10-custom.conf:/etc/dovecot/conf.d/10-custom.conf - ./certs/:/certs . . .Start the container, monitor the logs for any errors, send yourself a message, and then confirm the file on disk is encrypted:
[root@ip-XXXXXXXXXX ~]# cat -A /mnt/efs-us-west-2/maildata/awesomesite.com/me/cur/1623989305.M6v�z�@�� m}��,��9����B*�247.us-west-2.compute.inE��\Ck*�@7795,W=7947:2,T�9�8t�6�� t���e�W��S `�H��C�ڤ �yeY��XZ��^�d�/��+�AThis should be the minimum required for encryption of the mail while in storage.
Automate Certificate Renewal
The following displays the expiration date of the certificate, according to the ITU-T standard specifications for X.509 digital certificates.
openssl x509 -dates -noout < docker-data/certbot/certs/live/mail.example.com/cert.pemLet’s Encrypt certificates are valid for 90 days. Use a cron job to automate the renewal process.
Edit the crontab
crontab -eAdd the following line to renew the certificates every two months automatically and reload the docker-mailserver to apply the renewed certificates:
0 0 1 */2 * certbot renew --quiet && podman compose -f /path/to/your/compose.yaml down && podman compose -f /path/to/your/compose.yaml up -dDNS Setup
Add A and MX DNS records:
| Type | Name | Content |
|---|---|---|
| A | IPV4_ADDRESS | |
| MX | @ | mail.example.com |
DKIM
To generate DKIM keys:
podman exec -it mailserver setup config dkimThen add a DNS TXT record:
| Type | Name | Content |
|---|---|---|
| TXT | mail._domainkey | v=DKIM1; k=rsa; p=MIIxxx |
DMARC
_dmarc IN TXT "v=DMARC1; p=none; rua=mailto:dmarc.report@example.com; ruf=mailto:dmarc.report@example.com; sp=none; ri=86400"a bit more strict policies (mind p=quarantine and sp=quarantine)
_dmarc IN TXT "v=DMARC1; p=quarantine; rua=mailto:dmarc.report@example.com; ruf=mailto:dmarc.report@example.com; fo=0; adkim=r; aspf=r; pct=100; rf=afrf; ri=86400; sp=quarantine"SPF
| Type | Name | Content |
|---|---|---|
| TXT | example.com. | v=spf1 mx ~all |
PTR (rDNS, Reverse DNS)
To set a reverse DNS (rDNS or PTR) entry for a cloud server, you first need to log into Cloud Console. Once you’re there, select your cloud server and click on “Networking”. To the right of your IPs, you can edit the rDNS entry.
BIMI (Optional)
Brand Indicator Message Identification (BIMI) Records is used to display a company’s logo inside an email inbox if the email is trustworthy.
# without VMCdefault._bimi.example.com IN TXT "v=BIMI1; l=https://example.com/bimi/logo.svg;"# with VMCdefault._bimi.example.com IN TXT "v=BIMI1; l=https://example.com/bimi/logo.svg; a=https://example.com/vmc.pem;"Requirements Before BIMI
- SPF, DKIM, and DMARC properly set up. You must have a valid DMARC policy with either
p=quarantineorp=reject. - Your brand logo must be in SVG Tiny Portable/Secure (SVG P/S) format. File must be accessible via HTTPS (e.g., https://example.com/logo.svg).
- (Optional) Some providers like Gmail require a Verified Mark Certificate (VMC) issued by a trusted authority like DigiCert or Entrust.
BIMI SVG (Tiny P/S) Requirements
- Format: SVG Tiny 1.2 Portable/Secure (P/S) Profile (not regular SVG)
- MIME type:
image/svg+xml - Dimensions: At least 64x64 pixels or higher is recommended
- Aspect ratio: Square (1:1), ideally with centered content
- File size: Should be under 32 KB
- No scripts, fonts, or external resources
- Must include
<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny-ps">
MTA-STS (Optional)
MTA-STS (Mail Transfer Agent Strict Transport Security) helps email servers automatically enforce the use of secure communication channels (TLS) for sending and receiving email, making email delivery safer and more reliable.
compose.yaml
services: mta-sts: image: httpd:alpine # or nginx:alpine container_name: mta-sts volumes: - ./mta-sts/.well-known:/usr/local/apache2/htdocs/.well-known:ro labels: - "traefik.enable=true" - "traefik.http.routers.mta-sts.rule=Host(`mta-sts.example.com`)" - "traefik.http.routers.mta-sts.entrypoints=websecure" - "traefik.http.routers.mta-sts.tls.certresolver=myresolver" # for Let's Encrypt - "traefik.http.services.mta-sts.loadbalancer.server.port=80"/.well-known/mta-sts.txt
version: STSv1mode: enforcemx: mail.example.commax_age: 86400DNS records
| Type | Name | Content |
|---|---|---|
| TXT | _mta-sts | v=STSv1; id=20250418T0000; |
| A/CNAME | mta-sts | IP or subdomain.example.com |
Test
https://mta-sts.example.com/.well-known/mta-sts.txtTLS-RPT (Optional)
TLS-RPT is a useful tool for administrators to monitor and enforce email security by reporting on TLS failures. It’s an important step in securing email transmissions and preventing the interception of sensitive data.
_tlsrpt.example.com. 3600 IN TXT "v=TLSRPTv1; rua=mailto:admin@example.com; ruf=mailto:admin@example.com; pct=100"v=TLSRPTv1: Indicates the version of TLS-RPT.rua=mailto:admin@example.com: Specifies the email address to receive aggregate reports.ruf=mailto:admin@example.com: Specifies the email address to receive forensic reports.pct=100: Indicates that the policy applies to 100% of email traffic (you can adjust this percentage if you want to start with a smaller subset of email traffic).
Backup
crontab -eMAILTO=dev@aripd.com0 3 * * * /usr/bin/tar -czvf /root/backup.tar.gz /root/docker-data/ > /dev/null