# Standard library only. Uses Cloudflare's public DNS-over-HTTPS JSON API, # so it also works on networks that block or rewrite plain DNS on port 53. import csv, json, sys, time, urllib.parse, urllib.request PROVIDERS = [ # MX host suffix -> provider label ("google.com", "Google Workspace"), ("googlemail.com", "Google Workspace"), ("mail.protection.outlook.com", "Microsoft 365"), ("pphosted.com", "Proofpoint"), ("ppe-hosted.com", "Proofpoint"), ("mimecast.com", "Mimecast"), ("zoho.com", "Zoho Mail"), ("zoho.eu", "Zoho Mail"), ("messagingengine.com", "Fastmail"), ("secureserver.net", "GoDaddy"), ("protonmail.ch", "Proton Mail"), ("barracudanetworks.com", "Barracuda"), ("iphmx.com", "Cisco Secure Email"), ] def mx_hosts(domain): url = "https://cloudflare-dns.com/dns-query?" + urllib.parse.urlencode({"name": domain, "type": "MX"}) req = urllib.request.Request(url, headers={"accept": "application/dns-json"}) data = json.load(urllib.request.urlopen(req, timeout=10)) if data.get("Status") == 3: return None # NXDOMAIN rows = [a["data"].split() for a in data.get("Answer", []) if a.get("type") == 15] return sorted((int(p), h.rstrip(".").lower()) for p, h in rows) def provider(domain): try: hosts = mx_hosts(domain) except Exception as e: return "", "lookup failed: " + type(e).__name__ if hosts is None: return "", "no such domain" if not hosts or hosts[0][1] == "": return "", "no MX record" for _, h in hosts: for suffix, label in PROVIDERS: if h == suffix or h.endswith("." + suffix): return hosts[0][1], label return hosts[0][1], "other / self-hosted" w = csv.writer(sys.stdout) w.writerow(["domain", "primary_mx", "provider"]) seen = set() for line in open(sys.argv[1]): d = line.strip().lower().removeprefix("https://").removeprefix("http://").split("/")[0].removeprefix("www.") if not d or d in seen: continue seen.add(d) w.writerow([d, *provider(d)]) time.sleep(0.2) # be polite to the public resolver