0
votes

I have written a wrapper over node-imap(https://github.com/mscdex/node-imap). Currently I am using the since flag to search for emails that arrive in a particular Inbox. For each email that I listen I call upon a imap.search() method and then imap.fetch() method. Is it possible to directly fetch the emails without the imap.search() method. Providing snippets from my current code.

self.imap.on("mail", function(id) {
 self.parseUnreadEmails(time)
});

MailListener.prototype.parseUnreadEmails = function(time) {
var self = this;

 self.imap.search([["SINCE", time]], function(error, searchResults) {
     var fetch;
     if (error) {
         self.emit("error", error);
     } else {
         fetch = self.imap.fetch(searchResults, {
             bodies: '',
             markSeen: true
         })
     }
     //do some action       

 }
}
1

1 Answers

0
votes

If you are looking to monitor and fetch new mails arriving for a particular mailbox there are many ways you can do this, below are some ideas that are effective that can be used.

  1. Using IDLE command - yes, using this command you can put a folder in idle state and watch for EXISTS/EXPUNGE responses whenever new mails arrive or if there are any flag changes. Do read this RFC . Once constraint using IDLE command is that you have to have a dedicated tcp connection for this for a particular folder, multiple folders can not be monitored in single IDLE command.
  2. Polling STATUS (UIDNEXT MESSAGECOUNT) command frequently on reasonable interval. This will tell you the uidnext(if there is any change from previous value you should find the difference and fetch those mails).Before proceeding read the IMAP RFC carefully.