A robust email validation library that goes beyond basic regex validation by checking:
Most email validators only check basic syntax, allowing many invalid emails to pass. This library catches emails that other validators miss:
| Invalid Email | Reason |
|---|---|
| som_e-one@gmail.com | Gmail doesn't allow "_" and "-" symbols |
| someone@8avymt4v93mvt3t03.com | "8avymt4v93mvt3t03.com" isn't a real domain and doesn't have DNS MX records |
| s!o#m$e%o^n&e@realdomain.com | Most public email providers only allow "a-z","0-9",".","_","-","+" before "@" |
| someone@hotnail.com | Common typo that can be blocked with the domain blocklist feature |
# NPM
npm install email-validator-dns-provider-rules
# Yarn
yarn add email-validator-dns-provider-rules
# pnpm
pnpm add email-validator-dns-provider-rules
import {validateEmail} from "email-validator-dns-provider-rules";
// Basic validation
const result = await validateEmail('someone@gmail.com');
if (!result.valid) {
console.log(`Email is invalid: ${result.reasonText}`);
}
Validates an email address using DNS checks and provider-specific rules.
Parameters:
email (string): The email address to validateoptions (object, optional): Configuration optionsReturns:
ValidationResult Object:
valid (boolean): Whether the email is validreasonId (number, optional): ID of the validation failure reasonreasonText (string, optional): Human-readable description of the validation failureinterface ValidatorOptions {
blocklistDomains?: string[]; // Domains to block
dohProviderUrl?: string; // Custom DNS-over-HTTPS provider URL
dohRetryAmount?: number; // Number of retries for DNS queries
skipCache?: boolean; // Skip the internal MX domain cache
mxResolver?: (domain: string) => Promise<string[] | false>; // Custom MX resolver
}
You can provide your own error messages by mapping the reason IDs:
const customReasons = {
[INVALID_REASON_AMOUNT_OF_AT]: 'Email must contain exactly one @ symbol',
[INVALID_REASON_USERNAME_GENERAL_RULES]: 'Username contains invalid characters',
[INVALID_REASON_DOMAIN_GENERAL_RULES]: 'Domain name is invalid',
[INVALID_REASON_NO_DNS_MX_RECORDS]: 'Domain does not have mail server records',
[INVALID_REASON_DOMAIN_IN_BLOCKLIST]: 'This email domain is not allowed',
[INVALID_REASON_USERNAME_VENDOR_RULES]: 'Username does not meet provider requirements',
[INVALID_REASON_DOMAIN_POPULAR_TYPO]: 'Domain appears to be a typo (did you mean gmail.com?)',
};
const result = await validateEmail('someone@gmail.com');
if (!result.valid) {
console.log(`Email is invalid: ${customReasons[result.reasonId]}`);
}
You can block specific domains:
const blockedDomains = [
'disposable-email.com',
'temporary-mail.org',
'hotnail.com' // Common typo of hotmail.com
];
const result = await validateEmail('user@disposable-email.com', {
blocklistDomains: blockedDomains
});
// result.valid will be false
You can specify a custom DNS-over-HTTPS provider:
const result = await validateEmail('someone@gmail.com', {
dohProviderUrl: 'https://your-custom-doh-provider.com/dns-query'
});
For Node.js environments, you can use the native DNS module:
import {resolveMx} from 'dns/promises';
async function nodeResolver(emailDomain: string): Promise<string[] | false> {
try {
const records = await resolveMx(emailDomain);
return records.map(rec => rec.exchange);
} catch (error) {
if (error.message.includes('ENOTFOUND')) {
return []; // Empty records treated as invalid
}
return false; // Other errors treated as "can't determine"
}
}
const result = await validateEmail('someone@gmail.com', {
mxResolver: nodeResolver
});
This project is licensed under the MIT License - see the LICENSE file for details.
$ claude mcp add email-validator-dns-provider-rules \
-- python -m otcore.mcp_server <graph>