aaronbriel
aaronbriel

Reputation: 301

Waiting for email to arrive using node-imap

I'm using node-imap as a mail solution, but I need a way of waiting until an email arrives. In this post, someone referenced using the IMAP IDLE command to do this. I was wondering if anybody has had success with this, and how you would suggest incorporating that into the example code provided in the node-imap readme?

Upvotes: 3

Views: 4160

Answers (4)

magolexa
magolexa

Reputation: 21

You need to open a mailbox, which you want to listen for new messages. Here an example:

function listerBox() {
  imap.once("error", console.error);
  imap.on("ready", () => {
    imap.openBox("INBOX", true, (error, box) => {
      if (error) throw error;
      
      console.log('Connected!')

      imap.on("mail",  () => {
        console.log("New one!")
      })
    });
  });

  imap.connect();
}

listerBox()

Upvotes: 2

Peeranut Chindanonda
Peeranut Chindanonda

Reputation: 96

For node-imap, "mail" event will be emitted when new mail arrives in the currently open mailbox.

You can listen to new mail event like this:

imap.on('mail', function(numNewMsgs) {
    // Fetch new mail
});

Upvotes: 1

aaronbriel
aaronbriel

Reputation: 301

I decided to go with the inbox module. This provides a clear and quick solution through use of the call, client.on("new", function(message){.

Upvotes: 6

vodolaz095
vodolaz095

Reputation: 6986

I think good starting point is to research how is this method created in https://www.npmjs.com/package/inbox#wait-for-new-messages module.

Looks like this code emits the event of new. As far as i understood the code of this module, they call the fetch command with intervals

Upvotes: 2

Related Questions