Sha256: 07c1e1f5e3486084fff2172ec972bc7b218c4a54f85b7decffea11aac6041865

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

module MailAutoconfig

  # Email address that we're going to investigate
  class EmailAddress
    attr_reader :address

    # @param address [String] the email address to look up
    def initialize(address)
      @address = address
    end

    # Fetch the client configuration for this email address.
    # First of all try the domain determined by {#domain}. If nothing is found there,
    # check the domain specified in {#primary_mx_domain}. Returns false if none found.
    # @return [MailAutoconfig::ClientConfig] the client configuration for this address
    def client_config
      @client_config ||= begin
        config = MailAutoconfig::ClientConfig.search(domain)
        config ||= MailAutoconfig::ClientConfig.search(primary_mx_domain)
        config.email_address = self if config
        config
      end
    end

    # The local part of the emai address (before the @ symbol).
    # Useful for usage with the %EMAILLOCALPART% substitution.
    # @return [String] the local part of the email address
    def local_part
      @local_part ||= @address.split('@', 2).first
    end

    # The domain of the email address (the part after the @ symbol)
    # @return [String] the domain of the email address
    def domain
      @domain ||= @address.split('@', 2).last
    end
  
    # Finds the primary MX domain for this address. Would change gmail-smtp-in.l.google.com to google.com
    # @return [String] the domain of the pimary MX record for this address
    def primary_mx_domain
      @primary_mx_domain ||= mx_records.first.split(".")[-2..-1].join(".") # Not very nice to 2nd level domains
    end

    private

    # Finds MX records for the associated email address, ordered by preference.
    # @return [Array] MX records for this email address
    def mx_records
      @mx_records ||= Resolv::DNS.open.getresources(domain, Resolv::DNS::Resource::IN::MX).sort_by(&:preference).map{ |r| r.exchange.to_s }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mail_autoconfig-0.0.1 lib/mail_autoconfig/email_address.rb