Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch email during antivirus scanning fetches empty body #907

Open
mirkobertone opened this issue Sep 7, 2023 · 2 comments
Open

Fetch email during antivirus scanning fetches empty body #907

mirkobertone opened this issue Sep 7, 2023 · 2 comments

Comments

@mirkobertone
Copy link

mirkobertone commented Sep 7, 2023

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();
}
@nnew2
Copy link

nnew2 commented Nov 16, 2023

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

@ValerioCietto
Copy link

ValerioCietto commented May 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants