Reputation: 668
I'm using both the inbox
and mailparser
npm modules to read and parse emails from a mailbox.
I'm having some trouble with duplicate messages being parsed. currently what is happening is:
The emails are being dropped into the correct mailbox as you would expect from an email server. Then they are being picked up by inbox
in my node.js application. Then they are being piped to mailparser
and being parsed.
That is working correctly. The problem is that when I send a second email I just get the first one again. Sometimes I get multiple, but I haven't figured out what causes that.
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.
});
My spidey senses are telling me it has something to do with the fact I have no idea how streams
and pipe
work. It's also worth noting that this is the first time I've used either of these libraries and I may have missed something.
I'm using MongoDB and it throws a wobbly if you try to insert the same _id
twice, but this isn't complaining at all. Which reinforces my suspicion about streams
and pipe
.
I'm using es6 with the babel transpiler.
I no longer need an answer to this question. I decided to look for a different library. I'm now using mail-notifier
.
Just in case someone is interested. This is how I solved the problem.
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();
I'd still be interested to know what was causing the problem with the other approach, but it's not important.
Upvotes: 4
Views: 2583
Reputation: 1215
I had the same problem. The reason is that you have to create new instance of MailParser each time you run through a cycle.
let _MailParser = require( "mailparser" ).MailParser;
mail.on( "new", ( message ) => {
parser = new _MailParser();
// do your stuff
parser.on( "end", ( message ) => {
// finished
});
}
Upvotes: 5