mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.14k stars 379 forks source link

Fetch email during antivirus scanning fetches empty body #907

Open mirkobertone opened 10 months ago

mirkobertone commented 10 months ago

Hello I have a job process that downloads email using node-imap. I have a kaspersky antivirus which scans email in mailbox in search of malware. While the antivirus does this scan, if node-imap fetch the email body, it returns with empty body. Is there some work around to deal with it?

This is my script:

function main() {
  const Imap = require("imap");
  const inspect = require("util").inspect;

  const imap = new Imap({
    user: "your-email@example.com",
    password: "your-password",
    host: "imap.example.com",
    port: 993,
    tls: true,
  });

  imap.once("ready", function () {
    imap.openBox("INBOX", true, function (err, box) {
      if (err) throw err;

      let fetched = 0;
      let total = box.messages.total;

      let fetch = imap.fetch(box.messages.total + ":*", {
        bodies: "HEADER.FIELDS (FROM TO SUBJECT DATE)",
      });
      fetch.on("message", function (msg, seqno) {
        console.log("Message #%d", seqno);
        let prefix = "(#" + seqno + ") ";
        msg.on("body", function (stream, info) {
          let buffer = "";
          stream.on("data", function (chunk) {
            buffer += chunk.toString("utf8");
          });
          stream.once("end", function () {
            console.log(
              prefix + "Parsed header: %s",
              inspect(Imap.parseHeader(buffer))
            );
            fetched++;
            if (fetched === total) {
              imap.end();
            }
          });
        });
        msg.once("attributes", function (attrs) {
          console.log(prefix + "Attributes: %s", inspect(attrs, false, 8));
        });
        msg.once("end", function () {
          console.log(prefix + "Finished");
        });
      });
      fetch.once("error", function (err) {
        console.log("Fetch error: " + err);
      });
      fetch.once("end", function () {
        console.log("Done fetching all messages!");
      });
    });
  });
  imap.once("error", function (err) {
    console.log(err);
  });
  imap.once("end", function () {
    console.log("Connection ended");
  });
  imap.connect();
}
nnew2 commented 7 months ago

We also have this issue, is there an update here?

ValerioCietto commented 1 month ago

Worked around the issue with SEARCH criteria HEADER Example imap.search(['UNSEEN','HEADER','ANTI-SPAM'], handleEmails()) Emails without the header are yet to be scanned so this prevent fetching them.