Skip to content

Instantly share code, notes, and snippets.

@sled
Created October 5, 2015 13:25
Show Gist options
  • Save sled/4d880b7c5d5a311a07f0 to your computer and use it in GitHub Desktop.
Save sled/4d880b7c5d5a311a07f0 to your computer and use it in GitHub Desktop.
Checks whether a mail is probably a google apps email
#!/usr/bin/env ruby
require 'resolv'
require 'public_suffix'
email = ARGV[0]
domain = email.split("@").last
servers = %w(googlemail.com, google.com)
# get the server with lowest preference number -> highest priority!
mail_server = Resolv::DNS.open.getresources(domain, Resolv::DNS::Resource::IN::MX).min_by{|s| s.preference}
# grab TLD and compare with our list of servers
is_valid = mail_server.nil? ? false: servers.include?( PublicSuffix.parse(mail_server.exchange.to_s).domain)
puts "Is google apps:", is_valid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment