最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Unable to retrieve email using message id in imap, node js - Stack Overflow

programmeradmin1浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论