email_checker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 287b145d67ecd07f435e662d23557d5383365e9e
4
+ data.tar.gz: 91062933ded8f2ff2e4081cf148cde5eac0bf137
5
+ SHA512:
6
+ metadata.gz: 5eacfdcdb7e892fec853b94e8d880161a379c76e806785b2530693825473360c6a72742440432479ae0dc9aef17569c1a2bbe2e40432b68af783f7ae12b791a2
7
+ data.tar.gz: df4ae4f2a51a082e59451dd631060bf5d37fcb2367b93337ec9ba66c81b668b33f0a1a280f890a822cabaecb55b5a4f29f333738cde212d677270cfc484f6887
data/.editorconfig ADDED
@@ -0,0 +1,20 @@
1
+ # EditorConfig helps developers define and maintain consistent
2
+ # coding styles between different editors and IDEs
3
+ # editorconfig.org
4
+
5
+ root = true
6
+
7
+ [*]
8
+
9
+ # Change these settings to your own preference
10
+ indent_style = space
11
+ indent_size = 2
12
+
13
+ # We recommend you to keep these unchanged
14
+ end_of_line = lf
15
+ charset = utf-8
16
+ trim_trailing_whitespace = true
17
+ insert_final_newline = true
18
+
19
+ [*.md]
20
+ trim_trailing_whitespace = false
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ Style/IndentationConsistency:
2
+ Enabled: false
3
+
4
+ Style/SingleSpaceBeforeFirstArg:
5
+ Enabled: false
6
+
7
+ Style/AlignHash:
8
+ Enabled: false
9
+
10
+ Style/SpaceAroundEqualsInParameterDefault:
11
+ Enabled: false
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Style/EmptyLinesAroundBlockBody:
17
+ Enabled: false
18
+
19
+ Metrics/LineLength:
20
+ Max: 1000
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ email_checker
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in email_checker.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Genaro Madrid
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Email Checker
2
+
3
+ Check if an email address is can receive E-mails.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'email_checker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install email_checker
20
+
21
+ ## How do this gem works
22
+
23
+ For an email address to receive e-mails, it has to meet the following:
24
+
25
+ 1. The domain must have MX records.
26
+ 2. The MX records must point to a valid server and
27
+ 3. That server must have the corresponding A records.
28
+
29
+ Now, that does not ensure that the email actually exists in the server, for that, we have to *pretend* to send an e-mail and check if the seerver is OK with it (respond with status 250).
30
+ If you want to read more about it you can read the [RFC 821](https://tools.ietf.org/html/rfc821).
31
+
32
+ For this gem to *pretend* to send an email you have to provide a valid email address from whitch the email "will be sent". If you don't, the server might reject the message without checking if the email exists. In this gem that is the `verifier_email`.
33
+
34
+ It is noteworthy that this method can result in your server being blacklisted since its known as a spamming technique.
35
+
36
+ ## Usage
37
+
38
+ You can provide the `verifier_email` by passign it to the `check` method in the second parameter:
39
+
40
+ ```ruby
41
+ EmailChecker.check(email, verifier_email=nil)
42
+ ```
43
+
44
+ Or in an initializer:
45
+
46
+ ```ruby
47
+ EmailChecker.config do |config|
48
+ config.verifier_email = 'realname@realdomain.com'
49
+ end
50
+
51
+ EmailChecker.check(email)
52
+ ```
53
+
54
+ ## Contributors
55
+
56
+ `email_checker` it's inspired on [Kamilc](https://github.com/kamilc) [email_verifier](https://github.com/kamilc/email_verifier) gem. The diference is that `email_checker` validates the domain MX records and its server before it pretends to send an email, this way it is less posible that your server get blacklisted.
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/email_checker/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :console do
4
+ exec 'irb -r email_checker -I ./lib'
5
+ end
6
+
data/bump ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env python
2
+ import re, glob, sys, os
3
+
4
+ __USAGE__ = \
5
+ """BUMP is a semantic versioning bump script which accepts the following
6
+ mutually exclusive arguments:
7
+ -m - a "major" version bump equal to +1.0.0
8
+ -n - a "minor" version bump equal to +0.1.0
9
+ -p - a "patch" version bump equal to +0.0.1
10
+ -h - a "hot fix" version bump equal to +0.0.1
11
+
12
+ All of these options allow for the -r flag, which indicates that the state
13
+ is a RELEASE not a SNAPSHOT. If -r is not specified, then -SNAPSHOT is
14
+ appended to the updated version string."""
15
+
16
+ __INITIAL__ = ['0', '0', '1']
17
+
18
+
19
+ if __name__ == "__main__":
20
+ v = []
21
+ try:
22
+ version_file = glob.glob("lib/*/version.rb")[0]
23
+ raw_v = re.search(r'VERSION = \'(.*)\'', open(version_file).read(), re.M|re.I|re.S).group(1)
24
+ v = re.split(re.compile("\.|-"), raw_v)
25
+ v = v[0:3]
26
+ map(int, v)
27
+
28
+ except ValueError:
29
+ print("failed to parse the existing VERSION file, assuming v 0.0.1")
30
+ v = ['0', '0', '1']
31
+
32
+ except FileNotFoundError:
33
+ print("failed to find a VERSION file, assuming v 0.0.0")
34
+ v = ['0', '0', '0']
35
+
36
+ op = ''
37
+ try:
38
+ op = sys.argv[1]
39
+ except:
40
+ print(__USAGE__)
41
+ sys.exit(-1)
42
+
43
+ if(op == '-m'):
44
+ v = [str(int(v[0])+1), '0', '0']
45
+
46
+ elif(op == '-n'):
47
+ v = [v[0], str(int(v[1])+1), '0']
48
+
49
+ elif(op == '-p' or op == '-h'):
50
+ v = [v[0], v[1], str(int(v[2])+1)]
51
+
52
+ else:
53
+ print(__USAGE__)
54
+ sys.exit(-1)
55
+
56
+ v = '.'.join(v)
57
+
58
+ if(op == '-h'):
59
+ os.system("git checkout -b hotfix/v%s master" % v)
60
+
61
+ else:
62
+ os.system("git checkout -b release/v%s develop" % v)
63
+
64
+ os.system("bump set %s" % v)
65
+
66
+ v += "\n"
67
+
68
+ print(v)
69
+
70
+ sys.exit(0)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'email_checker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'email_checker'
8
+ spec.version = EmailChecker::VERSION
9
+ spec.authors = ['Genaro Madrid']
10
+ spec.email = ['genmadrid@gmail.com']
11
+ spec.summary = %q{Check if an email address is can receive E-mails.}
12
+ spec.description = %q{Validates, at some degree, that the email you want to send to it's valid and exists.}
13
+ spec.homepage = 'https://github.com/genaromadrid/email_checker'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake', '~> 10.4'
23
+ spec.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.7'
24
+ spec.add_development_dependency 'pry', '~> 0.10', '>= 0.10.1'
25
+ spec.add_development_dependency 'pry-byebug', '~> 3.3', '>= 3.3.0'
26
+ spec.add_development_dependency 'bump', '~> 0.5', '>= 0.5.3'
27
+ end
@@ -0,0 +1,65 @@
1
+ require 'net/smtp'
2
+
3
+ module EmailChecker
4
+ class Checker
5
+ attr_reader :domain
6
+
7
+ def initialize(email)
8
+ @email = email
9
+ email_m = email.match(EMAIL_PATTERN)
10
+ @domain = Domain.new(email_m[2])
11
+ end
12
+
13
+ def email_exists_in_server?
14
+ mailfrom if EmailChecker.config.verifier_domain
15
+ rcptto.tap do
16
+ close_connection
17
+ end
18
+ ensure
19
+ close_connection
20
+ end
21
+
22
+ private
23
+
24
+ def smtp
25
+ return @smtp if @smtp
26
+ tries ||= 5
27
+ domain.mx_servers.each do |server|
28
+ @smtp = Net::SMTP.start(server[:address], 25, EmailChecker.config.verifier_domain)
29
+ return @smtp if @smtp
30
+ end
31
+ fail EmailChecker::ServerConnectionError, "Unable to connect to any of the mail servers for #{@email}"
32
+ rescue EmailChecker::ServerConnectionError => e
33
+ fail EmailChecker::ServerConnectionError, e.message
34
+ rescue => e
35
+ if (tries -= 1) > 0
36
+ retry
37
+ else
38
+ fail EmailChecker::FailureError, e.message
39
+ end
40
+ end
41
+
42
+ def mailfrom
43
+ ensure_250(smtp.mailfrom(EmailChecker.config.verifier_email))
44
+ end
45
+
46
+ def rcptto
47
+ ensure_250(smtp.rcptto(@email))
48
+ rescue => e
49
+ if e.message[/^550/]
50
+ return false
51
+ else
52
+ fail EmailChecker::FailureError, e.message
53
+ end
54
+ end
55
+
56
+ def ensure_250(smtp_return)
57
+ return true if smtp_return.status.to_i == 250
58
+ fail EmailChecker::FailureError, "Mail server responded with #{smtp_return.status} when we were expecting 250"
59
+ end
60
+
61
+ def close_connection
62
+ smtp.finish if smtp && smtp.started?
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,24 @@
1
+ module EmailChecker
2
+ module Config
3
+ class << self
4
+ attr_accessor :verifier_email,
5
+ :test_mode
6
+ attr_reader :verifier_domain
7
+
8
+ def reset
9
+ @verifier_email = nil
10
+ @test_mode = false
11
+ if defined?(Rails) && defined?(Rails.env) && Rails.env.test?
12
+ @test_mode = true
13
+ end
14
+ end
15
+
16
+ def verifier_email=(verifier_email)
17
+ @verifier_email = verifier_email
18
+ @verifier_domain = verifier_email.split('@').last
19
+ end
20
+ end
21
+
22
+ self.reset
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ module EmailChecker
2
+ class Domain
3
+
4
+ def initialize(domain)
5
+ @domain = domain
6
+ end
7
+
8
+ # Checks if the domian exists and has valid MX and A records
9
+ #
10
+ # @return [type] [description]
11
+ def valid?
12
+ return false unless @domain
13
+ Timeout::timeout(SERVER_TIMEOUT) do
14
+ return true if valid_mx_records?
15
+ return true if a_records?
16
+ end
17
+ rescue Timeout::Error, Errno::ECONNREFUSED
18
+ false
19
+ end
20
+
21
+ # Check if it has valid MX records and it can receive emails.
22
+ # The MX server exists and it has valid A records.
23
+ #
24
+ # @return [Boolean]
25
+ def valid_mx_records?
26
+ mx_servers.each do |server|
27
+ exchange_a_records = dns.getresources(server[:address], Resolv::DNS::Resource::IN::A)
28
+ return true if exchange_a_records.any?
29
+ end
30
+ false
31
+ end
32
+
33
+ # Check if the domain exists validation that has at least 1 A record.
34
+ #
35
+ # @return [Boolean]
36
+ def a_records?
37
+ a_records.any?
38
+ end
39
+
40
+ def a_records
41
+ @a_records ||= dns.getresources(@domain, Resolv::DNS::Resource::IN::A)
42
+ end
43
+
44
+ def mx_records
45
+ @mx_records ||= dns.getresources(@domain, Resolv::DNS::Resource::IN::MX).sort_by {|mx| mx.preference}
46
+ end
47
+
48
+ def mx_servers
49
+ return @mx_servers if @mx_servers
50
+ @mx_servers = []
51
+ mx_records.each do |mx|
52
+ @mx_servers.push(preference: mx.preference, address: mx.exchange.to_s)
53
+ end
54
+ @mx_servers
55
+ end
56
+
57
+ private
58
+
59
+ def dns
60
+ @dns ||= Resolv::DNS.new
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,4 @@
1
+ module EmailChecker
2
+ ServerConnectionError = Class.new StandardError
3
+ FailureError = Class.new StandardError
4
+ end
@@ -0,0 +1,3 @@
1
+ module EmailChecker
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'resolv'
2
+ require 'email_checker/version'
3
+
4
+ module EmailChecker
5
+ require 'email_checker/errors'
6
+
7
+ autoload :Config, 'email_checker/config'
8
+ autoload :Checker, 'email_checker/checker'
9
+ autoload :Domain, 'email_checker/domain'
10
+
11
+ EMAIL_PATTERN = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
12
+ SERVER_TIMEOUT = 5000
13
+
14
+ def self.check(email, verifier_email=nil)
15
+ config.verifier_email = verifier_email if verifier_email
16
+ checker = EmailChecker::Checker.new(email)
17
+ return false unless checker.domain.valid?
18
+ checker.email_exists_in_server?
19
+ end
20
+
21
+ def self.config
22
+ if block_given?
23
+ yield(EmailChecker::Config)
24
+ else
25
+ EmailChecker::Config
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_checker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Genaro Madrid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.1.7
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.1'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.1.7
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.10'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 0.10.1
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '0.10'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 0.10.1
81
+ - !ruby/object:Gem::Dependency
82
+ name: pry-byebug
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.3'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 3.3.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.3'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 3.3.0
101
+ - !ruby/object:Gem::Dependency
102
+ name: bump
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '0.5'
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.5.3
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.5'
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 0.5.3
121
+ description: Validates, at some degree, that the email you want to send to it's valid
122
+ and exists.
123
+ email:
124
+ - genmadrid@gmail.com
125
+ executables: []
126
+ extensions: []
127
+ extra_rdoc_files: []
128
+ files:
129
+ - ".editorconfig"
130
+ - ".gitignore"
131
+ - ".rspec"
132
+ - ".rubocop.yml"
133
+ - ".ruby-gemset"
134
+ - ".ruby-version"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - bump
140
+ - email_checker.gemspec
141
+ - lib/email_checker.rb
142
+ - lib/email_checker/checker.rb
143
+ - lib/email_checker/config.rb
144
+ - lib/email_checker/domain.rb
145
+ - lib/email_checker/errors.rb
146
+ - lib/email_checker/version.rb
147
+ homepage: https://github.com/genaromadrid/email_checker
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.2.2
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Check if an email address is can receive E-mails.
171
+ test_files: []