← back to Claude Mail

sent-audit.js

22 lines

'use strict';
const { ImapFlow } = require('imapflow');
const { simpleParser } = require('mailparser');
const { cfg } = require('./lib');
(async () => {
  const c = new ImapFlow({ host: cfg.imapHost, port: cfg.imapPort, secure: true,
    auth: { user: cfg.user, pass: cfg.pass }, logger: false });
  await c.connect();
  for (const box of ['Sent','INBOX.Sent']) {
    let lock; try { lock = await c.getMailboxLock(box); } catch { continue; }
    try {
      const all = await c.search({ all: true });
      console.log(`\n===== ${box}: ${all.length} msgs =====`);
      for await (const m of c.fetch(all.slice(-12), { uid:true, source:true })) {
        const p = await simpleParser(m.source);
        console.log(`\n--- to=${p.to?.text} | ${p.date?.toISOString?.()||''}\n  subj: ${p.subject}\n  body: ${(p.text||'').replace(/\s+/g,' ').trim().slice(0,400)}`);
      }
    } finally { lock.release(); }
  }
  await c.logout().catch(()=>{});
})().catch(e=>{console.error('SENT AUDIT FAILED:',e.message);process.exit(1);});