Skip to content

ActiveModel email validation. Performs MX lookups, blacklisting and blocks disposable email

License

Notifications You must be signed in to change notification settings

wolfemm/email_assessor

 
 

Repository files navigation

EmailAssessor

Build Status Gem Version

A fork of Micke Lisinge's ValidEmail2.

ValidEmail2:

  • Validates emails with the help of the mail gem instead of some clunky regexp.
  • Aditionally validates that the domain has a MX record.
  • Optionally validates against a static list of disposable email services.

Why?

ValidEmail2 offers very comprehensive email validation, but it has a few pitfalls.

For starters, it loads the entire list of blacklisted/disposable email domains into memory from a YAML file. In a never ending battle against spam, loading such an extremely large (and ever-growing) array into memory is far from ideal. Instead, EmailAssessor reads a text file line-by-line.

Another pitfall is that subdomains are able to bypass the disposable and blacklist checks in ValidEmail2. EmailAssessor checks if a given domain ends with a blacklisted/disposable domain, preventing subdomains from masking an email that would otherwise be considered invalid.

Installation

Add this line to your application's Gemfile:

gem "email_assessor"

And then execute:

$ bundle

Or install it yourself as:

$ gem install email_assessor

Domain List Files

Domain list files, used for blacklisting and blocking disposable emails, are plain-text files with one lower case domain per line.

Valid domain list file:

example.com
subdomain.example.org

Invalid domain list file:

http://example.com
example.com/mail

Be careful with subdomains. Given the following domain list file:

sub.example.com

A user would be able to register with the email [email protected] but not [email protected] or [email protected].

Usage

Use with ActiveModel

If you just want to validate that it is a valid email address:

class User < ActiveRecord::Base
  validates :email, presence: true, email: true
end

To validate that the domain has a MX record:

validates :email, email: { mx: true }

To validate that the domain is not a disposable email:

validates :email, email: { disposable: true }

To validate that the domain is not blacklisted (via vendor/blacklisted_domains.txt):

validates :email, email: { blacklist: true }

All together:

validates :email, email: { mx: true, disposable: true, blacklist: true }

Note that this gem will let an empty email pass through so you will need to add presence: true if you require an email

Use without ActiveModel

address = EmailAssessor::Address.new("[email protected]")
address.valid? => true
address.disposable? => false
address.valid_mx? => true

Test environment

If you are validating mx then your specs will fail without an internet connection. It is a good idea to stub out that validation in your test environment. Do so by adding this in your spec_helper:

config.before(:each) do
  allow_any_instance_of(EmailAssessor::Address).to receive(:valid_mx?) { true }
end

Requirements

This gem requires Rails 3.2 or 4.0. It is tested against both versions using:

  • Ruby-1.9
  • Ruby-2.0
  • Ruby-2.1
  • Ruby-2.2
  • JRuby-1.9

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

ActiveModel email validation. Performs MX lookups, blacklisting and blocks disposable email

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%