Reputation: 1
I am using the node-imap NPM module and am fetching all emails from my inbox. It works perfectly when a new email is added, however when I delete an email from my mail client (Mac mail) and refresh the code, the node-imap module is still pulling this email in the inbox. For example, if I have an @icloud.com email and I delete an email in the inbox on my Mac mail client, it is removed from the inbox when I go to www.icloud.com, but when I load my emails through the node-imap module, the email is still there.
Please see below code.
router.get('/get-imap-emails', (req, res) => {
const { email, password, incomingHost, port, tls, accountType } = req.query;
let emailMessages = []
var imap = new Imap({
user: email,
password: password,
host: incomingHost,
port: port,
tls: tls
});
openInbox = cb => {
imap.openBox('INBOX', true, cb);
}
imap.once('ready', () => {
openInbox((err, box) => {
if (err) throw err;
getEmails(box.messages.total);
});
});
imap.once('error', err => {
console.log(err);
});
// Custom functions
getEmails = messageCount => {
imap.search(['ALL'], (err, results) => {
if (err) {
console.log(err)
} else {
imap.fetch(results, {
bodies: '',
struct: true
}).on('message', (message) => {
message.on('body', (stream) => {
// Code to process email with mailparser
});
}).once('error', error => console.log(error)).once('end', () => {
imap.end()
res.status(200).send()
});
}
});
}
imap.connect();
});
Upvotes: 0
Views: 429