Sha256: a0ebe0d0d7a45b791da485c665d1cf2bfc95e85ae96e80f166df08fc8c2ea412

Contents?: true

Size: 1.29 KB

Versions: 15

Compression:

Stored size: 1.29 KB

Contents

require "valid_email2"
require "resolv"
require "mail"

module ValidEmail2
  class Address
    attr_accessor :address

    def initialize(address)
      @parse_error = false
      @raw_address = address

      begin
        @address = Mail::Address.new(address)
      rescue Mail::Field::ParseError
        @parse_error = true
      end
    end

    def valid?
      return false if @parse_error

      if address.domain && address.address == @raw_address
        domain = address.domain
        # Valid address needs to have a dot in the domain but not start with a dot
        !!domain.match(/\./) && !domain.match(/\.{2,}/) && !domain.match(/^\./)
      else
        false
      end
    end

    def disposable?
      valid? && domain_is_in?(ValidEmail2.disposable_emails)
    end

    def blacklisted?
      valid? && domain_is_in?(ValidEmail2.blacklist)
    end

    def valid_mx?
      return false unless valid?

      mx = []

      Resolv::DNS.open do |dns|
        mx.concat dns.getresources(address.domain, Resolv::DNS::Resource::IN::MX)
      end

      mx.any?
    end

    private

    def domain_is_in?(domain_list)
      address_domain = address.domain.downcase
      domain_list.any? { |domain|
        address_domain.end_with?(domain) && address_domain =~ /\A(?:.+\.)*?#{domain}\z/
      }
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
valid_email2-2.0.2 lib/valid_email2/address.rb
valid_email2-2.0.1 lib/valid_email2/address.rb
valid_email2-2.0.0 lib/valid_email2/address.rb
valid_email2-1.2.22 lib/valid_email2/address.rb
valid_email2-1.2.21 lib/valid_email2/address.rb
valid_email2-1.2.20 lib/valid_email2/address.rb
valid_email2-1.2.19 lib/valid_email2/address.rb
valid_email2-1.2.18 lib/valid_email2/address.rb
valid_email2-1.2.17 lib/valid_email2/address.rb
valid_email2-1.2.16 lib/valid_email2/address.rb
valid_email2-1.2.15 lib/valid_email2/address.rb
valid_email2-1.2.14 lib/valid_email2/address.rb
valid_email2-1.2.13 lib/valid_email2/address.rb
valid_email2-1.2.12 lib/valid_email2/address.rb
valid_email2-1.2.11 lib/valid_email2/address.rb