in reply to gethostbyname() and www prefix

All the advice above is very good, but you're probably wondering "how do I check for an MX record in perl?" Well, it's pretty easy using Net::DNS.
use Net::DNS; my $resolver = Net::DNS::Resolver->new; my $domain = "perlmonks.org"; my $query = $resolver->query($domain,"MX"); if ($query) { # They have an MX record, send the mail. } elsif (gethostbyname($domain)) { # No MX record, but the host exists. Send the # mail. } else { # No host, and no MX. No mail for you! }
Update: If we don't find an MX record, we now check to see if that address exists using gethostbyname(), as it's valid to have hosts without MX records. Thanks to Kanji for spotting this.

Cheers,
Paul

Replies are listed 'Best First'.
Re: Re: gethostbyname() and www prefix
by tachyon (Chancellor) on Oct 08, 2001 at 13:35 UTC

    You can also do it like this to be a trifle more complete:

    use Net::DNS; $res = new Net::DNS::Resolver; warn "Net::DNS error $@\n" if $@; # on some systems without the required config files you will need to # direct Net::DNS to your DNS nameserver(s) or it will timeout $res->nameservers( $prinary_nameserver_ip_address, $secondary... ) if +$res; sub dns_query { return undef unless $res; my $domain = shift; $packet = $res->send($domain, 'MX') or warn $res->errorstring; return 1 if $packet->header->ancount; my $packet = $res->send($domain, 'A') or warn $res->errorstring; return 1 if $packet->header->ancount; return 0; } print "OK DNS $domain" if dns_query($domain);

    Kanji notes my slack duplication of identical code. It is more maintainable to do this:

    sub dns_query { return undef unless $res; my $domain = shift; for my $dns ( 'MX', 'A' ) { $packet = $res->send($domain, $dns ) or warn $res->errorstring +; return 1 if $packet->header->ancount; } return 0; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print