Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Created March 23, 2023 16:59
Show Gist options
  • Save SmugZombie/a6e63d3609ea5c82196c46752e6a4d3c to your computer and use it in GitHub Desktop.
Save SmugZombie/a6e63d3609ea5c82196c46752e6a4d3c to your computer and use it in GitHub Desktop.
Uses SMTP communication to validate whether the input email address exists or not
const dns = require('dns');
const net = require('net');
const email = process.argv[2];
if (!email) {
console.error('Please provide an email address as an argument');
process.exit(1);
}
// Extract the domain from the email address
const domain = email.split('@')[1];
// Look up the MX records for the domain
dns.resolveMx(domain, (err, addresses) => {
if (err) {
console.error(`Failed to look up MX records for ${domain}: ${err.message}`);
process.exit(1);
}
if (addresses.length === 0) {
console.error(`No MX records found for ${domain}`);
process.exit(1);
}
// Connect to the SMTP server
const socket = net.createConnection(25, addresses[0].exchange);
socket.on('connect', () => {
console.log(`Connected to ${addresses[0].exchange}`);
socket.write(`HELO ${domain}\r\n`, () => {
console.log('Sent HELO command');
socket.write(`MAIL FROM:<${email}>\r\n`, () => {
console.log(`Sent MAIL FROM command for ${email}`);
socket.end();
});
});
});
socket.on('error', (err) => {
console.error(`Error connecting to ${addresses[0].exchange}: ${err.message}`);
process.exit(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment