Skip to content

Commit

Permalink
Merge branch 'rubygem' of https://github.com/buren/mailchecker into b…
Browse files Browse the repository at this point in the history
…uren-rubygem

# Conflicts:
#	.gitignore
#	README.md
  • Loading branch information
FGRibreau committed Sep 29, 2015
2 parents ca0adb2 + b6dea7d commit 9818a9b
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
node_modules
npm-*.log
compare.js
*.pyc
*.pyc
*.gem
Gemfile.lock
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MailChecker
# MailChecker
[![Build Status](https://drone.io/github.com/FGRibreau/mailchecker/status.png)](https://drone.io/github.com/FGRibreau/mailchecker/latest) [![Deps](https://david-dm.org/FGRibreau/mailchecker.png)](https://david-dm.org/FGRibreau/mailchecker) [![Version](http://badge.fury.io/js/mailchecker.png)](http://badge.fury.io/js/mailchecker) [![Downloads](http://img.shields.io/npm/dm/mailchecker.svg)](https://www.npmjs.com/package/mailchecker)

Cross-language email validation. Backed by a [database](./list.json) of **1979 throwable email providers**.
Expand All @@ -10,7 +10,7 @@ This will be very helpful when you have to contact your users and you want to av

-------------------------

> Looking for a free **Redis administration & monitoring** service?
> Looking for a free **Redis administration & monitoring** service?
>
> [**Create your free account on Redsmin**](https://redsmin.com?gh)
Expand All @@ -23,6 +23,8 @@ MailChecker currently supports:
* [NodeJS](https://github.com/FGRibreau/mailchecker/tree/master/platform/node) (CommonJS)
* [JavaScript](https://github.com/FGRibreau/mailchecker/tree/master/platform/javascript) (Client-Side)
* [PHP](https://github.com/FGRibreau/mailchecker/tree/master/platform/php)
* [Python](https://github.com/FGRibreau/mailchecker/tree/master/platform/python)
* [Ruby](https://github.com/FGRibreau/mailchecker/tree/master/platform/ruby)
* **Easily add support for your own language with MailChecker template system and [send us a pull-request!](https://github.com/FGRibreau/mailchecker/fork_select)**

-------------------------
Expand Down Expand Up @@ -59,6 +61,7 @@ if(!MailChecker('myemail.com')){
```

### PHP

```php
include __DIR__."/MailChecker/platform/php/MailChecker.php";

Expand All @@ -71,8 +74,8 @@ if(!MailChecker('myemail.com')){
}
```


### Python

```python
# no package yet; just drop in MailChecker.py where you want to use it.
import MailChecker
Expand All @@ -82,6 +85,20 @@ if not m.is_valid('bla@example.com'):
print "O RLY !"
```

### Ruby

```ruby
require 'mail_checker'

unless MailChecker('myemail@yopmail.com')
fail('O RLY!')
end

unless MailChecker.valid?('myemail@yopmail.com')
fail('O RLY!')
end
```

--------------------


Expand All @@ -92,6 +109,11 @@ NodeJS/JavaScript
npm install mailchecker
```

Ruby
```bash
gem install mail_checker
```

__We accept pull-requests for other package manager__.

## Data sources
Expand Down
15 changes: 15 additions & 0 deletions mail_checker.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8
Gem::Specification.new do |spec|
spec.name = 'mail_checker'
spec.version = '0.1.0'
spec.authors = ['Francois-Guillaume Ribreau', 'Jacob Burenstam']
spec.email = ['github@fgribreau.com']

spec.summary = 'Temporary (disposable/throwaway) email detection library. Covers 1979 fake email providers.'
spec.description = 'Cross-language temporary (disposable/throwaway) email detection library. Covers 1979 fake email providers. http://twitter.com/FGRibreau'
spec.homepage = 'https://github.com/FGRibreau/mailchecker'
spec.license = 'MIT'

spec.files = ['platform/ruby/mail_checker.rb']
spec.require_paths = ['platform/ruby']
end
32 changes: 32 additions & 0 deletions platform/ruby/mail_checker.rb

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions platform/ruby/mail_checker.tmpl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'set'

module MailChecker
# Based on PHP FILTER_VALIDATE_EMAIL
# See https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php
EMAIL_REGEX = /^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i
# Blacklisted domains
BLACKLIST = [{{& listSTR }}].
map(&:downcase).
to_set

def self.valid?(email)
return false unless valid_email?(email)

!BLACKLIST.include?(extract_domain(email))
end

def self.valid_email?(email)
return false if email.nil?

email =~ EMAIL_REGEX
end

def self.extract_domain(email)
domain = email.gsub(/.+@([^.]+)/, '\1').downcase
domain.split('.')[-2, 2].join('.') # Don't include subdomains
end
end

def MailChecker(email)
MailChecker.valid?(email)
end
39 changes: 39 additions & 0 deletions test/platform.ruby.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'minitest/autorun'

require_relative '../platform/ruby/mail_checker'

class TestMailChecker < MiniTest::Unit::TestCase
def valid!(email)
assert_equal MailChecker(email), true
end

def invalid!(email)
assert_equal MailChecker(email), false
end

def test_return_true_if_valid
valid!('plop@plop.com')
valid!('my.ok@ok.plop.com')
valid!('my+ok@ok.plop.com')
valid!('my=ok@ok.plop.com')
valid!('ok@gmail.com')
valid!('ok@hotmail.com')
end

def test_return_false_if_email_invalid
invalid!('plopplop.com')
invalid!('my+ok@ok=plop.com')
invalid!('my,ok@ok.plop.com')
invalid!('ok@tmail.com')
end

def test_return_false_if_throwable_domain
invalid!('ok@33mail.com')
invalid!('ok@ok.33mail.com')
invalid!('ok@guerrillamailblock.com')
end

def test_can_be_called_as_regular_method
assert_equal MailChecker.valid?(nil), false
end
end

0 comments on commit 9818a9b

Please sign in to comment.