Class: ValidateEmail

Inherits:
Object
  • Object
show all
Defined in:
lib/valid_email/validate_email.rb

Class Method Summary collapse

Class Method Details

.ban_disposable_email?(value) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/valid_email/validate_email.rb', line 52

def ban_disposable_email?(value)
  r = false
  begin
    m = Mail::Address.new(value)
    r = !BanDisposableEmailValidator.config.include?(m.domain) if m.domain
  rescue Exception => e
    r = false
  end
  
  r
end

.mx_valid?(value, fallback = false) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/valid_email/validate_email.rb', line 30

def mx_valid?(value, fallback=false)
  r = false
  begin
    m = Mail::Address.new(value)
    if m.domain
      mx = []
      Resolv::DNS.open do |dns|
        mx.concat dns.getresources(m.domain, Resolv::DNS::Resource::IN::MX)
        mx.concat dns.getresources(m.domain, Resolv::DNS::Resource::IN::A) if fallback
      end
      r = mx.size > 0
    end
  rescue Exception =>e
    r = false
  end
  r
end

.mx_valid_with_fallback?(value) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/valid_email/validate_email.rb', line 48

def mx_valid_with_fallback?(value)
  mx_valid?(value, true)
end

.valid?(value, user_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/valid_email/validate_email.rb', line 5

def valid?(value, user_options={})
  options = { :mx => false, :message => nil }.merge!(user_options)
  r       = false
  begin
    m = Mail::Address.new(value)
    # We must check that value contains a domain and that value is an email address
    r = m.domain && m.address == value

    # Check that domain consists of dot-atom-text elements > 1
    # In mail 2.6.1, domains are invalid per rfc2822 are parsed when they shouldn't
    # This is to make sure we cover those cases
    domain_dot_elements = m.domain.split(/\./)
    r &&= domain_dot_elements.none?(&:blank?) && domain_dot_elements.size > 1

    # Check if domain has DNS MX record
    if r && options[:mx]
      require 'valid_email/mx_validator'
      r &&= mx_valid?(email)
    end
  rescue Exception => e
    r = false
  end
  r
end