Home › Blog › Article

Email Deliverability: SPF, DKIM and DMARC Setup

Email Deliverability: SPF, DKIM and DMARC Setup

Answer first: what to do and how to know it worked

Inventory every sender that uses your domain, publish one SPF TXT record, enable DKIM with your ESP or mail host, then add DMARC starting at v=DMARC1; p=none; with reporting. Send controlled tests to Gmail, Microsoft 365 and a mailbox you control. It is done right when SPF passes with fewer than 10 DNS lookups, DKIM verifies with the published selector, DMARC reports show aligned identifiers, and bounce logs contain no 550 5.7.1 policy rejects. Keep the old records in a change note so rollback takes one DNS edit.

准备: inventory, access and a rollback snapshot

Before touching DNS, list senders: WordPress transactional mail, WooCommerce order mail, contact forms, CRM, marketing ESP, helpdesk, invoices, cron/server alerts and any legacy SMTP plugin. On a typical WordPress 6.8 or 6.9 site in 2026, check wp-config.php, the options table, and plugin settings rather than assuming wp_mail() uses PHP mail. The official wp_mail() reference documents the headers and phpmailer_init hook we will use later.

Record the current zone as a rollback file. Run these from a workstation with dig installed; Cloudflare, Route 53, cPanel and Porkbun all expose the same TXT values, only the UI differs. Set TTL to 300 seconds during the change window, then raise it to 3600 after 48 hours.

mkdir -p ~/dns-change-2026/example.com
dig +short TXT example.com | tee ~/dns-change-2026/example.com/spf.before.txt
dig +short TXT selector1._domainkey.example.com | tee ~/dns-change-2026/example.com/dkim.before.txt
dig +short TXT _dmarc.example.com | tee ~/dns-change-2026/example.com/dmarc.before.txt
dig +short MX example.com | tee ~/dns-change-2026/example.com/mx.before.txt
wp option get admin_email --path=/var/www/example.com/public
wp plugin list --status=active --path=/var/www/example.com/public

Common pitfall: two SPF TXT records. RFC 7208 permits exactly one; split records produce permerror. Also check the envelope sender, not only From:. A form can display From: sales@example.com while the ESP envelope is bounces@send.example.net, which changes DMARC alignment. If you run several storefronts, align the sender policy with the architecture decision documented in Multisite or Separate Installs: Choosing by Scenario before publishing DMARC.

Step Process: publish, test and enforce in six moves

  1. Freeze mailer changes and snapshot the zone with the commands above.
  2. Publish a single consolidated SPF record including every authorized sender.
  3. Enable DKIM in each provider and add the selector CNAME or TXT records.
  4. Add DMARC at p=none with rua and ruf reporting.
  5. Send seeded tests, read headers and aggregate reports for 7-14 days.
  6. Tighten to p=quarantine then p=reject only after 100% aligned volume.

H3.1 Lock the WordPress mail path

Use an SMTP or API mailer, not local sendmail, so the ESP can sign DKIM. In wp-content/mu-plugins/mailer-policy.php add a small guard that forces a consistent From address and logs the Message-ID. WordPress 6.8+ loads MU plugins before normal plugins, so this survives plugin deactivation.

<?php
/** Plugin Name: Site Mail Policy */
add_action( 'phpmailer_init', function ( $phpmailer ) {
    $phpmailer->From = 'no-reply@example.com';
    $phpmailer->FromName = 'Example Store';
    $phpmailer->Sender = 'no-reply@example.com';
} );
add_filter( 'wp_mail', function ( $atts ) {
    error_log( 'MAIL_TO=' . $atts['to'] . ' SUBJECT=' . $atts['subject'] );
    return $atts;
} );

Log to /var/log/php8.3-fpm/example.com-mail.log via your pool’s php_admin_value[error_log] setting. PHP 8.3 is the safe baseline on production hosts in 2026; PHP 8.4 is common but confirm your mailer plugin supports it. If you customize templates, keep header changes compatible with Customize WooCommerce Emails: Vendor Guide 2026 so DKIM-signed bodies are not rewritten after signing.

The three DNS records

SPF, DKIM and DMARC solve different problems. SPF authorizes the connecting IP for the envelope sender. DKIM adds a cryptographic signature over selected headers and the body, surviving forwarding. DMARC tells receivers what to do when neither SPF nor DKIM produces an identifier aligned with the visible From: domain. All three are TXT records at different names; DKIM is often a CNAME to the provider instead.

