Picture the scene. It's Monday, someone in marketing forwards you a message from a customer: "Your invoice email went to my spam folder." You check your DMARC record. It's been sitting at p=none for eight months, faithfully sending reports to a mailbox nobody opens. That mailbox now holds 1,400 unread .xml.gz attachments. Congratulations, you have DMARC monitoring. You just have never looked at it.
The good news: DMARC aggregate reports are boring, well-specified XML, and boring XML is exactly what a 60-line Node.js script is good at. This post walks through a script that reads a folder of reports (plain, gzipped or zipped), adds them up per sending IP, and tells you which sources are failing DMARC, worst first.
What a DMARC aggregate report actually contains
Mailbox providers send aggregate reports to the address in the rua tag of your DMARC record. The format is defined in RFC 7489, Appendix C, and every report has the same shape:
<feedback> <report_metadata> <!-- who sent the report, report_id, date_range --> <policy_published> <!-- your record as they saw it: domain, adkim, aspf, p, sp, pct --> <record> <!-- one per sending source --> <row> <source_ip>198.51.100.7</source_ip> <count>14</count> <policy_evaluated> <disposition>none</disposition> <dkim>fail</dkim> <spf>fail</spf> </policy_evaluated> </row> <identifiers><header_from>example.com</header_from></identifiers> <auth_results> <!-- raw SPF and DKIM results, with the domains they checked --> </record> </feedback> Two details matter for monitoring:
-
policy_evaluatedis the DMARC verdict. It says whether DKIM and SPF passed and aligned with your From domain. A message passes DMARC if either one ispassthere.auth_resultscan show SPF "pass" for a vendor's own domain whilepolicy_evaluatedstill saysfail, because that pass wasn't aligned with yours. - Files arrive compressed. RFC 7489 (section 7.2.1.1) names them like
receiver!your-domain!begin!end.xml.gz, and providers use gzip or zip. Your script has to handle both.
Step 1: Collect the reports in one folder
Point your rua address at a mailbox you can export from, and drop the attachments into a reports/ folder. If you'd rather not read reports at all for now, paste one into a free DMARC report analyzer first to see what you're dealing with.
Step 2: Install two small dependencies
npm install fast-xml-parser adm-zip fast-xml-parser turns the XML into plain objects. adm-zip opens .zip reports. Node's built-in zlib handles .gz.
Step 3: The script
// dmarc-summary.js: summarize every DMARC aggregate report in a folder const fs = require("fs"); const path = require("path"); const zlib = require("zlib"); const AdmZip = require("adm-zip"); const { XMLParser } = require("fast-xml-parser"); const parser = new XMLParser({ ignoreAttributes: true, parseTagValue: false }); const asArray = (x) => (x === undefined ? [] : Array.isArray(x) ? x : [x]); function readXml(file) { const buf = fs.readFileSync(file); if (file.endsWith(".gz")) return zlib.gunzipSync(buf).toString("utf8"); if (file.endsWith(".zip")) { const entry = new AdmZip(buf).getEntries().find((e) => e.entryName.endsWith(".xml")); return entry ? entry.getData().toString("utf8") : ""; } return buf.toString("utf8"); } const totals = new Map(); // source_ip -> counts for (const name of fs.readdirSync(process.argv[2] || "reports")) { const file = path.join(process.argv[2] || "reports", name); const xml = readXml(file); if (!xml) continue; const feedback = parser.parse(xml).feedback; const reporter = feedback.report_metadata.org_name; for (const record of asArray(feedback.record)) { const { source_ip, count, policy_evaluated } = record.row; const aligned = policy_evaluated.dkim === "pass" || policy_evaluated.spf === "pass"; const t = totals.get(source_ip) || { messages: 0, failing: 0, reporters: new Set(), from: record.identifiers.header_from }; t.messages += Number(count); if (!aligned) t.failing += Number(count); t.reporters.add(reporter); totals.set(source_ip, t); } } const rows = [...totals.entries()] .map(([ip, t]) => ({ ip, from: t.from, messages: t.messages, failing: t.failing, reporters: [...t.reporters].join(", ") })) .sort((a, b) => b.failing - a.failing); console.table(rows); const failing = rows.reduce((n, r) => n + r.failing, 0); const all = rows.reduce((n, r) => n + r.messages, 0); console.log(`${failing} of ${all} messages failed DMARC (${all ? ((100 * failing) / all).toFixed(1) : 0}%)`); A few choices worth explaining:
-
parseTagValue: falsekeeps every value as a string. Without it, the parser may try to turn values that look like numbers into numbers, and you don't want surprises in IP addresses or report IDs. We convertcountwithNumber()ourselves. -
asArrayexists because a report with a single<record>parses to an object, not an array. Forgetting this is the classic bug in every DMARC parser ever written. - Aligned = DKIM pass OR SPF pass in
policy_evaluated, which is the DMARC rule.
Step 4: Run it
node dmarc-summary.js reports With one Google report and one gzipped Outlook report in the folder, the output looks like this (trimmed):
| ip | from | messages | failing | reporters | | 198.51.100.7 | example.com | 17 | 17 | outlook.com, google.com | | 203.0.113.10 | example.com | 120 | 0 | google.com | 17 of 137 messages failed DMARC (12.4%) (Those IPs are from the documentation ranges, so don't go looking them up.)
Step 5: Turn the table into decisions
The table sorts by failures, so the top rows are your to-do list:
- A failing IP you recognise (your CRM, helpdesk or invoicing tool): it's sending as your domain without aligned DKIM. Set up DKIM for your domain inside that tool, or send it from a subdomain.
- A failing IP you don't recognise, with small volumes: often forwarding or spoofing. This is exactly what
p=quarantineand laterp=rejectexist to stop, once your legitimate sources are all passing. - Everything at 0 failing for a few weeks: you're ready to tighten the policy, one step at a time.
Common mistakes
- Reading
auth_resultsinstead ofpolicy_evaluated. SPF "pass" forcrm-vendor.exampleis not a DMARC pass forexample.com. - Not handling single-record reports (see
asArray). - Moving to
p=rejecton a calendar rather than on data. Move when the failing list contains nothing you send yourself. - Never deleting old reports. Keep 30 to 90 days of history in the folder, archive the rest.
Where to take it next
Run it from a cron job once a week and post the summary to Slack, store the rows in SQLite to see trends, or add a column for auth_results DKIM domains so you can see which signature a failing source used.
Or skip the plumbing. If your app sends transactional email, Email Digit's transactional email API with managed DMARC starts your domain at p=none and offers the step up to quarantine and reject only when your alignment data shows it's safe. One POST to send, suppression checked on every send.
FAQ
How often do providers send DMARC aggregate reports?
Usually once a day per reporting provider, covering a 24-hour window in date_range.
Do I need DMARC monitoring if my policy is p=none?
Yes. p=none is the monitoring phase. The reports are the only reason to have it.
Why does SPF pass but DMARC fail?
Because SPF passed for a different domain (usually your vendor's bounce domain), so it isn't aligned with your From address.
Drafted with AI assistance and checked against RFC 7489 and a local test run. Email Digit builds an email platform with managed SPF, DKIM and DMARC. emaildigit.com