Skip to content

Instantly share code, notes, and snippets.

@orhanveli
Last active March 4, 2020 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orhanveli/ed71337af0036b5fb28e35ef6643eff0 to your computer and use it in GitHub Desktop.
Save orhanveli/ed71337af0036b5fb28e35ef6643eff0 to your computer and use it in GitHub Desktop.
email-verify with nodejs
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 = 'user@not-existed-domain.com';
const email = 'user@hotmal.com';
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}`);
})
{
"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