RecordHost / NameType2026 baseline valueFailure signal
SPFexample.com or the envelope subdomainTXTv=spf1 include:_spf.google.com include:amazonses.com -allspf=permerror, spf=fail, too many lookups
DKIMselector._domainkey.example.comTXT or CNAMEv=DKIM1; k=rsa; p=MIIBIjANBgkqh...dkim=fail (body hash did not verify)
DMARC_dmarc.example.comTXTv=DMARC1; p=none; rua=mailto:dmarc@example.com; pct=100; adkim=r; aspf=rdmarc=fail, no reports, spoofing unnoticed

Get a Quote

Email + requirement + budget range — we reply with a quote within 24 hours.

H3.2 SPF mechanics and lookup budget

SPF allows 10 DNS-querying mechanisms: include, a, mx, exists, ptr and redirects. A single include:amazonses.com can expand further, so count with dig, not by eyeballing. Use ip4: / ip6: for your own relays because they cost zero lookups. Prefer ~all during migration, then -all after reports are clean. Do not use +all and do not add ptr.

dig +short TXT example.com
# v=spf1 ip4:203.0.113.42 include:_spf.google.com include:amazonses.com include:mailgun.org ~all
python3 -m pip install --user spf-tools==2026.1
sPFquery example.com 203.0.113.42 no-reply@example.com

Rollback: replace the TXT with the exact spf.before.txt value at 300-second TTL, then re-run dig. If the provider UI added a second SPF row, delete the newer row; never merge by concatenating two v=spf1 strings.

H3.3 DKIM keys, selectors and body integrity

Generate keys at the provider, not on your web server, so the private key never lands in the WordPress document root. Google Workspace uses google._domainkey; Microsoft 365 publishes selector1._domainkey and selector2._domainkey CNAMEs; Amazon SES generates three CNAMEs under _domainkey. 2048-bit RSA remains the interoperable default in 2026; Ed25519 (k=ed25519) is supported by some receivers but keep an RSA selector as fallback. Rotate by publishing the new selector, sending with it for 72 hours, then removing the old TXT.

Pitfall: a plugin or CDN that appends tracking pixels, unsubscribe footers or encoding conversions after signing breaks the body hash. Sign last. If you use an API mailer, pass the fully rendered HTML body to the API and avoid a second local SMTP hop. Rollback is selector-scoped: restore the previous p= value from dkim.before.txt and switch the mailer back to the old selector in the provider console; no zone-wide change is needed.

H3.4 DMARC policy, alignment and reporting

Start with monitoring. adkim=r and aspf=r mean relaxed alignment, so mail.example.com aligns with example.com. Strict (s) is for later, after you confirm every subdomain sender. rua=mailto: receives daily XML aggregates; ruf=mailto: receives forensic samples, which many receivers now redact, so do not depend on them. Add fo=1 only if you have a mailbox that can handle the volume. A subdomain policy sp=none prevents an accidental org-wide reject while you test.

_dmarc.example.com. 300 IN TXT "v=DMARC1; p=none; sp=none; rua=mailto:dmarc-agg@example.com; ruf=mailto:dmarc-forensic@example.com; fo=1; adkim=r; aspf=r; pct=100; ri=86400"

# After 14 clean days:
_dmarc.example.com. 300 IN TXT "v=DMARC1; p=quarantine; pct=25; rua=mailto:dmarc-agg@example.com; adkim=r; aspf=r; ri=86400"

# Final state:
_dmarc.example.com. 3600 IN TXT "v=DMARC1; p=reject; sp=reject; rua=mailto:dmarc-agg@example.com; adkim=r; aspf=r; ri=86400"

External reporting addresses require authorization: add example.com._report._dmarc.reporting-domain.net TXT "v=DMARC1" if your DMARC SaaS uses another domain. Rollback from an over-tight policy is immediate: set p=none and pct=100, keep rua, wait one TTL, then re-send tests. Do not delete the DMARC record; deleting it removes visibility and leaves spoofing unmonitored.

Provider-specific values

Copy values from the provider console on the day of the change; selectors and include domains are account-specific and rotate. The table below reflects the stable 2026 patterns we deploy on client sites. Never paste a vendor value from a blog into production without matching it to the console, and never add two SPF includes for the same sender because a plugin and a manual entry both looked plausible.

