Module: BulkMail

Defined in:
lib/bulkmail/dns_mx.rb,
lib/bulkmail/message.rb

Defined Under Namespace

Classes: Message

Class Method Summary collapse

Class Method Details

.smtp_servers_for_domain(domain) ⇒ Object

Performs a DNS query for a domain’s MX records and returns an array of SMTP servers for the domain ordered by preference. If no records are found an error is raised.



9
10
11
12
13
14
15
16
17
# File 'lib/bulkmail/dns_mx.rb', line 9

def self.smtp_servers_for_domain(domain)
  res = Net::DNS::Resolver.new
  mx = Net::DNS.mx(domain, res, 'IN')
  if mx
    mx.sort {|x,y| y.preference <=> x.preference}.map {|rr| rr.exchange}
  else
    raise RuntimeError, "Could not locate MX records for domain #{domain}."
  end
end