DEV Community

yaroslav
yaroslav

Posted on Originally published at servertoolpick.com

Email Server Setup on VPS: Avoiding the Spam Folder (DKIM, SPF, DMARC Explained)

Introduction

One of the most frustrating problems when running your own mail server on a VPS is watching perfectly legitimate emails land in spam folders. You've configured Postfix or Exim correctly, your server is stable, and messages are sending—but they're disappearing into recipients' junk bins. The culprit is almost never your mail server's reliability; it's almost always authentication and reputation.

Modern email providers like Gmail, Microsoft, and Yahoo didn't implement DKIM, SPF, and DMARC out of paranoia. These standards emerged because unsecured email systems enabled mass spoofing and phishing attacks. Today, these three authentication methods aren't optional—they're table stakes for inbox delivery. Without them, ISPs will actively filter your mail, and no amount of server optimization will help.

This guide walks you through what these standards do, why they matter, and how to implement them correctly on your VPS.

Understanding Email Authentication Problems

Before the 1990s, email had no built-in authentication. Anyone could forge a "From:" header and claim to be someone else. That's why spammers and phishers could impersonate banks and retailers so easily.

The authentication standards we use today—SPF, DKIM, and DMARC—form a layered defense:

  • SPF tells receivers which IP addresses are authorized to send mail for your domain
  • DKIM digitally signs outgoing messages so they can't be altered in transit
  • DMARC ties them together and tells receivers what to do if authentication fails

Email receivers use these signals to make reputation-based filtering decisions. A message from an unknown IP that fails all three checks? Spam. A message from an established domain with proper authentication? Delivered.

The cost of getting this wrong is significant. Legitimate transactional emails (password resets, order confirmations) may not reach users. Marketing campaigns underperform. Customer support tickets don't arrive. For businesses, poor email deliverability can cost thousands in lost revenue.

SPF (Sender Policy Framework) — The First Line of Defense

SPF is the simplest of the three standards. It's a DNS TXT record that lists which mail servers are authorized to send email from your domain.

How SPF Works

When a receiving mail server gets an email claiming to be from noreply@yourdomain.com, it performs an SPF check:

  1. It looks up the SPF record for yourdomain.com in DNS
  2. It extracts the sending server's IP address from the message headers
  3. It checks whether that IP appears in the authorized list
  4. It either accepts the message or rejects it based on the SPF policy

Creating Your SPF Record

An SPF record is a single DNS TXT entry. Here's a basic example:

v=spf1 ip4:192.0.2.1 include:sendgrid.net ~all
Enter fullscreen mode Exit fullscreen mode

Breaking this down:

  • v=spf1 — Specifies SPF version 1
  • ip4:192.0.2.1 — Authorizes the IP 192.0.2.1 to send mail
  • include:sendgrid.net — Authorizes all IPs listed in SendGrid's SPF record
  • ~all — Soft-fail on mismatches (recommended for testing); use -all for hard-fail once confirmed working

Common SPF Problems

The most frequent mistake is the "SPF permerror"—DNS TXT records that violate SPF syntax. Another is oversizing the record: SPF lookups have a hard limit of 10 DNS queries. If you use too many include: directives, SPF checks fail.

Better practice: Consolidate authorized mail services. If you're using multiple ESPs (email service providers), ask them to share a single SPF include or to route through one master service.

DKIM (DomainKeys Identified Mail) — Signing Your Messages

DKIM cryptographically signs outgoing emails so receivers can verify they haven't been tampered with. Think of it as a wax seal on a letter—receivers can check that the seal is genuine.

How DKIM Works

Your mail server signs outgoing messages using a private key stored locally. The public key lives in DNS as a TXT record. When a receiver gets your message:

  1. They extract the DKIM signature from the message headers
  2. They look up your public key in DNS
  3. They use the public key to verify the signature
  4. If the signature is valid, the message passed authentication

Setting Up DKIM

On most Linux mail servers, you generate a DKIM keypair using opendkim-genkey:

opendkim-genkey -b 2048 -d yourdomain.com -s default
Enter fullscreen mode Exit fullscreen mode

This creates two files:

  • default.private — Stored securely on your server, restricted to the mail daemon
  • default.txt — Published in DNS as a TXT record

