const Imap = require('imap'); const { simpleParser } = require('mailparser'); const imap = new Imap({ user: 'cococlawdy@gmail.com', password: 'wvfi htti cusm ypal', host: 'imap.gmail.com', port: 993, tls: true, tlsOptions: { rejectUnauthorized: false } }); function openInbox(cb) { imap.openBox('INBOX', true, cb); } imap.once('ready', function() { openInbox(function(err, box) { if (err) throw err; // Search for recent unread emails imap.search(['UNSEEN', ['SINCE', new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)]], function(err, results) { if (err) throw err; if (!results || !results.length) { console.log('No unread messages found'); imap.end(); return; } console.log(`Found ${results.length} unread messages`); const f = imap.fetch(results, { bodies: '' }); f.on('message', function(msg, seqno) { msg.on('body', function(stream, info) { simpleParser(stream, async (err, parsed) => { if (err) throw err; console.log('\n--- Email', seqno, '---'); console.log('From:', parsed.from.text); console.log('Subject:', parsed.subject); console.log('Date:', parsed.date); console.log('Text:', parsed.text?.substring(0, 500)); if (parsed.attachments && parsed.attachments.length > 0) { console.log('Attachments:', parsed.attachments.map(a => a.filename).join(', ')); } }); }); }); f.once('end', function() { console.log('\nDone fetching messages'); imap.end(); }); }); }); }); imap.once('error', function(err) { console.error('IMAP Error:', err); }); imap.once('end', function() { console.log('Connection ended'); }); imap.connect();