Sha256: fd3167cff3f6614ca8bdad38e12b090b335b8e110d06b28b30a719e73e022119

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 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
        tree = address.send(:tree)

        # Valid address needs to have a dot in the domain
        tree.domain.dot_atom_text.elements.size > 1
      else
        false
      end
    end

    def disposable?
      valid? && ValidEmail2.disposable_emails.include?(address.domain)
    end

    def blacklisted?
      valid? && ValidEmail2.blacklist.include?(address.domain)
    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
valid_email2-1.1.4 lib/valid_email2/address.rb
valid_email2-1.1.3 lib/valid_email2/address.rb
valid_email2-1.1.2 lib/valid_email2/address.rb
valid_email2-1.1.1 lib/valid_email2/address.rb
valid_email2-1.1.0 lib/valid_email2/address.rb