← back to Claude Mail

thread-dump.js

37 lines

'use strict';
// READ-ONLY: dump recent INBOX messages + list folders (to find Sent). No writes.
const { ImapFlow } = require('imapflow');
const { simpleParser } = require('mailparser');
const { cfg } = require('./lib');

(async () => {
  const client = new ImapFlow({
    host: cfg.imapHost, port: cfg.imapPort, secure: true,
    auth: { user: cfg.user, pass: cfg.pass }, logger: false,
  });
  await client.connect();
  console.log('=== mailboxes ===');
  const boxes = await client.list();
  for (const mb of boxes) console.log('  ', mb.path);

  const dump = async (box) => {
    let lock;
    try { lock = await client.getMailboxLock(box); } catch { console.log(`\n(no ${box})`); return; }
    try {
      console.log(`\n=== ${box} (most recent 6) ===`);
      const all = await client.search({ all: true });
      const last = (all || []).slice(-6);
      for (const uid of last) {
        const { content } = await client.download(uid, undefined, { uid: true });
        const p = await simpleParser(content);
        const body = (p.text || '').replace(/\s+\n/g, '\n').trim().slice(0, 600);
        console.log(`\n--- uid ${uid} | from=${p.from?.text} | to=${p.to?.text}\n    subj=${p.subject}\n${body}`);
      }
    } finally { lock.release(); }
  };

  await dump('INBOX');
  for (const s of ['Sent', 'INBOX.Sent', 'Sent Items']) { await dump(s); }
  await client.logout().catch(() => {});
})().catch((e) => { console.error('DUMP FAILED:', e.message); process.exit(1); });