Skip to content

Instantly share code, notes, and snippets.

@jonmaim
Last active October 25, 2016 05:04
Show Gist options
  • Save jonmaim/6956f87e0b9546a12ee960ebec37aee6 to your computer and use it in GitHub Desktop.
Save jonmaim/6956f87e0b9546a12ee960ebec37aee6 to your computer and use it in GitHub Desktop.
Iterate over emails in a .mbox file and output bouncing emails
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() {});
@jonmaim
Copy link
Author

jonmaim commented Oct 24, 2016

Each bouncing email is output on a new line, you can then pipe the result to another command to further process it:

node index.js | xargs -I {} echo bouncing {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment