node-email-mx-checker
v1.0.0
Published
A Node.js library to check MX records for domains
Maintainers
Readme
node-email-mx-checker
A simple Node.js library to check MX records for domains. Written in TypeScript and built with Vite.
Installation
npm install node-email-mx-checkerUsage
ES Modules
import { checkMx } from 'node-email-mx-checker';
async function checkDomain() {
const result = await checkMx('example.com');
if (result.hasMx) {
console.log(`Domain ${result.domain} has MX records:`);
result.mxRecords.forEach(record => {
console.log(`- ${record.exchange} (priority: ${record.priority})`);
});
} else {
console.log(`Domain ${result.domain} has no MX records`);
if (result.error) {
console.log(`Error: ${result.error}`);
}
}
}
checkDomain();CommonJS
const { checkMx } = require('node-email-mx-checker');
async function checkDomain() {
const result = await checkMx('example.com');
if (result.hasMx) {
console.log(`Domain ${result.domain} has MX records:`);
result.mxRecords.forEach(record => {
console.log(`- ${record.exchange} (priority: ${record.priority})`);
});
} else {
console.log(`Domain ${result.domain} has no MX records`);
if (result.error) {
console.log(`Error: ${result.error}`);
}
}
}
checkDomain();API
checkMx(domain, options)
Checks if a domain has MX records and returns the list of MX records.
Parameters
domain(string): The domain to checkoptions(object, optional): Check optionstimeout(number, default: 5000): Timeout in milliseconds for DNS lookups
Returns
Promise resolving to an object with the following properties:
domain(string): The domain that was checkedhasMx(boolean): Whether the domain has MX recordsmxRecords(array, optional): MX records for the domainerror(string, optional): Error message if check failed
Examples
Check MX Records for a Domain
import { checkMx } from 'node-email-mx-checker';
async function checkDomainMx() {
const result = await checkMx('gmail.com');
if (result.hasMx) {
console.log('MX records found:');
result.mxRecords.forEach(record => {
console.log(`- ${record.exchange} (priority: ${record.priority})`);
});
} else {
console.log(`No MX records found: ${result.error || 'Domain has no MX records'}`);
}
}
checkDomainMx();Check Multiple Domains
import { checkMx } from 'node-email-mx-checker';
async function checkMultipleDomains() {
const domains = ['gmail.com', 'example.com', 'nonexistentdomain123456.com'];
for (const domain of domains) {
const result = await checkMx(domain);
console.log(`${domain}: ${result.hasMx ? 'Has MX records' : 'No MX records'}`);
if (result.hasMx) {
result.mxRecords.forEach(record => {
console.log(` - ${record.exchange} (priority: ${record.priority})`);
});
} else if (result.error) {
console.log(` Error: ${result.error}`);
}
}
}
checkMultipleDomains();Custom Timeout
import { checkMx } from 'node-email-mx-checker';
async function checkWithLongerTimeout() {
// Use a 10-second timeout
const result = await checkMx('example.com', { timeout: 10000 });
console.log(result);
}
checkWithLongerTimeout();Development
Building the package
npm run buildRunning tests
npm testLicense
MIT
