nodejs mailparser多次解析相同的消息

Hec*_*tor 4 email node.js

我正在使用inboxmailparsernpm模块来读取和解析来自邮箱的电子邮件.

我在解析重复的消息时遇到了一些麻烦.目前发生的事情是:

正如您对电子邮件服务器所期望的那样,电子邮件将被放入正确的邮箱中.然后他们inbox在我的node.js应用程序中被选中.然后他们被管道传输mailparser并被解析.

这是正常的.问题是当我发送第二封电子邮件时,我再次获得第一封电子邮件.有时我会得到多个,但我还没弄清楚是什么原因导致的.

let _inbox      = require( "inbox"      );
let _MailParser = require( "mailparser" ).MailParser;

let parser  = new _MailParser();
let mail    = _inbox.createConnection( false, "mail.myemailserver.com", {
  auth: {
    user: "email@myemailserver.com",
    pass: "mypasswordthatissostrongnoonewilleverguessit:)"
  }
});

mail.on( "new", ( message ) => {
  console.log( message.UID, message.title );
  db_insert( DB.collection( "email_ids" ), { _id: message.UID } ).then( () => {
    mail.createMessageStream( message.UID ).pipe( parser );
  });
});

parser.on( "end", ( message ) => {
  // This works the first time, I get the correct message.
  // The second time this gets called I just get the first message again.
});
Run Code Online (Sandbox Code Playgroud)

我的蜘蛛侠感觉告诉我这事做的事实,我不知道如何streamspipe工作.值得注意的是,这是我第一次使用这些库中的任何一个,但我可能错过了一些东西.

mailparser
收件箱

我正在使用MongoDB,如果你尝试插入相同的_id两次,它会抛出一个摇摆不定,但这根本不是抱怨.这加强了我对streams和的怀疑pipe.

我正在使用es6和babel转换器.

更新

我不再需要这个问题的答案.我决定寻找一个不同的图书馆.我现在正在使用mail-notifier.

以防有人感兴趣.这就是我解决问题的方法.

let _notifier = require( "mail-notifier" );

let imap = {
  user    : "email@myemailserver.com",
  password: "mypasswordthatissostrongnoonewilleverguessit:)",
  host    : "mail.mymailserver.com"
};

_notifier( imap ).on( "mail", ( mail ) => {
  // process email
}).start();
Run Code Online (Sandbox Code Playgroud)

我仍然有兴趣知道是什么导致了另一种方法的问题,但这并不重要.

小智 5

我有同样的问题.原因是每次运行循环时都必须创建MailParser的新实例.

let _MailParser = require( "mailparser" ).MailParser;

mail.on( "new", ( message ) => {

  parser = new _MailParser();

  // do your stuff

  parser.on( "end", ( message ) => {

     // finished

  });
}
Run Code Online (Sandbox Code Playgroud)