node.jsimapnode-imap

Node Imap - close the opened box?


I am using node-imap to connect to an email server. I have around 20 users for which I open a connection to the server and listen for new emails.

The problem is that in order to be able to do:

imap.once('mail', function(numNewMsgs) {
  console.log('');
});

It's required that you have an open box. So, I always open a box, which I never close. My flow is as follows:

function search(imap) {
  imap.openBox('INBOX', true, (err, box) => {

    imap.once('mail', function() {
      search(imap);
    });
    imap.search(someConditions, function(err, results) {
      //do some stuff, fetch or whatever
    })
  })

}

As it can be seen, I am calling the search function from within itself when the mail event fires. And I then open a box, which I hadn't closed the time before.

So, finally the question: is this troublesome for some reason? If it is and I should close the box, how would I go about using the "mail" event?

@mscdex hope you see this.


Solution

  • The IMAP RFC specifies that whenever you open another mailbox when there is one already open, an implicit close takes place on the previous mailbox before opening the new mailbox. If there is an error while opening the new mailbox, then no mailbox will be open (since the previous mailbox is still closed).