node.jstypescriptvalidationemailimap

Unable to retrieve email using message id in imap, node js


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! šŸš€

What I'm Trying to Do: I'm trying to verify if an email address exists by sending an email and checking for bounce (debounce) messages.

If no bounce email is received, the email likely exists. If a bounce email is received after resending, it means the email is fake, and I should block it. I'm using GoDaddy email and **IMAP **to track these messages.

What I've Tried:

imap.search([["HEADER", "MESSAGE-ID", messageId]], (err, results) => {
  console.log(results);
});

āž” Returns empty array ([]).

When i search the message id the web app directly its showing the email When i search the message id the web app directly its showing

Is there any better strategy to check if the email are genuine or not?


Solution

  • The search you are using searches for a message that has the specified message-id. But what you're actually looking for is a bounce for a message that has the specified message-id.

    Something like imap.search([["BODY", messageId]], ... may work a great deal better, since a bounce is likely to contain the bounced message in its body.

    But I think you'll have even more luck if you change your approach to one that processes all bounces, one by one. If you can parse and process one, do that, if not, escalate to a human and go on with the next.