Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Created March 23, 2023 17:08
Show Gist options
  • Save SmugZombie/4a823715127fd6a04506ea42d3939af4 to your computer and use it in GitHub Desktop.
Save SmugZombie/4a823715127fd6a04506ea42d3939af4 to your computer and use it in GitHub Desktop.
const dns = require('dns');
const tls = require('tls');
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 options = {
host: addresses[0].exchange,
port: 465,
secure: true,
rejectUnauthorized: false,
};
const socket = tls.connect(options);
socket.on('secureConnect', () => {
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