Rich Email Validator
A set of helpers to validate emails:
- Validate an email
- Filter a list of emails
- Filter a list of emails from file
- Filter a list of emails from file and output to file
It works as follows:
- Checks the availability of the mx records of the email's domain. This is done via a DNS lookup.
- Checks the format of the email via a regular expression that can be configured. This is done for performance only.
- Use a configurable number of threads to check thousands of emails in minutes.
Installation
Add this line to your application's Gemfile:
gem 'rich_email_validator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rich_email_validator
Usage
Validate an email
require 'rich_email_validator'
RichEmailValidator.valid_email?('someone@gmail.com') # => true
RichEmailValidator.valid_email?('someone@g.com') # => false because of DNS lookup check
Set a predfined regular expression
It's only used for performace reasons to filter the format of an email before doing a DNS lookup check.
The current value is:
%r{
\A[\w!#$%&'*+/=?`{|}~^-]+
(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*
@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\Z
}ix
You can change it:
# Customized email regular expression
RichEmailValidator.email_regexp = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
Filter a list of emails
list = ['someone@g.com', 'someone@gmail.com']
RichEmailValidator.filter_list(list) #=> ["someone@gmail.com"]
# You have a fine grained control, so you can control threads count
# Default is 20, max 100
RichEmailValidator.filter_list(list, threads_count: 15)
Filter a list of emails from file
file_path = '/path/to/emails_list'
File.readlines(file_path).size #=> 15
RichEmailValidator.filter_file(file_path).size #=> 10
# You have a fine grained control, so you can control threads count
# Default is 20, max 100
RichEmailValidator.filter_file(file_path, threads_count: 15)
Filter a list of emails from file and output to file
file_path = '/path/to/emails_list'
output_path = '/path/to/output'
File.readlines(file_path).size # => 15
RichEmailValidator.export_valid_list(file_path, output_path)
File.readlines('output.txt').size #=> 10
# You have a fine grained control, so you can control threads count
# Default is 20, max 100
RichEmailValidator.export_valid_list(file_path, output_path, threads_count: 15)
Contributing
- Fork it ( https://github.com/[my-github-username]/rich_email_validator/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request