← back to Claude Mail

inbox-audit.js

25 lines

'use strict';
// READ-ONLY: list all INBOX messages with uid, \Seen flag, from, subject, date, body snippet.
const { ImapFlow } = require('imapflow');
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();
  const lock = await c.getMailboxLock('INBOX');
  try {
    const all = await c.search({ all: true });
    console.log(`INBOX total: ${all.length}`);
    for await (const msg of c.fetch(all, { uid: true, flags: true, envelope: true, bodyStructure: false, source: true })) {
      const seen = msg.flags && msg.flags.has('\\Seen') ? 'SEEN ' : 'UNSEEN';
      const from = msg.envelope?.from?.[0]?.address || '?';
      const subj = msg.envelope?.subject || '(no subj)';
      const date = msg.envelope?.date ? new Date(msg.envelope.date).toISOString() : '?';
      // crude body extract
      let body = '';
      try { const { simpleParser } = require('mailparser'); const p = await simpleParser(msg.source); body = (p.text||'').replace(/\s+/g,' ').trim().slice(0,200); } catch {}
      console.log(`\n[uid ${msg.uid}] ${seen} | ${date}\n  from: ${from}\n  subj: ${subj}\n  body: ${body}`);
    }
  } finally { lock.release(); await c.logout().catch(()=>{}); }
})().catch(e => { console.error('AUDIT FAILED:', e.message); process.exit(1); });