I'm trying to complete e script wich checks if a domain exists. But... it seems it doesn't work always, sometimes the domain exists but the script says that it doesn't.

list ($alias, $domain) = split ('[@]', $email);
$domain1= "www.".$domain;
$domain2= "smtp.".$domain;
$domain3= "mail.".$domain;
$domain4= "pop.".$domain;
$domain5= "mx.".$domain;
$check1=@fsockopen($domain1, 80, $errno, $errstr, 5);  
if (!$check1) { $check2=@fsockopen($domain2, 80, $errno, $errstr, 5); $check3=@fsockopen($domain3, 80, $errno, $errstr, 5); $check4=@fsockopen($domain4, 80, $errno, $errstr, 5); $check5=@fsockopen($domain5, 80, $errno, $errstr, 5); if (!$check3 and !$check2 and !$check4 and !$check5) { echo "THE DOMAIN DOES NOT EXIST"; }}

    Why would you be expecting smtp.domain.tld to be listening on port 80? SMTP runs on port 25 and POP3 on 110. I would suggest using [man]gethostbyname()[/man] to check if the domain exists, although this won't really help you decide which ports it's going to be listening on.

      You should not use the above code.

      Instead, look for an "A" or "MX" for the domain. If either exists (i.e. returns a valid IP), then the domain exists for mail (although it may not have a working SMTP server or accept mail).

      You should not assume that because the mail server doesn't have a listening port 25 at some particular instant in time that it never will.

      You should also not check port 80 on a mail server, because mail servers do not need to (and typically don't) have HTTP servers on them.

      The above example is rubbish because it's looking for specific host names in the domain, like mx and smtp, pop etc.

      There is no guarantee that a given domain will have such hosts - it could still accept mail.

      Anyway, who's to say that mydomain.com doesn't have its mail served by blargh.wibble.org.uk ?

      Just do an MX and A lookup (in that order).

      Of course, if you get a temporary error like server failure, you can't take that to mean the domain doesn't exist.

      Mark

        Thanks Mark and bubblnut... i just tried the following using your ideas:

        list ($alias, $domain) = split ('[@]', $email);
        $domain = "www.".$domain;
        $check = checkdnsrr($domain, ANY);
        if (!$check) { 
         echo "EMAIL/DOMAIN DOES NOT EXIST"; 
         }

        It's already better, but it doesn't work with the domain "manzoni.it" which exists and is used for email.

          And what happens if the $domain is something like "it.nestle.it"?

            Write a Reply...