When managing a VPS or dedicated server, security extends beyond just protecting your file system and databases. If your server is responsible for sending emails—whether that involves hosting a full mail server like Postfix or simply sending automated password reset emails from your web application—you must protect your domain from being hijacked by spammers.
Without proper authentication, malicious actors can easily forge your domain name to send phishing emails, making it appear as though the malicious messages came directly from you. This will quickly destroy your domain's reputation, causing legitimate emails sent by your server to be permanently flagged as spam or blocked entirely by providers like Gmail and Outlook.
Implementing three specific DNS-based security protocols—SPF, DKIM, and DMARC—cryptographically proves to receiving servers that the emails you send are authentic.
Sender Policy Framework (SPF)
SPF acts as a public guest list for your domain. It is a simple DNS TXT record that explicitly lists the exact IP addresses and mail servers that are officially authorized to send emails on behalf of your domain.
When an email provider receives a message claiming to be from your website, it checks your domain's SPF record. If the IP address of the server that sent the email is not on that list, the receiving provider knows the email is likely a forgery and will reject it.
-
Creating the Record: Access your domain registrar's DNS dashboard and create a new TXT record.
-
Defining the Policy: A standard SPF record looks like
v=spf1 mx a ip4:192.0.2.1 -all. -
The "-all" Mechanism: This strict fail directive is crucial. It tells receiving servers to absolutely reject any email originating from an IP address not explicitly listed in the record. If you use
~all(soft fail), forged emails may still land in your users' spam folders rather than being blocked entirely.
DomainKeys Identified Mail (DKIM)
While SPF verifies the sender's IP, DKIM verifies the integrity of the message itself. DKIM ensures that the contents of the email (and its headers) have not been intercepted and altered while in transit across the internet.
It accomplishes this by using asymmetric cryptography. Your mail server is configured with a private key that it uses to digitally sign every outgoing email. You then publish the corresponding public key in your domain's DNS records.
-
Generating Keys: Depending on your mail transfer agent (MTA) or email delivery service (like SendGrid or Mailgun), you will generate a cryptographic key pair.
-
Adding the DNS Record: You publish the public key as a TXT record, usually under a specific selector subdomain (e.g.,
mail._domainkey.yourdomain.com). -
The Verification Process: When an email arrives at its destination, the receiving server queries your DNS for the public key and uses it to decrypt the signature. If the signature matches the email's content, the receiver knows the message is authentic and unaltered.
Domain-based Message Authentication, Reporting, and Conformance (DMARC)
SPF and DKIM are powerful, but they operate independently and do not tell the receiving server what to do if an email fails the checks. DMARC ties both protocols together into a unified, strict enforcement policy.
DMARC allows you to instruct email providers on exactly how they should handle messages that fail SPF or DKIM validation. More importantly, it provides a reporting mechanism so you can see exactly who is attempting to spoof your domain globally.
-
Setting the Policy: Create a DNS TXT record at
_dmarc.yourdomain.com. -
Enforcement Levels: You define the enforcement using the
p=tag.-
p=none: Monitors traffic and sends you reports without actually blocking any emails (used for initial testing). -
p=quarantine: Sends failing emails to the recipient's spam/junk folder. -
p=reject: The ultimate goal. This instructs receiving servers to completely drop and delete any email that fails authentication, entirely stopping phishing campaigns using your domain name.
-
-
Receiving Reports: Add the
rua=mailto:security@yourdomain.comtag to your DMARC record. Email providers will send daily XML reports to this address, showing you exactly which IP addresses are trying to send mail on your behalf.