The DNS record looks like:

default._domainkey.yourdomain.com TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w..."
Enter fullscreen mode Exit fullscreen mode

Configure your mail daemon (Postfix, Exim) to sign all outgoing mail with this key. Most modern configurations do this automatically once the key is in place.

DKIM Best Practices

  • Use 2048-bit keys at minimum; 4096-bit is stronger but slower to verify
  • Rotate keys annually; keep the old key published for 30 days during transitions
  • Test your DKIM setup at tools like MXToolbox or 250ok

DMARC (Domain-based Message Authentication, Reporting & Conformance) — The Enforcement Layer

DMARC ties SPF and DKIM together and lets you specify what receivers should do if both fail. It also sends you reports on authentication failures, which is invaluable for debugging.

How DMARC Works

DMARC is a DNS policy that says: "If a message claims to be from my domain but fails SPF and DKIM, I want you to either quarantine it or reject it outright. And please send me a report."

A basic DMARC record:

_dmarc.yourdomain.com TXT "v=DMARC1; p=quarantine; rua=mailto:dmarc@yourdomain.com"
Enter fullscreen mode Exit fullscreen mode

Breaking this down:

  • v=DMARC1 — DMARC version 1
  • p=quarantine — Quarantine failing messages (safer than p=reject during rollout)
  • rua=mailto:dmarc@yourdomain.com — Send aggregate reports to this address

DMARC Policy Levels

Start conservative and escalate:

Policy Behavior Use Case
p=none No action; receive reports only Testing, initial setup
p=quarantine Receivers quarantine (spam folder) Production, after testing
p=reject Receivers reject outright Long-term, once you control all senders

Most organizations spend 2–4 weeks on p=none, review the reports, then move to p=quarantine.

Reading DMARC Reports

ISPs send XML reports showing which messages passed/failed authentication. Key metrics:

  • Percent passed: Aim for 100% before switching to p=reject
  • Alignment failures: Messages claiming your domain but sent from uncontrolled IPs
  • DKIM/SPF failures: Signing or policy misconfigurations

Tools like Google Admin Toolbox and Dmarcian parse these reports automatically.

Practical Implementation Guide

For a Fresh VPS Setup

  1. Add SPF: Create a TXT record v=spf1 ip4:YOUR_SERVER_IP -all
  2. Generate DKIM: Create keys and add the public key to DNS
  3. Add DMARC: Start with v=DMARC1; p=none; rua=mailto:admin@yourdomain.com
  4. Send test emails to Gmail, Outlook accounts you control
  5. Check headers: Verify Authentication-Results shows PASS for SPF, DKIM, DMARC
  6. Wait 48 hours: Let DNS propagate globally
  7. Review DMARC reports (arrive within 24–48 hours)
  8. Upgrade DMARC policy: Move to p=quarantine after validating 100% legitimate traffic passes

VPS Provider Considerations

If you're comparing hosting options, look for providers that:

  • Offer reverse DNS (PTR) configuration—essential for mail reputation
  • Don't block outbound port 25 or require authentication via separate port 587
  • Provide detailed control panel DNS management

ServerToolPick compares VPS providers with mail-hosting in mind and can help you find options that don't impose unnecessary restrictions on email.

Monitoring Deliverability

Beyond SPF/DKIM/DMARC, monitor:

  • Sender reputation: Check your IP at MXToolbox and SenderScore
  • Bounce rates: High bounces harm reputation. Implement list hygiene.
  • Complaint rates: If recipients mark you as spam, ISPs notice
  • Authentication logs: Review postfix/exim logs for signing errors

Conclusion

Email authentication isn't optional anymore—it's the baseline for inbox delivery. SPF, DKIM, and DMARC work together to prove you control your domain and aren't spoofing. Setting them up correctly requires 15–20 minutes of DNS configuration and takes a few days to propagate, but the payoff is enormous: your transactional and marketing emails reach inboxes instead of spam folders.

Start with SPF, add DKIM, and layer on DMARC. Begin with permissive policies, monitor reports, and escalate once you're confident. This methodical approach prevents legitimate email from being rejected while catching spoofing attempts.

The infrastructure for secure email exists. Using it is just a matter of DNS records and configuration discipline.

Top comments (0)