Trying to delete email in node.js using node-imap module.
I open the INBOX in read/write mode:
imap.openBox('INBOX', false, cb);
I then fetch all the messages:
var f = imap.seq.fetch("1:"+box.messages.total, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)','TEXT'],
struct: true
});
I flag the mail to be deleted:
msg.on('end', function() {
imap.seq.addFlags(seqno, '\\Deleted', function(err) { } );
});
I close the mailbox with autopurge set to true
imap.closeBox(true);
But this doesn't work. What am I doing wrong?
imap.setFlags()
works with UIDs. imap.seq.setFlags()
works with sequence numbers. Since it seems like you're trying to pass a sequence number, you should use the latter function instead.
const mailBox = new imap({
host: "imap.gmail.com",
password: GMAIL_PASSWORD,
port: 993,
tls: true,
user: GMAIL
});
mailBox.once("error", console.error);
mailBox.once("ready", () => {
mailBox.openBox("INBOX", false, (error, box) => {
if (error) throw error;
mailBox.search(
[
"UNSEEN",
["SUBJECT", "xxx"],
["FROM", "no-reply@xxx.xxx"]
],
(error, results) => {
if (error) throw error;
for (const uid of results) {
const mails = mailBox.fetch(uid, {
bodies: ""
// markSeen: true
});
mails.once("end", () => mailBox.end());
mails.on("message", (message, seq) => {
message.on("body", stream => {
let buffer = "";
stream.on("data", chunk => (buffer += chunk.toString("utf8")));
stream.once("end", () => mailBox.addFlags(uid, "Deleted"));
});
});
}
}
);
});
});
mailBox.connect();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.