ProviderSPF additionDKIM recordNotes
Google Workspaceinclude:_spf.google.comgoogle._domainkey TXT, 2048-bit key generated in Admin Console > Apps > Gmail > Authenticate emailPropagation can take up to 24 hours; click START AUTHENTICATION after the TXT resolves.
Microsoft 365 / Exchange Onlineinclude:spf.protection.outlook.comselector1._domainkey and selector2._domainkey CNAMEs to *.onmicrosoft.comUse Get-DkimSigningConfig in Exchange Online PowerShell v3.5+ to confirm Enabled=True.
Amazon SESinclude:amazonses.comThree CNAMEs under _domainkey.example.com from Easy DKIMUse a MAIL FROM subdomain such as mail.example.com with its own SPF + MX for bounce handling.
Mailguninclude:mailgun.orgsmtp._domainkey TXT or CNAME depending on regionEU region uses eu.mailgun.org sending; keep the domain verified in the correct region.
Postmark / SendGridinclude:spf.postmarkapp.com / include:sendgrid.netPostmark 20260601rsa._domainkey-style dated selector; SendGrid CNAME s1._domainkey and s2._domainkeyRequires a custom Return-Path CNAME for SPF alignment; without it DMARC relies on DKIM only.
Transactional host / cPanela or ip4:203.0.113.42default._domainkey generated by cPanel Email DeliverabilityShared IPs change; prefer an ESP API over host relay for WooCommerce receipts.

For stores, the highest-risk gap is a second mail path: a fulfilment extension sending directly through sendmail while orders go through SES. Audit plugin hooks with wp hook list wp_mail --path=/var/www/example.com/public (WP-CLI 2.11+) and compare against the sender inventory. If you are rebuilding that mail layer, our Custom WordPress Development team typically moves all transactional templates to one API sender before enforcing p=reject. Store owners comparing vendor tooling can also review the broader technical column index for adjacent deployment notes.

Verification and greylisting

Verify in layers: DNS resolution, protocol-level authentication, then real inbox placement. DNS first, because a cached negative answer looks exactly like a broken DKIM signature. Use an authoritative nameserver directly to bypass your resolver cache, then query a public resolver.

NS=$(dig +short NS example.com | head -1 | sed 's/.$//')
dig @$NS +short TXT example.com
dig @$NS +short TXT google._domainkey.example.com
dig @$NS +short TXT _dmarc.example.com
dig @1.1.1.1 +short TXT _dmarc.example.com
openssl s_client -connect gmail-smtp-in.l.google.com:25 -starttls smtp -brief < /dev/null

Next, send one message per sender path to a Gmail, an Outlook and a self-hosted mailbox. Read Authentication-Results, not the marketing UI. A healthy 2026 header looks like spf=pass smtp.mailfrom=mail.example.com; dkim=pass header.d=example.com; dmarc=pass header.from=example.com. Also confirm Return-Path matches the domain you authorized. Use the WordPress email deliverability documentation when explaining to site owners why plugin-level From headers do not replace DNS authentication.

H3.5 Greylisting: distinguish a deferral from a reject

Greylisting is a temporary 4xx deferral keyed on the triplet (sender IP, envelope sender, recipient). It is not an SPF/DKIM/DMARC failure. A typical response is 451 4.7.1 Greylisting in action, please try again later or 421 4.7.0 Try again later. Correct handling is to let the MTA queue and retry with backoff; do not rewrite DNS, do not rotate DKIM keys, and do not switch providers after one deferral. On Postfix 3.9, confirm queue_run_delay=300s, minimal_backoff_time=300s, maximal_backoff_time=3600s and maximal_queue_lifetime=3d in /etc/postfix/main.cf, then reload with postfix reload. For API ESPs, inspect the provider event log for deferred followed by delivered within 5-45 minutes.

Pitfall: a new sending IP with no reputation gets greylisted more often, and an impatient smoke test marked as a failure leads teams to roll back a perfectly correct SPF record. Log the SMTP code before changing anything. If the same triplet is still deferred after 3 hours, check rDNS/PTR, EHLO name and IP blocklists; those are reputation issues, not authentication issues. Rollback from a bad verification conclusion is simply restoring spf.before.txt / dmarc.before.txt, but only do it when the header shows spf=permerror or dmarc=fail, never for 4xx.

Tracing bounces

Tracing bounces

Tracing bounces means following one Message-ID from WordPress to the ESP, to the receiver response, and back into a DSN or webhook. Enable structured logging first. In the MU plugin above we logged recipient and subject; add the Message-ID by reading $phpmailer->getLastMessageID() after send via the wp_mail_succeeded action, and write failures with wp_mail_failed. Both actions are available in WordPress 6.8+ and give you a join key.

