NeahFront9/lib/email-parser.ts

23 lines
678 B
TypeScript

export function parseEmailHeaders(emailBody: string): { from: string; subject: string; date: string } {
const headers: { [key: string]: string } = {};
// Split the email body into lines
const lines = emailBody.split('\r\n');
// Parse headers
for (const line of lines) {
if (line.trim() === '') break; // Stop at the first empty line (end of headers)
const match = line.match(/^([^:]+):\s*(.+)$/);
if (match) {
const [, key, value] = match;
headers[key.toLowerCase()] = value;
}
}
return {
from: headers['from'] || '',
subject: headers['subject'] || '',
date: headers['date'] || new Date().toISOString()
};
}