Created
November 22, 2009 12:57
-
-
Save secretfader/240559 to your computer and use it in GitHub Desktop.
email validation in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'resolv' | |
require 'rubygems' | |
require 'sequel' | |
def validate_email_domain(email) | |
domain = email.match(/\@(.+)/)[1] | |
Resolv::DNS.open do |dns| | |
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX) | |
end | |
@mx.size > 0 ? true : false | |
end | |
DB = Sequel.mysql(:host => 'localhost', :user => 'user', :password => 'password', :database => 'database') | |
subscribers = DB[:previous_email_list_table] | |
validated = DB[:validated] | |
subscribers.each do |sub| | |
puts "Validating email number #{sub[:id]}" | |
validator = validate_email_domain(sub[:email]) | |
if validator == true | |
validated.insert(:id => nil, :email => sub[:email], :firstname => sub[:first_name], :lastname => sub[:last_name]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `validated` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`email` varchar(255) NOT NULL, | |
`firstname` varchar(255) DEFAULT NULL, | |
`lastname` varchar(255) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment