Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilc committed Dec 13, 2012
0 parents commit ab853b1
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 0 deletions.
Binary file added .README.md.swp
Binary file not shown.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in verifier.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2012 Kamil Ciemniewski

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
@@ -0,0 +1,48 @@
# Email Verifier

Helper validation utility for checking whether given email address is real or not.
Many times as developers we've put in our models statements for checking email address
*format*. This gem will complete your existing setups with validator that actually
connects with given mail server and asks if given email address exists for real.

## Installation

Add this line to your application's Gemfile:

gem 'email_verifier'

And then execute:

$ bundle

Or install it yourself as:

$ gem install verifier

## Usage

Just put this in your model e. g:

validates_email_realness_of :email

Or - if you'd like to use it outside of your models:

EmailValidator.check(youremail)

This method will return true or false, or - will throw exception
with nicely detailed info about what's wrong.

## Credits
![End Point Corporation](http://www.endpoint.com/images/end_point.png)

Email Verifier is maintained and funded by [End Point Corporation](http://www.endpoint.com/)

Please send questions to [kamil@endpoint.com](mailto:kamil@endpoint.com)

## 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
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require "bundler/gem_tasks"
19 changes: 19 additions & 0 deletions email_verifier.gemspec
@@ -0,0 +1,19 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'email_verifier/version'

Gem::Specification.new do |gem|
gem.name = "email_verifier"
gem.version = EmailVerifier::VERSION
gem.authors = ["Kamil Ciemniewski"]
gem.email = ["kamil@endpoint.com"]
gem.description = %q{Helper utility checking if given email address exists or not}
gem.summary = %q{Helper utility checking if given email address exists or not}
gem.homepage = "http://endpoint.com"

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
end
Binary file added lib/.verifier.rb.swp
Binary file not shown.
22 changes: 22 additions & 0 deletions lib/email_verifier.rb
@@ -0,0 +1,22 @@
require "email_verifier/version"

module EmailVerifier
require 'email_verifier/checker'
require 'email_verifier/exceptions'
require 'email_verifier/config'
require "email_verifier/validates_email_realness"

def self.check(email)
v = EmailVerifier::Checker.new(email)
v.connect
v.verify
end

def self.config(&block)
if block_given?
block.call(EmailVerifier::Config)
else
EmailVerifier::Config
end
end
end
Binary file added lib/email_verifier/.server.rb.swp
Binary file not shown.
Binary file not shown.
94 changes: 94 additions & 0 deletions lib/email_verifier/checker.rb
@@ -0,0 +1,94 @@
require 'net/smtp'

class EmailVerifier::Checker

##
# Returns server object for given email address or throws exception
# Object returned isn't yet connected. It has internally a list of
# real mail servers got from MX dns lookup
def initialize(address)
@email = address
@domain = address.split("@").last
@servers = list_mxs @domain
raise EmailVerifier::NoMailServerException.new("No mail server for #{address}") if @servers.empty?
@smtp = nil

# this is because some mail servers won't give any info unless
# a real user asks for it:
@user_email = EmailVerifier.config.verifier_email
end

def list_mxs(domain)
`host -t MX #{domain}`.scan(/(?<=by ).+/).map do |mx|
res = mx.split(" ")
next if res.last[0..-2].strip.empty?
{ priority: res.first.to_i, address: res.last[0..-2] }
end.reject(&:nil?).sort_by { |mx| mx[:priority] }
end

def is_connected
!@smtp.nil?
end

def connect
begin
server = next_server
raise EmailVerifier::OutOfMailServersException.new("Unable to connect to any one of mail servers for #{@email}") if server.nil?
@smtp = Net::SMTP.start(server[:address], 25)
return true
rescue EmailVerifier::OutOfMailServersException => e
return false
rescue => e
retry
end
end

def next_server
@servers.shift
end

def verify
self.helo @user_email
self.mailfrom @user_email
self.rcptto(@email)
end

def helo(address)
ensure_connected

ensure_250 @smtp.helo(@domain)
end

def mailfrom(address)
ensure_connected

ensure_250 @smtp.mailfrom(address)
end

def rcptto(address)
ensure_connected

begin
ensure_250 @smtp.rcptto(address)
rescue => e
if e.message[/^550/]
return false
else
raise EmailVerifier::FailureException.new(e.message)
end
end
end

def ensure_connected
raise EmailVerifier::NotConnectedException.new("You have to connect first") if @smtp.nil?
end

def ensure_250(smtp_return)
if smtp_return.status.to_i == 250
return true
else
raise EmailVerifier::FailureException.new "Mail server responded with #{smtp_return.status} when we were expecting 250"
end
end

end
12 changes: 12 additions & 0 deletions lib/email_verifier/config.rb
@@ -0,0 +1,12 @@
module EmailVerifier
module Config
class << self
attr_accessor :verifier_email

def reset
@verifier_email = "nobody@nonexistant.com"
end
end
self.reset
end
end
9 changes: 9 additions & 0 deletions lib/email_verifier/exceptions.rb
@@ -0,0 +1,9 @@

module EmailVerifier

class NoMailServerException < StandardError; end
class OutOfMailServersException < StandardError; end
class NotConnectedException < StandardError; end
class FailureException < StandardError; end

end
32 changes: 32 additions & 0 deletions lib/email_verifier/validates_email_realness.rb
@@ -0,0 +1,32 @@
require "active_record"

module EmailVerifier
module ValidatesEmailRealness

module Validator
class EmailRealnessValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
record.errors.add attribute, 'must point to a real mail account' unless EmailVerifier.check(value)
rescue EmailVerifier::OutOfMailServersException
record.errors.add attribute, 'appears to point to dead mail server'
rescue EmailVerifier::NoMailServerException
record.errors.add attribute, "appears to point to domain which doesn't handle e-mail"
rescue EmailVerifier::FailureException
record.errors.add attribute, "could not be checked if is real"
end
end
end
end

module ClassMethods
def validates_email_realness_of(*attr_names)
validates_with ActiveRecord::Base::EmailRealnessValidator, _merge_attributes(attr_names)
end
end

end
end

ActiveRecord::Base.send(:include, EmailVerifier::ValidatesEmailRealness::Validator)
ActiveRecord::Base.send(:extend, EmailVerifier::ValidatesEmailRealness::ClassMethods)
3 changes: 3 additions & 0 deletions lib/email_verifier/version.rb
@@ -0,0 +1,3 @@
module EmailVerifier
VERSION = "0.0.1"
end
1 change: 1 addition & 0 deletions rails/init.rb
@@ -0,0 +1 @@
require 'email_verifier'

0 comments on commit ab853b1

Please sign in to comment.