Back to cheatsheets
Web Security
Email Header Analysis
Parse raw email headers, trace sender path, and verify SPF, DKIM, and DMARC authentication results.
What you'll learn
- Parse Received headers
- Extract auth results
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
- Try the interactive related tool to see the concept in action.
Code Snippets
Copy-paste code snippets
2 ready-to-use snippets for email header analysis. Click copy and paste into your project.
Parse Received headers
function parseReceivedHeaders(rawHeaders) {
const lines = rawHeaders.split('\n');
const received = [];
let current = '';
for (const line of lines) {
if (line.startsWith('Received:')) {
if (current) received.push(current);
current = line.slice(9).trim();
} else if (/^\s/.test(line) && current) {
current += ' ' + line.trim();
}
}
if (current) received.push(current);
return received; // newest first
}
Extract auth results
function extractAuthResults(headers) {
const authHeader = headers.find(h =>
h.startsWith('Authentication-Results:')
);
if (!authHeader) return null;
const results = { spf: null, dkim: null, dmarc: null };
if (/spf=pass/i.test(authHeader)) results.spf = 'pass';
if (/dkim=pass/i.test(authHeader)) results.dkim = 'pass';
if (/dmarc=pass/i.test(authHeader)) results.dmarc = 'pass';
return results;
}
Tool
Related ShieldX tool
Try this concept hands-on with the interactive tool.
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site