I am trying to implement, email notifier for outlook IMAP. As soon as the IMAP server receives an email, my nodejs code need to process that.
This is how I am listening to mail event.
imap.openBox('INBOX', false, function (err, box) {
if (err) {
console.log("Error oppening inbox");
} else {
imap.on('mail', function (numNewMsgs) {
console.log("messages has arrived");
fetchMessages(imap);
});
}
});
// This is the code to fetch new email which is unseen and has recent flag
function fetchMessages(imap) {
var query = imap.search(['UNSEEN', 'RECENT'], function (error, results) {
if (error) { console.log("ERROR imap.search"); throw err; }
if (results.length <= 0) return;
var fetchQuery = imap.fetch(results, { bodies: '', markSeen: true });
fetchQuery.on('message', function (msg, seqno) {
// parseMessage(msg, seqno);
console.log('New mail received.');
var uid, flags;
msg.once('attributes', function (attrs) {
uid = attrs.uid;
flags = attrs.flags;
});
var mp = new MailParser();
mp.once('end', function (mail) {
mail.uid = uid;
mail.flags = flags;
console.log("new email from mail parser");
//self.emit('mail', mail);
//self.dbg('found mail '+mail.headers["message-id"]);
});
msg.on('body', function (stream, info) {
stream.pipe(mp);
});
msg.once('end', function () {
// console.log(prefix + 'Finished');
});
});
fetchQuery.once('error', function (err) {
console.log('Fetch error: ' + err);
});
fetchQuery.once('end', function () {
console.log('Done fetching all messages');
imap.end();
});
});
}
My problem is, the first time when I start my node js server, I am able to receive the "mail" event, but after that, I see I don't get any mail notification. It requires me to restart the node js server.
Based on my speculation I found, fetch query "end" event never gets fired.
I tried IMAP.seq.fetch query example from the git hub which works fine, I see end event, but here with imap.fetch I don't get end event.
Note: I am testing this on Outlook IMAP. (Gmail IMAP is working fine for me.)
Please let me know what I am doing wrong. Thanks in advance.
I am trying to implement, email notifier for outlook IMAP. As soon as the IMAP server receives an email, my nodejs code need to process that.
This is how I am listening to mail event.
imap.openBox('INBOX', false, function (err, box) { if (err) { console.log("Error oppening inbox"); } else { imap.on('mail', function (numNewMsgs) { console.log("messages has arrived"); fetchMessages(imap); }); } });
// This is the code to fetch new email which is unseen and has recent flag function fetchMessages(imap) {
var query = imap.search(['UNSEEN', 'RECENT'], function (error, results) { if (error) { console.log("ERROR imap.search"); throw err; }
}); }
My problem is, the first time when I start my node js server, I am able to receive the "mail" event, but after that, I see I don't get any mail notification. It requires me to restart the node js server.
Based on my speculation I found, fetch query "end" event never gets fired.
I tried IMAP.seq.fetch query example from the git hub which works fine, I see end event, but here with imap.fetch I don't get end event.
Note: I am testing this on Outlook IMAP. (Gmail IMAP is working fine for me.) Please let me know what I am doing wrong. Thanks in advance.