Skip to content

Instantly share code, notes, and snippets.

@Workman
Created May 22, 2023 08:29
Show Gist options
  • Save Workman/e75cfca6bc6341368768f051ae40f9ad to your computer and use it in GitHub Desktop.
Save Workman/e75cfca6bc6341368768f051ae40f9ad to your computer and use it in GitHub Desktop.
def get_domain email
return unless email_format_valid?(email)
email.split("@").last
end
def get_mxs email
return unless email_format_valid?(email)
domain = get_domain(email)
cache("mxs_#{domain}") do
Resolv::DNS.open do |dns|
ress = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
ress.map do |r|
[
r.exchange.to_s,
IPSocket.getaddress(r.exchange.to_s),
r.preference
]
end
rescue Exception => e
puts "Failed lookup: #{e}"
nil
end
end
end
def get_mx email
get_mxs(email)&.first&.first
end
def base_email(email)
return unless email_format_valid?(email)
a = email.split("@", 2)
"#{a[0].split("+").first.strip}@#{get_domain(email)}"
end
def email_format_valid?(email)
return nil unless email =~ /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+\z/
true
end
def free_esp?(domain)
return nil unless domain
domains = remote.get("https://gist.githubusercontent.com/ammarshah/f5c2624d767f91a7cbdc4e54db8dd0bf/raw/660fd949eba09c0b86574d9d3aa0f2137161fc7c/all_email_provider_domains.txt").cache.body.split("\n")
domains.include?(domain.downcase)
end
def smtp_validation(email)
return unless get_mx(email).present?
remote.get("https://crcr.me/?email=#{email}&mx=#{get_mx(email)}").cache(1.hour.from_now).json
end
def business_information(email)
return unless email_format_valid?(email)
remote.get("https://sso.serial.im/r/bin:logo-api/search?q=#{email}").cache.json
end
ret = {}
ret[:query] = params[:email]
if ret[:query].present?
ret[:email] = base_email(ret[:query])
ret[:domain] = get_domain(ret[:email])
ret[:is_email_format] = email_format_valid?(ret[:email])
ret[:free_esp] = free_esp?(ret[:domain])
ret[:smtp_validation] = smtp_validation(ret[:email])
ret[:is_valid] = ret[:smtp_validation] && ret[:smtp_validation].dig("valid") == "valid" || false
ret[:business_info] = ret[:free_esp] ? nil : business_information(ret[:email])
ret[:mxs] = get_mxs(ret[:email])
ret[:mx] = get_mx(ret[:email])
else
ret[:error] = "?email= is required"
end
send_json(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment