-
-
Save mnazim/3013290 to your computer and use it in GitHub Desktop.
Verify email addresses
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 _ = require('underscore'), | |
exec = require('child_process').exec, | |
net = require('net'), | |
email = process.argv.splice(2)[0], | |
domain = email.split('@')[1]; | |
function verify(mxrecord) { | |
console.log('connecting to', mxrecord); | |
var conn = net.createConnection(25, mxrecord), | |
commands = [ | |
'HELO ' + mxrecord, | |
'mail from:<[email protected]>', | |
'rcpt to:<' + email + '>' | |
], | |
i = 0; | |
conn.setEncoding('ascii'); | |
conn.on('connect', function () { | |
console.log('connected successfully'); | |
}); | |
conn.on('data', function (data) { | |
console.log(data) | |
if (i >= commands.length) { | |
if ((/ok\s|okay/).test(data.toLowerCase())) { | |
console.log(email, 'appears to be valid'); | |
} else { | |
console.error(data); | |
} | |
conn.end(); | |
return; | |
} | |
console.log(commands[i]); | |
var message = conn.write(commands[i] + '\n'); | |
if (!message) { | |
console.error('error sending last message'); | |
process.exit(1); | |
} | |
i += 1; | |
}); | |
conn.on('error', function () { | |
console.error('error', arguments); | |
}); | |
conn.on('end', function () { | |
process.exit(0); | |
}); | |
} | |
exec('nslookup -type=MX ' + domain, function (err, stdout, stderr) { | |
var records = _.reject(stdout.split(/\n/), function (l) { | |
return l.indexOf(domain) !== 0 || l.indexOf('exchanger') === -1; | |
}).sort(function (a, b) { | |
var a = a.split(/\s+/)[4], | |
b = b.split(/\s+/)[4]; | |
return ~~a - ~~b; | |
}), | |
record; | |
if (!records.length) { | |
console.log('no records'); | |
console.log(records); | |
process.exit(1); | |
} | |
record = _.last(_.first(records).split(/\s+/)).replace(/\.$/, ''); | |
verify(record); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment