// simple-emailtojson.mjs
import fs from 'fs/promises';
import { simpleParser } from 'mailparser';

if (process.argv.length < 3) {
  console.error('USAGE: node simple-emailtojson.mjs <emailfile.eml>');
  process.exit(1);
}

const mailpath = process.argv[2];

(async () => {
  const emlBuffer = await fs.readFile(mailpath);
  const mail      = await simpleParser(emlBuffer);

  // Optional: entferne Buffers, die du nicht serialisieren willst
  if (mail.attachments) {
    mail.attachments = mail.attachments.map(att => ({
      filename: att.filename,
      contentType: att.contentType,
      size: att.size,
      // evtl. att.content.toString('base64') oder weglassen
    }));
  }

  console.log(JSON.stringify(mail, null, 2));
})().catch(err => {
  console.error('Fehler beim Parsen:', err);
  process.exit(1);
});
