""" Looks up a domain's email records (SPF, DKIM, DMARC, MX) so Settings > Advanced can say what is already set up instead of only describing it. Uses a public DNS-over-HTTPS resolver, so it needs no extra library. DKIM records live under a "selector" name chosen by the mail provider; there is no way to list them, so the common selectors are tried and "not found" only means none of those exist. """ from __future__ import annotations import json import re import urllib.parse import urllib.request _RESOLVER = "https://dns.google/resolve" _DOMAIN = re.compile(r"^(?=.{3,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$") DKIM_SELECTORS = ("default", "google", "selector1", "selector2", "k1", "k2", "mail", "dkim", "s1", "s2", "x", "zoho", "mx") def _lookup(name: str, rtype: str) -> list[str]: url = f"{_RESOLVER}?{urllib.parse.urlencode({'name': name, 'type': rtype})}" with urllib.request.urlopen(url, timeout=6) as resp: # noqa: S310 — fixed https resolver data = json.load(resp) return [a["data"].strip('"').replace('" "', "") for a in data.get("Answer", []) if a.get("type") in (1, 5, 15, 16)] def check_domain(domain: str, lookup=_lookup) -> dict: """{domain, spf, dkim, dmarc, mx}. Each item is {ok, value, note}; ok is None when the lookup itself failed (no answer either way).""" domain = (domain or "").strip().lower() if not _DOMAIN.match(domain): raise ValueError("Enter a domain like yourcompany.com") def safe(name: str, rtype: str) -> list[str] | None: try: return lookup(name, rtype) except Exception: # noqa: BLE001 — network trouble is reported, not raised return None result: dict = {"domain": domain} txt = safe(domain, "TXT") spf = [t for t in txt or [] if t.lower().startswith("v=spf1")] result["spf"] = ( {"ok": None, "value": "", "note": "We couldn't look this up. Try again."} if txt is None else {"ok": len(spf) == 1, "value": spf[0] if spf else "", "note": "Found." if len(spf) == 1 else "More than one SPF record. Mail servers need exactly one." if spf else "No SPF record."} ) dmarc_txt = safe(f"_dmarc.{domain}", "TXT") dmarc = [t for t in dmarc_txt or [] if t.lower().startswith("v=dmarc1")] result["dmarc"] = ( {"ok": None, "value": "", "note": "We couldn't look this up. Try again."} if dmarc_txt is None else {"ok": bool(dmarc), "value": dmarc[0] if dmarc else "", "note": "Found." if dmarc else "No DMARC record."} ) found = None for selector in DKIM_SELECTORS: records = safe(f"{selector}._domainkey.{domain}", "TXT") or [] if any("p=" in r for r in records): found = selector break result["dkim"] = ( {"ok": True, "value": f"{found}._domainkey", "note": f"Found under the name {found}."} if found else {"ok": False, "value": "", "note": "Not found under the usual names. Your provider may use another name; check with them."} ) mx = safe(domain, "MX") result["mx"] = ( {"ok": None, "value": "", "note": "We couldn't look this up. Try again."} if mx is None else {"ok": bool(mx), "value": ", ".join(mx[:3]), "note": "Found." if mx else "No mail servers listed, so replies can't arrive."} ) return result