node-imap icon indicating copy to clipboard operation
node-imap copied to clipboard

Fetch email during antivirus scanning fetches empty body

Open mirkobertone opened this issue 2 years ago • 2 comments

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: "[email protected]",
    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();
}

mirkobertone avatar Sep 07 '23 14:09 mirkobertone

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

nnew2 avatar Nov 16 '23 13:11 nnew2

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.

ValerioCietto avatar May 09 '24 12:05 ValerioCietto