saksh-email-validator
v2.1.2
Published
A comprehensive email validation module with syntax, domain, blacklist, and SMTP checks.
Downloads
29
Maintainers
Readme
Email Validation Module Overview
This Email Validation Module provides a comprehensive solution for validating email addresses. It includes various features such as syntax validation, domain checks, blacklist checks, disposable email detection, role-based email detection, and SMTP server validation. Additionally, it supports custom validation rules and offers suggestions for correcting common email syntax errors.
Key Features
Syntax Validation
- Validates the syntax of an email address using a regular expression.
- Provides suggestions for correcting common syntax errors.
Domain Checks
- Checks if the domain of an email address has MX (Mail Exchange) records.
- Skips SMTP validation for blacklisted domains.
Blacklist Checks
- Checks if the domain of an email address is blacklisted.
- Supports custom blacklisted domains.
Disposable Email Detection
- Detects if an email address is from a disposable email provider.
- Supports custom disposable domains.
Role-Based Email Detection
- Detects if an email address is role-based (e.g., [email protected]).
- Supports custom role-based emails.
SMTP Server Validation
- Validates the SMTP server for the email domain.
- Supports custom SMTP ports, connection timeout, and retry settings.
Custom Validation Rules
- Allows adding custom validation rules to extend the validation logic.
International Email Normalization
- Normalizes international email addresses to ASCII format using the punycode module.
Example Usage
Here’s an example of how to use the email validation module:
const {
sakshValidateEmail,
sakshValidateEmailBatch,
sakshCache,
sakshAddCustomValidationRule
} = require('saksh-email-validator');
// Custom validation rule 1: Disallow emails containing "test"
function customRule1(email) {
return {
valid: !email.includes('test'),
reason: 'Emails containing "test" are not allowed.',
priority: 1 // Higher priority
};
}
// Custom validation rule 2: Disallow emails from "example.com"
function customRule2(email) {
const domain = email.split('@')[1];
return {
valid: domain !== 'example.com',
reason: 'Emails from "example.com" are not allowed.',
priority: 2 // Lower priority
};
}
// Example email addresses to test
const emails = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
];
// Configuration object
const config = {
validateMx: true,
validateSmtp: true,
blacklistedDomains: [],
disposableDomains: [],
roleBasedEmails: [],
wellKnownDomains: ['sakshamapp.com'],
customValidationRules: [customRule1, customRule2]
};
// Test the email validation function
async function testEmailValidation() {
for (const email of emails) {
const result = await sakshValidateEmail(email, config);
console.log(`Validation result for ${email}:`, result);
}
}
testEmailValidation();
function clearCache() {
sakshCache.flushAll();
console.log('Cache cleared.');
}
// clearCache()
Detailed Function Descriptions
sakshValidateEmailSyntax(email)
- Description: Validates the syntax of an email address using a regular expression.
- Parameters:
email
(string) - The email address to validate. - Returns:
boolean
- Returnstrue
if the email syntax is valid,false
otherwise.
sakshEmailSyntaxSuggestions(email)
- Description: Provides suggestions for correcting common email syntax errors.
- Parameters:
email
(string) - The email address to suggest corrections for. - Returns:
string[]
- Returns an array of suggested email formats.
sakshCheckDomainMXRecords(email)
- Description: Checks if the domain of an email address has MX records.
- Parameters:
email
(string) - The email address to check. - Returns:
Promise
- Resolves totrue
if the domain has MX records,false
otherwise.
sakshIsDomainBlacklisted(email, customBlacklistedDomains = [])
- Description: Checks if the domain of an email address is blacklisted.
- Parameters:
email
(string) - The email address to check.customBlacklistedDomains
(string[]) - Custom blacklisted domains.
- Returns:
boolean
- Returnstrue
if the domain is blacklisted,
Conclusion
This email validation module offers a robust set of features to ensure the validity of email addresses. It is highly configurable and can be extended with custom validation rules to meet specific requirements. The module also provides helpful suggestions for correcting common email errors, making it a comprehensive solution for email validation needs.
Support
susheel2339 at gmail.com