add_action( 'wp_mail_succeeded', function ( $mail_data ) {
    error_log( 'MAIL_OK ' . wp_json_encode( $mail_data ) );
} );
add_action( 'wp_mail_failed', function ( $wp_error ) {
    error_log( 'MAIL_FAIL ' . $wp_error->get_error_message()
        . ' DATA=' . wp_json_encode( $wp_error->get_error_data() ) );
} );

# Correlate on the host
grep 'MAIL_OK|MAIL_FAIL' /var/log/php8.3-fpm/example.com-mail.log | tail -50
journalctl -u postfix@- --since '2026-06-01 09:00' --until '2026-06-01 10:00' | grep '<message-id@>'
postqueue -p | grep -i 'deferred' -A3

Classify the response before acting. 550 5.7.1 ... DMARC policy or 550 5.7.26 Unauthenticated email is an alignment problem: check the envelope domain and DKIM d=. 550 5.1.1 User unknown is list hygiene. 554 5.7.1 SPF permerror means duplicate TXT or lookup overflow. 421/451 is greylisting or throttling. 552 5.2.2 Mailbox full needs no DNS change at all. For SES, match the Message-ID in the SNS notification mail.messageId; for Mailgun, use the Events API message-id filter; for Postmark, use the Message Streams bounce webhook.

Build a small triage table in your runbook with columns: timestamp, Message-ID, sender path, envelope domain, SMTP code, ESP event, action taken. After 7-14 days, parse DMARC aggregates and reconcile counts against WordPress logs; a gap means an unlogged sender. If enforcement causes receipts to fail, roll back to p=none, keep reporting, fix the missing include or Return-Path CNAME, wait one TTL plus one queue retry cycle, then re-test. Do not disable DKIM to hide a body-hash failure; fix the component modifying the body after signing.

回滚: the exact rollback runbook

Rollback is a DNS revert plus a mailer revert, executed in that order. Step 1: set SPF back to the single value in spf.before.txt. Step 2: set _dmarc to v=DMARC1; p=none; rua=mailto:dmarc-agg@example.com; rather than deleting it. Step 3: restore the prior DKIM selector TXT from dkim.before.txt and point the provider back to that selector. Step 4: in the MU plugin, comment out the forced Sender line if it conflicts with the ESP Return-Path, then wp cache flush --path=/var/www/example.com/public. Step 5: re-send the three seeded messages and confirm Authentication-Results returns to the last known-good state. Step 6: keep TTL at 300 for 24 hours, then restore 3600 and append the incident to ~/dns-change-2026/example.com/notes.md.

Two pitfalls make rollbacks fail: DNS provider UIs that wrap long TXT values into multiple quoted chunks (valid, but copying only one chunk truncates the DKIM key), and stale CDN/API caches that keep serving the old zone for a few minutes. Always verify against the authoritative NS with dig @$NS, and always confirm the full key with dig +short TXT selector._domainkey.example.com | tr -d '"' | wc -c before declaring recovery.

Frequently asked questions

Can I have two SPF records while migrating providers?

No. Receivers return permerror when more than one v=spf1 TXT exists. Merge all include: and ip4: mechanisms into one record, keep it under 10 DNS lookups, and remove the old row in the same change window.

Why does DKIM pass but DMARC still fail?

DMARC needs the DKIM d= domain or the SPF envelope domain to align with the visible From: domain. If the ESP signs as d=send.provider.net, add a custom Return-Path / MAIL FROM subdomain or enable a branded DKIM key on your own domain.

How long should I stay on p=none in 2026?

Stay 7-14 days, or one full billing/invoice cycle for stores, until aggregate reports show 100% aligned volume and zero unknown senders. Then move to p=quarantine; pct=25, step to 100, and finally p=reject.

Action checklist

  • Snapshot SPF, DKIM, DMARC and MX TXT values and lower TTL to 300.
  • Publish one SPF record, provider DKIM selectors and p=none DMARC with rua.
  • Send seeded tests to three mailbox families and confirm spf=pass dkim=pass dmarc=pass.
  • Separate 4xx greylisting from 5xx policy rejects; correlate every bounce by Message-ID.
  • Enforce gradually, then document the six-step rollback runbook with the restored values.

Need this audited on a live store? Our engineers handle SPF DKIM DMARC setup, mailer consolidation and bounce triage end to end. See Custom WordPress Development or browse all services.

References & further reading

admin

Senior WordPress developer building custom themes, plugins and WooCommerce solutions for global clients.

Need WordPress Development?

Free proposal and quote within 24 hours — we serve clients worldwide.

QQ