emailjs / emailjs-imap-client

Low-level JS IMAP client for all your IMAP needs.
MIT License
553 stars 122 forks source link

Amount of emails currently in a folder and its size in kb recursively #179

Closed CodeCook83 closed 6 years ago

CodeCook83 commented 6 years ago

Hi, it would be great if you could help me with the question on how to extend the mailboxes-object by the amount of emails currently in a folder and its size in kb recursively, when I go over the "selectFolder" route?

https://github.com/CodeCook83/offhaul/blob/c6e797c00b2072326e5c4d746e1e66ada237315c/controller/migration.js#L111

My first approach is as follows:

client.listMailboxes().then((mailboxes) => {
 if (mailboxes.children.length > 0) {
   mailboxes.children.forEach(mb => {
     client.selectMailbox(mb.name).then(mailbox => {
       const mailboxInfo = mailbox;
       res.render('migration/selectFolder', {
         title: title,
         children: mailboxes.children
       });
     })
   })
 }
});

Your help is greatly appreciated.

felixhammerl commented 6 years ago

Sorry for taking so long on this, have been stretched really thin over the last couple of weeks. this would require you to list the entire directory and then sum up the individual messages. this can take a very long time, as i have folders that easily exceed 10000 messages.

Anyway, fetching RFC822.SIZE should do the trick:

        return imap.listMessages('inbox', '1:*', ['RFC822.SIZE']).then((messages) => {
          console.log(messages)
          expect(messages).to.not.be.empty
        })

Output:

[ { '#': 1, 'rfc822.size': 28 },
  { '#': 2, 'rfc822.size': 28 },
  { '#': 3, 'rfc822.size': 28 },
  { '#': 4, 'rfc822.size': 170 },
  { '#': 5, 'rfc822.size': 28 },
  { '#': 6, 'rfc822.size': 28 },
  { '#': 7, 'rfc822.size': 28 } ]