chadxz / imap-simple

Wrapper over node-imap, providing a simpler api for common use cases
MIT License
243 stars 80 forks source link

How to retrieve new mail content with onMail event ? #82

Closed Loufi1 closed 3 years ago

Loufi1 commented 4 years ago

Hello,

I'm looking for a way to retrieve the content of the mail who triggered the onMail event. Is there a way to do it ?

lacajanegra commented 3 years ago

You can use an event that allows you to handle the arrival of new emails.

imaps.connect(config).then(function (connection) {
    connection.on('mail', () => {
      const searchCriteria = [
        'UNSEEN'
      ];
      const fetchOptions = {
        bodies: ['HEADER', 'TEXT'],
        markSeen: false
      };
      connection.search(searchCriteria, fetchOptions).then(function (messages) {
        messages.forEach(function (item) {
          console.log('item', item)
        });
      });
    })
    return connection.openBox('INBOX')
  })