Since February 2024, Gmail and Yahoo require bulk senders to publish SPF, DKIM and a DMARC record. Plenty of domains still have gaps: no DMARC at all, a p=none policy nobody ever tightened, or an SPF record that quietly blew past the 10-DNS-lookup limit after one more SaaS tool was added with include:.

If you look after more than a handful of domains (clients, brands, old marketing domains), checking them one by one in a web UI gets old. Here is a ~70-line, standard-library-only Python script that audits a list of domains in one go.

What it checks

  • SPF exists, and only once. Two v=spf1 records is a permerror; receivers treat it as no SPF.
  • How SPF ends. -all (fail) and ~all (softfail) are fine with DMARC; +all lets anyone send as you.
  • SPF DNS lookups. RFC 7208 caps the mechanisms that trigger DNS queries (include, a, mx, ptr, exists, redirect) at 10, counted recursively through every include. Go over and SPF fails with a permerror.
  • DMARC policy. p=none only monitors; quarantine or reject actually protects the domain.
  • DMARC reports. Without rua= you never see who is sending as you.

It doesn't check DKIM: DKIM keys live at <selector>._domainkey.<domain>, and the selector isn't discoverable from DNS alone.

No DNS library needed

The script uses Cloudflare's DNS-over-HTTPS JSON API (https://cloudflare-dns.com/dns-query with accept: application/dns-json), so it's just urllib, with no dnspython and no local resolver quirks. Long TXT records come back as several quoted chunks, which the txt() helper joins.

import json, sys, urllib.parse, urllib.request DOH = "https://cloudflare-dns.com/dns-query" def txt(name): """Return the TXT strings for a name via DNS over HTTPS (Cloudflare JSON API).""" url = DOH + "?" + urllib.parse.urlencode({"name": name, "type": "TXT"}) req = urllib.request.Request(url, headers={"accept": "application/dns-json"}) with urllib.request.urlopen(req, timeout=10) as r: data = json.load(r) out = [] for a in data.get("Answer", []): if a.get("type") == 16: # TXT # long records arrive as several quoted chunks: "abc" "def" out.append("".join(p.strip('"') for p in a["data"].split('" "'))) return out def spf_lookups(record, depth=0, seen=None): """Count DNS-querying mechanisms (RFC 7208 limit: 10), following include/redirect.""" seen = seen if seen is not None else set() count = 0 for term in record.split()[1:]: t = term.lstrip("+-~?").lower() if t.startswith(("include:", "redirect=")): count += 1 target = t.split(":", 1)[1] if t.startswith("include:") else t.split("=", 1)[1] if target in seen or depth > 10: continue seen.add(target) child = next((r for r in txt(target) if r.lower().startswith("v=spf1")), None) if child: count += spf_lookups(child, depth + 1, seen) elif t in ("a", "mx", "ptr") or t.startswith(("a:", "mx:", "ptr:", "exists:", "a/", "mx/")): count += 1 return count def audit(domain): row = {"domain": domain, "problems": []} spf = [r for r in txt(domain) if r.lower().startswith("v=spf1")] if not spf: row["problems"].append("no SPF record") elif len(spf) > 1: row["problems"].append("multiple SPF records (permerror)") else: s = spf[0] row["spf_all"] = (s.split()[-1] if s.split()[-1].lstrip("+-~?") == "all" else "none") row["spf_lookups"] = spf_lookups(s) if row["spf_all"] in ("+all", "all"): row["problems"].append("SPF ends in +all (anyone may send)") if row["spf_lookups"] > 10: row["problems"].append(f"SPF needs {row['spf_lookups']} DNS lookups (limit 10)") dmarc = [r for r in txt("_dmarc." + domain) if r.lower().startswith("v=dmarc1")] if not dmarc: row["problems"].append("no DMARC record") else: tags = dict(kv.strip().split("=", 1) for kv in dmarc[0].split(";") if "=" in kv) tags = {k.strip().lower(): v.strip() for k, v in tags.items()} row["dmarc_p"] = tags.get("p", "?") row["dmarc_rua"] = "rua" in tags if row["dmarc_p"] == "none": row["problems"].append("DMARC p=none (monitor only)") if not row["dmarc_rua"]: row["problems"].append("DMARC has no rua= (no reports)") return row if __name__ == "__main__": for d in sys.argv[1:] or [l.strip() for l in sys.stdin if l.strip()]: try: r = audit(d.lower()) except Exception as e: r = {"domain": d, "problems": [f"lookup failed: {e}"]} print(f"{r['domain']:<22} SPF {r.get('spf_all','-'):<5} lookups {str(r.get('spf_lookups','-')):<3} " f"DMARC p={r.get('dmarc_p','-'):<11} -> {'; '.join(r['problems']) or 'OK'}") 

Running it

python3 spf_dmarc_audit.py github.com example.com wikipedia.org stripe.com nonexistent-domain-xyz-123.com # or: python3 spf_dmarc_audit.py < domains.txt 

Real output from a run today:

github.com SPF ~all lookups 10 DMARC p=quarantine -> OK example.com SPF -all lookups 0 DMARC p=reject -> DMARC has no rua= (no reports) wikipedia.org SPF ~all lookups 1 DMARC p=reject -> OK stripe.com SPF ~all lookups 7 DMARC p=reject -> OK nonexistent-domain-xyz-123.com SPF - lookups - DMARC p=- -> no SPF record; no DMARC record 

Note github.com sitting exactly at 10 lookups: one more include: and its SPF would start failing. That is the kind of thing this audit is for. (example.com is a reserved domain that sends no mail, so -all plus p=reject without reports is a deliberate "never accept mail from us".)

Limits

  • The lookup count is an approximation of what a receiver does: it follows include: and redirect= and counts a/mx/ptr/exists, but doesn't count the extra lookups mx itself can trigger, or void lookups.
  • DMARC is read from _dmarc.<domain> only. It doesn't fall back to the organisational domain for subdomains.
  • Cloudflare's resolver is the only source; if you need answers from your own resolver, swap the DOH URL for another DoH endpoint that speaks the JSON format (Google's https://dns.google/resolve does).

Want a one-off check without running code?

The free Domain WHOIS & DNS lookup on siftwright.com runs the same SPF and DMARC health check, plus registrar, age, expiry and DNSSEC from RDAP, with no account. For bulk domain age/expiry/DNS audits there is also the Domain WHOIS & DNS Lookup Actor on the Apify Store, billed per domain, which AI agents can call through Apify's MCP server.