Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Determine if domain is actually used for email

by vit (Pilgrim)
on Oct 04, 2011 at 15:25 UTC ( #929571=perlquestion: print w/ replies, xml ) Need Help??
vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I will pose my task. I need to be able to determine whether a domain is used for email or not. Many domains have no website, but they could still be used for email only. It is easy to know by just checking the DNS records for an “MX record” which is required to be able to use it for email purposes. However, many DNS operators automatically make an MX record without using it. So the question is, how can we detect that a domain is actually used for email?
I know that something can be done with Net::Nslookup, but I am wondering if there is a straightforward Perl implemented solution.

Comment on Determine if domain is actually used for email
Re: Determine if domain is actually used for email
by blue_cowdawg (Monsignor) on Oct 04, 2011 at 15:33 UTC
        I need to be able to determine whether a domain is used for email or not.

    You're on the right track.

    In order for a domain to be valid for email you are required to have an MX record for it. The majority of MTAs will fail to send to your domain if you do not have an MX record. The other thing you can do is connect to port 25 for that domain name and check to see if an MTA answers.

    $ telnet some.domain.tld 25 Connected to some.domain.tld. Escape character is '^]'. 220 some.domain.tld ESMTP Postfix quit 221 2.0.0 Bye Connection closed by foreign host.

    There's a sample session for you to see.

    As far as Perl modules go to help with this check out Net::DNS and especially look at the mx method for the default resolver. Then there is cpan:://Net::Telnet module which you can customize to connect to port 25.

    Hope this helps...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Determine if domain is actually used for email
by Limbic~Region (Chancellor) on Oct 04, 2011 at 15:39 UTC
    vit,
    It has been many years since I read RFC 821/2821, but I seem to recall that certain email addresses were mandatory - such as postmaster. If you truly want to verify if there is a mail server listening on the IP address associated with the MX record, you could do the equivalent of
    telnet 10.10.10.10 25 helo mydomain.com vrfy postmaster@yourdomain.com

    Now of course, many mail servers have the vrfy function turned off because it can be used by spammers for nefarious purposes but the fact that you are communicating with the server on port 25 in a fashion resembling SMTP should be a pretty good indication they handle mail.

    I didn't write code that you could use for a reason. Your post itself looks like it might be for spamming purposes. If you don't have someone's email address then why are you checking to see if their domain accepts mail?

    Cheers - L~R

      I didn't write code that you could use for a reason. Your post itself looks like it might be for spamming purposes. If you don't have someone's email address then why are you checking to see if their domain accepts mail?

      I am doing a project for a large domain registrar in Europe. They collect statistical info on domains which includes website category, whether it is parked, for sale, etc. and they are also interested to know if the domain is used solely for emails which happens very frequently, as I wrote in my original post.
      So if you could be so kind and help me with the code I will appreciate it very much.
        vit,
        Interesting. There was a project nearly identical to this on one of the freelance boards I use to pick up jobs. I didn't bid on it because the poster specifically stated that automated routines would not be allowed - each record had to be checked manually.

        I would probably use IO::Socket::Telnet. Something along the lines of this completely untested code:

        use IO::Socket::Telnet; my $socket = IO::Socket::Telnet->new( PeerAddr => 'random.server.org', PeerPort => 25, Timeout => 3 ); die "Not an email server\n" if ! $socket; $socket->send("helo mydomain.com"); my $resp; $socket->recv(my $resp, 4096) or die $!; die "Not an email server\n" if ! defined $resp || ($resp !~ /^220 / && + $resp !~ /^250 /);
        this link may be of use in determining other SMTP response codes that indicate it is in fact an SMTP server but not necessarily behaving as you might expect.

        Cheers - L~R

Re: Determine if domain is actually used for email
by Your Mother (Canon) on Oct 04, 2011 at 17:49 UTC

    Email::Valid does this; for domains or as a part of validating addresses: "mx ( <ADDRESS>|<DOMAIN> ) This method accepts an email address or domain name and determines whether a DNS record (A or MX) exists for it."

    perl -MEmail::Valid -le 'print Email::Valid->mx("gmail.com") ? "yep" : + "nope"' yep perl -MEmail::Valid -le 'print Email::Valid->mx("tacohut.co.uk.jp.cx") + ? "yep" : "nope"' nope
      Your Mother,
      Except that vit indicated in the original post that the presence of an MX record is not a reliable source of the domain's ability to receive email. Apparently DNS administrators are registering MX records all willy nilly.

      Cheers - L~R

        Sure... but unless I'm misled, with UDP there simply is no way to know for sure excepting an out of band response—a visit to a tokenized URI sent in a message or a return message from the same domain/address. So no checks will be guaranteed. "Heroic" messages to circumvent misused standards / client-fail seems a mistake.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://929571]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-12-26 18:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls