Last active
October 25, 2016 05:04
-
-
Save jonmaim/6956f87e0b9546a12ee960ebec37aee6 to your computer and use it in GitHub Desktop.
Iterate over emails in a .mbox file and output bouncing emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Mbox = require('node-mbox'); | |
var MailParser = require("mailparser").MailParser; | |
var mbox = new Mbox('inbox.mbox', { /* options */ }); | |
var bouncingEmails = []; | |
mbox.on('message', function(msg) { | |
var mailparser = new MailParser(); | |
mailparser.on("end", function(mail) { | |
function _process() { | |
if (mail.text) { | |
var matches = mail.text.match(/([0-9a-z*](?:[-+*\.\w]*[0-9a-z]*)@(?:[0-9a-z][-\w]*\.)+[a-z]{2,9})/i); | |
if (matches) { | |
console.log(matches[0]); | |
bouncingEmails.push(matches[0]); | |
} | |
} | |
} | |
if (mail.from[0].address === '[email protected]' && mail.subject.match(/Delivery Status Notification \(Failure\)/i)) { | |
_process(); | |
} else if (mail.text) { | |
var isMatching = [/550-5\.1\.1/i, /550 5\.1\.1/i, /550-5\.1\.0/i, /550 5\.1\.0/i, /550-5\.1\.10/i, /550 5\.1\.10/i, /5\.5\.0/i, /5\.3\.0/i].some(function(re) { return mail.text.match(re); }); | |
if (isMatching) { _process() } | |
} | |
}); | |
mailparser.write(msg); | |
mailparser.end(); | |
}); | |
mbox.on('error', function(err) { | |
console.log('got an error', err); | |
}); | |
mbox.on('end', function() {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Each bouncing email is output on a new line, you can then pipe the result to another command to further process it: