I tried to fetch all the emails in the email body, but it failed to fetch those emails that are forwarded and have a spam popup which appears in the inbox.
If I send that email to another address, it gets fetched and is visible in the console, but only the mail that I sent is fetched successfully. The rest of the emails are fetched without any issues.
The only problem is with the spam popup. To clarify, the emails with the spam popup are not present in the spam folder; they are visible in the inbox.
const Imap = require("imap");
const { simpleParser } = require("mailparser");
const { readEmailsFromExcel } = require("../utils/excelReader");
const fs = require("fs");
const { Buffer } = require("buffer");
const quotedPrintable = require("quoted-printable");
let inspect = require("util").inspect;
const path = require("path");
const htmlToText = require("html-to-text");
const axios = require("axios");
const { extractFull } = require("node-7z"); // for extracting ZIP with password
const pathTo7zip = require("7zip-bin").path7za; // Path to 7-Zip binary
const fetchEmailsFromMultipleAccounts = async (specificDate) => {
const emailAccountsPath = path.join(__dirname, "../data/emailAccounts.xlsx");
const emailAccounts = readEmailsFromExcel(emailAccountsPath);
for (const account of emailAccounts) {
const { Email, Password } = account;
if (!Email || !Password) continue;
const imapConfig = {
user: Email,
password: Password,
host: process.env.IMAP_HOST || "imap.gmail.com",
port: process.env.IMAP_PORT || 993,
tls: true,
tlsOptions: { rejectUnauthorized: false },
};
const imap = new Imap(imapConfig);
try {
await connectToImap(imap);
const mailboxes = await listMailboxes(imap);
for (const mailbox of mailboxes) {
try {
const emails = await fetchEmailsFromMailbox(imap, mailbox, specificDate);
emails.forEach((email) => displayEmail(email));
} catch (mailboxError) {
console.error(`Error fetching emails from mailbox ${mailbox} for ${Email}:`, mailboxError);
}
}
} catch (error) {
console.error(`Error fetching emails for ${Email}:`, error);
} finally {
imap.end();
}
}
console.log("Email fetching complete.");
};
const connectToImap = (imap) => {
return new Promise((resolve, reject) => {
imap.once("ready", () => resolve());
imap.once("error", (err) => reject(err));
imap.connect();
});
};
const listMailboxes = (imap) => {
return new Promise((resolve, reject) => {
imap.getBoxes((err, boxes) => {
if (err) return reject(err);
const mailboxList = Object.keys(boxes);
resolve(mailboxList);
});
});
};
const fetchEmailsFromMailbox = (imap, mailbox, specificDate) => {
return new Promise((resolve, reject) => {
imap.openBox(mailbox, true, (err) => {
if (err) return reject(err);
const dateCriteria = specificDate
? ["SINCE", specificDate.toISOString().split("T")[0]]
: ["ALL"];
imap.search([dateCriteria], (err, results) => {
if (err) return reject(err);
if (!results || results.length === 0) return resolve([]);
const fetch = imap.fetch(results, { bodies: "", struct: true, envelope:true });
const emailDataArray = [];
fetch.on("message", (msg) => {
msg.on("body", (stream) => {
simpleParser(stream, async (err, parsed) => {
if (err) return console.error("Error parsing email:", err);
let body = parsed.text || (parsed.html ? htmlToText.convert(parsed.html) : "No Body Text");
if (parsed.attachments && parsed.attachments.length > 0) {
saveAttachments(parsed.attachments);
}
try {
const downloadedUrls = await extractAndDownloadUrls(body);
console.log("Downloaded URLs:", downloadedUrls);
} catch (err) {
console.error("Error extracting and downloading URLs:", err);
}
emailDataArray.push({
from: parsed.from?.text || "Unknown Sender",
to: parsed.to?.text || "Unknown Recipient",
subject: parsed.subject || "No Subject",
body,
attachments: parsed.attachments || [],
date: parsed.date ? parsed.date.toString() : "No Date",
});
});
});
});
fetch.on("end", () => resolve(emailDataArray));
fetch.on("error", (err) => reject(err));
});
});
});
};