Email Security with SPF, DKIM, and DMARC
Learn how to protect your domain from email spoofing using SPF, DKIM, and DMARC records. Step-by-step guide for dashboard and API setup on DNScale.
Email spoofing allows attackers to send messages that appear to come from your domain. Without proper authentication, anyone can send an email claiming to be from ceo@yourdomain.com. SPF, DKIM, and DMARC are DNS-based standards that help receiving mail servers verify legitimate emails.
What You'll Learn
- How SPF declares authorized mail servers
- How DKIM cryptographically signs emails
- How DMARC defines policies for failed authentication
- Setting up all three via DNScale dashboard and API
- Testing and validating your configuration
SPF (Sender Policy Framework)
What SPF Does
SPF lets you specify which IP addresses and mail servers can send email for your domain. Receiving servers check your SPF record to verify the sender is authorized.
SPF Record Syntax
An SPF record is a TXT record at your domain apex:
v=spf1 [mechanisms] [qualifier]all| Component | Description |
|---|---|
v=spf1 | Version identifier (required) |
include:domain | Allow servers authorized by another domain's SPF |
a | Allow the domain's A record IP |
mx | Allow the domain's MX servers |
ip4:x.x.x.x | Allow a specific IPv4 address or range |
ip6:x::x | Allow a specific IPv6 address or range |
~all | Soft fail for unauthorized senders |
-all | Hard fail for unauthorized senders |
Common SPF Examples
Google Workspace:
v=spf1 include:_spf.google.com ~allMicrosoft 365:
v=spf1 include:spf.protection.outlook.com ~allMultiple providers:
v=spf1 include:_spf.google.com include:spf.protection.outlook.com ~allCustom mail server:
v=spf1 ip4:203.0.113.5 mx ~allSPF Best Practices
- Start with
~all(soft fail), move to-allafter testing - Keep under 10 DNS lookups (SPF limit)
- Include all sending sources: transactional email, marketing platforms, etc.
DKIM (DomainKeys Identified Mail)
What DKIM Does
DKIM adds a digital signature to outgoing emails. Your mail server signs each email with a private key, and you publish the corresponding public key in DNS. Receiving servers use this public key to verify the signature, proving the email wasn't tampered with and came from an authorized sender.
How DKIM Works
- Your mail server generates a cryptographic signature using a private key
- The signature is added to the email's headers
- Receiving servers look up your DKIM public key in DNS
- They verify the signature matches the email content
- If valid, the email passes DKIM authentication
DKIM Record Syntax
A DKIM record is a TXT record at selector._domainkey.yourdomain.com:
selector._domainkey.yourdomain.com. TXT "v=DKIM1; k=rsa; p=MIGfMA0GCS..."| Tag | Description |
|---|---|
v=DKIM1 | Version identifier |
k=rsa | Key type (usually RSA) |
p=... | Public key (base64 encoded) |
t=s | (Optional) Strict mode - exact domain match required |
t=y | (Optional) Testing mode |
DKIM Selectors
The selector is a name that identifies which key to use. This allows:
- Key rotation without downtime
- Different keys for different mail services
- Multiple keys active simultaneously
Common selector patterns:
googlefor Google Workspaceselector1,selector2for Microsoft 365k1,k2,k3for custom setupsmailjet,sendgridfor third-party services
Getting Your DKIM Keys
DKIM keys are generated by your email provider. Here's where to find them:
Google Workspace:
- Admin Console β Apps β Google Workspace β Gmail
- Authenticate email β Generate new record
- Copy the TXT record value
Microsoft 365:
- Microsoft 365 Defender β Email & collaboration β Policies
- DKIM β Select domain β Create DKIM keys
- Copy both selector1 and selector2 records
Third-party services (SendGrid, Mailjet, etc.):
- Check your provider's dashboard for DNS settings
- They'll provide the selector name and public key
DKIM Record Examples
Google Workspace:
google._domainkey.yourdomain.com. TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA..."Microsoft 365:
selector1._domainkey.yourdomain.com. TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNA..."
selector2._domainkey.yourdomain.com. TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNA..."Custom/Third-party:
mail._domainkey.yourdomain.com. TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3..."DKIM Best Practices
- Use 2048-bit keys - 1024-bit keys are increasingly vulnerable
- Rotate keys periodically - At least annually for security
- Keep private keys secure - Never expose them in DNS or logs
- Use unique selectors - Different selectors for each service
- Test before enforcing DMARC - Ensure DKIM passes before
p=reject
DMARC (Domain-based Message Authentication)
What DMARC Does
DMARC builds on SPF (and DKIM) to provide:
- A policy telling receivers what to do with failed emails
- Reporting so you can monitor authentication results
DMARC Record Syntax
A DMARC record is a TXT record at _dmarc.yourdomain.com:
v=DMARC1; p=policy; [optional tags]Required Tags:
| Tag | Description |
|---|---|
v=DMARC1 | Version identifier |
p=none|quarantine|reject | Policy for failed emails |
Optional Tags:
| Tag | Description |
|---|---|
rua=mailto:address | Aggregate report destination |
ruf=mailto:address | Forensic report destination |
pct=100 | Percentage to apply policy (default 100) |
sp=policy | Subdomain policy |
DMARC Policy Options
| Policy | Action | When to Use |
|---|---|---|
p=none | Monitor only | Initial setup |
p=quarantine | Send to spam | Intermediate |
p=reject | Block entirely | Full protection |
DMARC Examples
Monitor mode:
v=DMARC1; p=none; rua=mailto:dmarc-reports@yourdomain.comQuarantine mode:
v=DMARC1; p=quarantine; rua=mailto:dmarc-reports@yourdomain.com; pct=100Reject mode:
v=DMARC1; p=reject; rua=mailto:dmarc-reports@yourdomain.comRecommended Rollout
- Week 1-2:
p=noneto monitor - Week 3-4: Review reports, fix issues
- Week 5-6:
p=quarantinewithpct=25 - Week 7-8: Increase to
pct=100 - Week 9+: Move to
p=reject
Setup via DNScale Dashboard
Create SPF Record
- Log in at dnscale.eu
- Select your domain
- Click Add Record
- Configure:
- Name: Leave empty (apex)
- Type: TXT
- Content:
v=spf1 include:_spf.google.com ~all - TTL: 3600
- Click Create Record
Create DKIM Record
- Get your DKIM public key from your email provider (see "Getting Your DKIM Keys" above)
- Click Add Record
- Configure:
- Name:
google._domainkey(or your provider's selector) - Type: TXT
- Content:
v=DKIM1; k=rsa; p=MIIBIjANBgkq...(your public key) - TTL: 3600
- Name:
- Click Create Record
Note: For Microsoft 365, you'll need to create two records (selector1 and selector2).
Create DMARC Record
- Click Add Record
- Configure:
- Name:
_dmarc - Type: TXT
- Content:
v=DMARC1; p=none; rua=mailto:dmarc@yourdomain.com - TTL: 3600
- Name:
- Click Create Record
Setup via DNScale API
Authentication
export DNSCALE_API_KEY="your-api-key"Get Zone ID
curl -s "https://api.dnscale.eu/v1/zones" \
-H "Authorization: Bearer $DNSCALE_API_KEY" | jq '.data.zones[] | {id, name}'Create SPF Record
curl -X POST "https://api.dnscale.eu/v1/zones/{zone_id}/records" \
-H "Authorization: Bearer $DNSCALE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "@",
"type": "TXT",
"content": "v=spf1 include:_spf.google.com ~all",
"ttl": 3600
}'Create DKIM Record
curl -X POST "https://api.dnscale.eu/v1/zones/{zone_id}/records" \
-H "Authorization: Bearer $DNSCALE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "google._domainkey",
"type": "TXT",
"content": "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...",
"ttl": 3600
}'Replace google._domainkey with your selector and the public key with your actual DKIM key.
Create DMARC Record
curl -X POST "https://api.dnscale.eu/v1/zones/{zone_id}/records" \
-H "Authorization: Bearer $DNSCALE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "_dmarc",
"type": "TXT",
"content": "v=DMARC1; p=none; rua=mailto:dmarc@yourdomain.com",
"ttl": 3600
}'Verify Records
curl -s "https://api.dnscale.eu/v1/zones/{zone_id}/records" \
-H "Authorization: Bearer $DNSCALE_API_KEY" | jq '.data.records[] | select(.type == "TXT")'Testing and Validation
Online Tools
- MXToolbox SPF Check
- MXToolbox DKIM Check
- MXToolbox DMARC Check
- DMARC Analyzer
- Mail Tester - Send a test email to check all authentication
Command Line
# Check SPF
dig TXT yourdomain.com +short
# Check DKIM (replace 'google' with your selector)
dig TXT google._domainkey.yourdomain.com +short
# Check DMARC
dig TXT _dmarc.yourdomain.com +shortSend a Test Email
The best way to verify everything works is to send a test email and check the headers:
- Send an email to a Gmail account
- Open the email β Click the three dots β "Show original"
- Look for:
spf=passdkim=passdmarc=pass
Summary
| Record | Name | Type | Example |
|---|---|---|---|
| SPF | @ | TXT | v=spf1 include:_spf.google.com ~all |
| DKIM | selector._domainkey | TXT | v=DKIM1; k=rsa; p=MIIBIjAN... |
| DMARC | _dmarc | TXT | v=DMARC1; p=none; rua=mailto:dmarc@example.com |
How They Work Together:
- SPF verifies the sending server is authorized
- DKIM verifies the email content wasn't modified
- DMARC tells receivers what to do when SPF or DKIM fails
Recommended Implementation Order:
- Set up SPF first (easiest)
- Configure DKIM with your email provider
- Add DMARC in monitor mode (
p=none) - Review reports, fix issues
- Gradually enforce DMARC (
p=quarantineβp=reject)