Back to articles
DevOps & Systems

Building an Absolute Anti-Spam Mail Server: Guide to Configuring Stalwart Mail Server with SPF, DKIM, DMARC, and RDNS

This guide provides a detailed process for configuring Stalwart Mail Server with SPF, DKIM, DMARC, and RDNS to build a spam-proof email system. It covers setup, DNS configuration,...

DPTCloud Engineering5 min read
Building an Absolute Anti-Spam Mail Server: Guide to Configuring Stalwart Mail Server with SPF, DKIM, DMARC, and RDNS

In today’s digital landscape, email remains a critical communication channel for businesses. Building a mail server with absolute anti-spam protection is essential to ensure security, reliability, and high performance. This guide provides a step-by-step process for configuring Stalwart Mail Server with SPF, DKIM, DMARC, and RDNS to prevent spam, phishing, and email spoofing. With its modern architecture, Stalwart offers scalability, internal security, and seamless integration with popular email services. This guide includes setup steps, DNS configuration, verification, CLI commands, and sample configuration files. It is suitable for users with basic DevOps and networking knowledge.

Introduction to Stalwart Mail Server and Email Security

Stalwart Mail Server is an open-source email system designed to function as an SMTP, IMAP, and POP3 server with a strong focus on security, performance, and scalability. Unlike traditional solutions like Postfix or Dovecot, Stalwart features a simplified, modular architecture and strong support for security protocols such as SPF, DKIM, DMARC, and RDNS. It is an ideal choice for businesses that want to manage their internal email systems without relying on public email services like Gmail or Microsoft 365.

These email security protocols play a critical role in authenticating email sources and preventing fraud:

  • SPF (Sender Policy Framework): Verifies that the sending server is authorized to send emails from your domain.
  • DKIM (DomainKeys Identified Mail): Digitally signs emails using a private key to prove they have not been altered during transmission.
  • DMARC (Domain-based Message Authentication, Reporting & Conformance): Combines SPF and DKIM to define how to handle spoofed emails and collect reports.
  • RDNS (Reverse DNS): Ensures the sending server's IP has a valid reverse DNS record, increasing trust with receiving servers.

Integrating all these protocols helps reduce the likelihood of emails being flagged as spam or rejected by major email providers such as Gmail, Outlook, and Yahoo.

Requirements and Environment Setup

To begin, you need a VPS or Dedicated Server with a stable internet connection. We recommend using NVMe Cloud VPS or High-Performance Dedicated Servers to ensure email processing performance and future scalability.

Technical requirements:

  • Operating System: Ubuntu 22.04 LTS or Debian 12
  • Software: Stalwart Mail Server (version 1.0 or higher), DNS client, OpenSSH
  • Hardware: Minimum 2GB RAM, 2 vCPU, 20GB SSD
  • Static IP address and a registered domain name

Before installation, ensure your domain is registered and you have access to the DNS management panel. You need to configure the following DNS records:

  • MX: Specifies the primary email server
  • TXT: For SPF and DMARC
  • CNAME or A: For DKIM (typically _domainkey.yourdomain.com)
  • PTR: For RDNS (requires configuration from your VPS/Dedicated Server provider)

Configuring Stalwart Mail Server and Security Integration

After installing the operating system, update it and install Stalwart:

sudo apt update
sudo apt upgrade -y
sudo apt install -y curl
curl -s https://get.stalwartmail.com/install.sh | sh

Stalwart creates a default configuration file at /etc/stalwart-mail/config.toml. Open this file to configure:

[smtp]
  domain = "yourdomain.com"
  port = 25
  tls = true
  [smtp.spf]
    enabled = true
    mode = "strict"
  [smtp.dkim]
    enabled = true
    selector = "default"
    key_file = "/etc/stalwart-mail/dkim/default.key"
  [smtp.dmarc]
    enabled = true
    policy = "quarantine"
    report_email = "[email protected]"

To generate a DKIM key:

stalwart-mail dkim generate --domain yourdomain.com --selector default --key-file /etc/stalwart-mail/dkim/default.key

After configuration, restart the service:

sudo systemctl restart stalwart-mail

Testing and Verification

Testing your configuration is crucial to ensure emails are not rejected. Use the following tools:

  • Check SPF: dig TXT yourdomain.com
  • Check DKIM: dig TXT default._domainkey.yourdomain.com
  • Check DMARC: dig TXT _dmarc.yourdomain.com
  • Verify RDNS: nslookup your-server-ip

Send a test email and verify delivery using tools like Mail Tester or DMARC Inspector. These tools will report errors and provide a security score.

Troubleshooting and Optimization

If emails are rejected, check the Stalwart logs at /var/log/stalwart-mail.log. Common issues include:

  • Invalid SPF: Verify the TXT SPF record and ensure it doesn't exceed the 10-record limit.
  • DKIM not verified: Check the DKIM key and TXT record for formatting errors or extra spaces.
  • DMARC rejected: Ensure the DMARC policy is valid (quarantine, reject, none).
  • Invalid RDNS: Contact your VPS provider to configure the PTR record.

Optimize performance by enabling SMTP caching, setting email sending limits, and using TLS 1.3.

References

For deeper insights into Stalwart Mail Server and email security protocols, refer to:

Related infrastructure & engineering guides: Guide to Setting Up a Private Ads Server with Keitaro on VPS for Businesses

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478