Advertisement
orglee

Untitled

Feb 23rd, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require('dotenv').config();
  2. const config = require('./config');
  3.  
  4. const { ImapFlow } = require('imapflow');
  5. const client = new ImapFlow(config);
  6.  
  7. const main = async () => {
  8.     // Wait until client connects and authorizes
  9.     await client.connect();
  10.  
  11.     // Select and lock a mailbox. Throws if mailbox does not exist
  12.     let lock = await client.getMailboxLock('INBOX');
  13.     try {
  14.         let options = [
  15.             { unseen: true },
  16.             {
  17.                 envelope: true,
  18.                 flags: true,
  19.                 labels: true,
  20.                 headers: true
  21.             }
  22.         ];
  23.         for await (let message of client.fetch(...options)) {
  24.             let from = (message.envelope.from.map((address) => {
  25.                 return `${address.name} <${address.address}>`;
  26.             })).join(',');
  27.  
  28.             console.log(`${message.uid}: ${message.envelope.date} ${message.envelope.subject} "${from}"`);
  29.             console.trace(message);
  30.         }
  31.  
  32.     } finally {
  33.         // Make sure lock is released, otherwise next `getMailboxLock()` never returns
  34.         lock.release();
  35.     }
  36.  
  37.     await client.messageFlagsSet({all: true}, ['\Seen']);
  38.  
  39.     // log out and close connection
  40.     await client.logout();
  41. };
  42.  
  43. main().catch(err => console.error(err));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement