Last active
March 4, 2020 13:39
-
-
Save orhanveli/ed71337af0036b5fb28e35ef6643eff0 to your computer and use it in GitHub Desktop.
email-verify with nodejs
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
const dns = require('dns'); | |
const mailcheck = require('mailcheck'); | |
const isDomainMXValid = (domain) => { | |
return new Promise((resolve, reject) => { | |
dns.resolveMx(domain, (err, mxs) => { | |
if (err) { | |
reject(err); | |
} | |
resolve(!!(mxs && mxs.length > 0 && mxs[0].exchange)); | |
}); | |
}); | |
}; | |
const isEmailValid = (email) => { | |
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(email); | |
}; | |
const getSuggestionForEmail = (email) => { | |
return new Promise((resolve, reject) => { | |
mailcheck.run({ | |
email, | |
// domains: domains, // optional | |
// topLevelDomains: topLevelDomains, // optional | |
// secondLevelDomains: secondLevelDomains, // optional | |
// distanceFunction: superStringDistance, // optional | |
suggested: function(suggestion) { | |
resolve(suggestion); | |
}, | |
empty: function() { | |
resolve(''); | |
} | |
}); | |
}); | |
}; | |
const verifyEmail = async (email) => { | |
if (!isEmailValid(email)) { | |
return false; | |
} | |
const split = email.split('@'); | |
if (split.length === 2) { | |
try { | |
return await isDomainMXValid(split[1]); | |
} catch (error) { | |
// console.log(error); | |
return false; | |
} | |
} | |
return false; | |
} | |
// isDomainMXValid('iamlanistar.com').then(r =>{ | |
// console.log(r); | |
// }); | |
// const email = '[email protected]'; | |
const email = '[email protected]'; | |
verifyEmail(email).then(isValid =>{ | |
console.log(`${email} is ${isValid}`); | |
}); | |
getSuggestionForEmail(email).then(suggestion => { | |
if (suggestion) { | |
console.log(`${email} looks slappy here is the suggestion: ${suggestion.full}`); | |
return; | |
} | |
console.log(`there is no suggestion for ${email}`); | |
}) | |
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
{ | |
"name": "email-verif", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"mailcheck": "^1.1.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment