Class: EmailVerifier::Checker
- Inherits:
-
Object
- Object
- EmailVerifier::Checker
- Defined in:
- lib/email_verifier/checker.rb
Instance Method Summary collapse
- #connect ⇒ Object
- #ensure_250(smtp_return) ⇒ Object
- #ensure_connected ⇒ Object
-
#initialize(address) ⇒ Checker
constructor
Returns server object for given email address or throws exception Object returned isn’t yet connected.
- #is_connected ⇒ Object
- #list_mxs(domain) ⇒ Object
- #mailfrom(address) ⇒ Object
- #next_server ⇒ Object
- #rcptto(address) ⇒ Object
- #verify ⇒ Object
Constructor Details
#initialize(address) ⇒ 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
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/email_verifier/checker.rb', line 9 def initialize(address) @email = address _, @domain = address.split("@") @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 _, @user_domain = @user_email.split "@" end |
Instance Method Details
#connect ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/email_verifier/checker.rb', line 35 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, @user_domain return true rescue EmailVerifier::OutOfMailServersException => e raise EmailVerifier::OutOfMailServersException, e. rescue => e retry end end |
#ensure_250(smtp_return) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/email_verifier/checker.rb', line 81 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 |
#ensure_connected ⇒ Object
77 78 79 |
# File 'lib/email_verifier/checker.rb', line 77 def ensure_connected raise EmailVerifier::NotConnectedException.new("You have to connect first") if @smtp.nil? end |
#is_connected ⇒ Object
31 32 33 |
# File 'lib/email_verifier/checker.rb', line 31 def is_connected !@smtp.nil? end |
#list_mxs(domain) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/email_verifier/checker.rb', line 22 def list_mxs(domain) return [] unless 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 |
#mailfrom(address) ⇒ Object
57 58 59 60 61 |
# File 'lib/email_verifier/checker.rb', line 57 def mailfrom(address) ensure_connected ensure_250 @smtp.mailfrom(address) end |
#next_server ⇒ Object
48 49 50 |
# File 'lib/email_verifier/checker.rb', line 48 def next_server @servers.shift end |
#rcptto(address) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/email_verifier/checker.rb', line 63 def rcptto(address) ensure_connected begin ensure_250 @smtp.rcptto(address) rescue => e if e.[/^550/] return false else raise EmailVerifier::FailureException.new(e.) end end end |
#verify ⇒ Object
52 53 54 55 |
# File 'lib/email_verifier/checker.rb', line 52 def verify self.mailfrom @user_email self.rcptto(@email) end |