Skip to content

Instantly share code, notes, and snippets.

@mikedamage
Last active December 2, 2023 13:10
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mikedamage/5564196 to your computer and use it in GitHub Desktop.
Save mikedamage/5564196 to your computer and use it in GitHub Desktop.
Ping an email address to see if it exists. This script resolves MX records to find the SMTP server responsible for delivering mail to the address, connects to it, and starts to send a message to the address. It disconnects before the message is sent.
#!/usr/bin/env ruby
#
# = Email Ping
#
# Check to see if an email address exists by looking up MX records and connecting
# to the address's home SMTP server. It then starts to send a message to the address
# but quits before the message is actually sent.
require 'resolv'
require 'net/smtp'
address = ARGV[0].chomp
domain = address.split('@').last
dns = Resolv::DNS.new
puts "Resolving MX records for #{domain}..."
mx_records = dns.getresources domain, Resolv::DNS::Resource::IN::MX
mx_server = mx_records.first.exchange.to_s
puts "Connecting to #{mx_server}..."
Net::SMTP.start mx_server, 25 do |smtp|
smtp.helo "loldomain.com"
smtp.mailfrom "test@loldomain.com"
puts "Pinging #{address}..."
puts "-" * 50
begin
smtp.rcptto address
puts "Address probably exists."
rescue Net::SMTPFatalError => err
puts "Address probably doesn't exist."
end
end
# vim: set ft=ruby ts=2 sw=2 :
@jramby
Copy link

jramby commented Jan 20, 2014

Hi, sorry with this quistion, but do I use this file ?

Thanks :)

@hugodias
Copy link

Hey @mikedamage nice code, but i was wondering, if you do this too much ( like 50.000 times a day ) the smtp servers could block you for some kind of spam?

@haihoa
Copy link

haihoa commented Feb 2, 2016

@beserious
Copy link

Cheapest bulk email verify at http://www.mycleanlist.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment