The Problem:
When I try to search for an email using Message-ID
, the search returns zero results.
Even though I explicitly set a Message-ID
while sending the email, it only appears inside text/rfc822-headers
or message/rfc822
when fetching emails with struct: true
.
The same query works for other headers (like Subject
) but not for Message-ID
.
Current IMAP Logic:
// Open inbox in read/write mode
imap.openBox("INBOX", false, (error) => {
if (error) throw error;
console.log("Inbox opened");
// Listen for new emails
imap.on("mail", function () {
console.log("New email received");
// Search for email by Message-ID
imap.search([["HEADER", "MESSAGE-ID", messageId]], function (error, uids) {
if (error) throw error;
console.log("Search results: ", uids);
if (uids.length > 0) {
const f = imap.fetch(uids, { bodies: "", struct: true });
f.on("message", function (message) {
message.on("body", (stream) => {
const buffer: Buffer[] = [];
stream.on("data", (chunk) => buffer.push(chunk));
stream.on("end", async () => {
const parsed = await simpleParser(Buffer.concat(buffer));
console.log(parsed);
});
message.on("attributes", (attrs) => {
imap.setFlags(attrs.uid, ["\\Seen"], (error) =>
console.log(error),
);
});
});
});
}
});
});
});
Question:
Why does imap.search([["HEADER", "MESSAGE-ID", messageId]])
return no results?
Is Message-ID
not indexed by some IMAP servers like GoDaddy?
Would searching inside rfc822-headers
be a better approach?
Would appreciate any insights or workarounds!