'How to get data of new mail message in node-imap
I need to get information of new mail message in new mail message event. Specifically:
- E-mail address which sended mail
- Message title
- Message content (text)
My code at this moment:
let Imap = require('node-imap')
let imap = new Imap({
user: '***@gmail.com',
password: '***',
host: 'imap.gmail.com',
port: 993,
tls: true
});
imap.on('ready', () => {
imap.openBox('INBOX', true, (err, box) => {
if (err) console.log(err)
else console.log('Recipient is ready for accepting')
})
})
imap.on('mail', mail => {
const title = ?
const subject = ?
const email = ?
console.log(`Email author: ${email} Title: ${title}, Description: ${subject}`)
})
imap.connect()
Solution 1:[1]
imap.on('mail', ...)
did not provide informations of new mails. Only a number of new mails.
mail(< integer >numNewMsgs) - Emitted when new mail arrives in the currently open mailbox.
To get the mail data use imap.seq.fetch
or imap.search
.
You can find some examples here: https://github.com/mscdex/node-imap#connection-events
Maybe this helps (https://github.com/mscdex/node-imap/issues/359#issuecomment-37099842):
imap.search([ 'NEW' ], function(err, results) {
if (err || !results.length) return imap.end();
// fetch all resulting messages
imap.fetch(results, ....);
// or just fetch only the first result... which may not necessarily be newest
// because the server can return results in any order
imap.fetch(results[0], ...);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Janneck Lange |