/** * Parse List-Unsubscribe URLs from raw email headers. * * Handles RFC 2822 header folding (continuation lines) and extracts * all angle-bracket-delimited URIs from the List-Unsubscribe header. */ export function parseListUnsubscribe(rawHeaders: string): string[] | null { // Unfold continuation lines (RFC 2822: CRLF followed by whitespace) const unfolded = rawHeaders.replace(/\r?\n[ \t]+/g, " "); const match = unfolded.match(/^List-Unsubscribe:\s*(.+)$/im); if (!match) return null; const urls = [...match[1].matchAll(/<([^>]+)>/g)].map((m) => m[1]); return urls.length > 0 ? urls : null; }