[PHP] Validate E-mail & Domain Checking

Shadow121

Member
Messages
901
Reaction score
0
Points
16
I'm working on a mass e-mailer script for my site and I can't seem to get this one little thing to work.

I have 4 users:
1 with yahoo, one with a co.cc domain, one with a non existent domain and another with Gmail.

It sends to the co.cc and the non existent domains. The other two I have opted out.

I want it to send to the co.cc because it is valid, and not the non existent one because it's not valid But it's not working. Can anyone help?

PHP:
function _validate_mail($email)
    {
        // First, we check that there's one @ symbol,
        // and that the lengths are right.
        if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
            // Email invalid because wrong number of characters
            // in one section or wrong number of @ symbols.
            return false;
        }
        // Split it into sections to make life easier
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
?'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
                return false;
            }
        }
        // Check if domain is IP. If not,
        // it should be valid domain name
        if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
?([A-Za-z0-9]+))$", $domain_array[$i])) {
                    return false;
                }
            }
        }
        $isco_cc = (stristr($email_array[1],'.co.cc')) ? true : false;
        if(!$this->checkDomain($email_array[1],$isco_cc)){
            return false;
        }else{
            return true;
        }
    }
    function checkDomain($domain, $iscocc = false)
    {
        
        $options = array(
    	    CURLOPT_RETURNTRANSFER => true,     // return web page
    	    CURLOPT_HEADER         => false,    // don't return headers
    	    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    	    CURLOPT_ENCODING       => "",       // handle all encodings
    	    CURLOPT_USERAGENT      => "mail_checker",
    	    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    	    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    	    CURLOPT_TIMEOUT        => 120,      // timeout on response
    	    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    	);
        if($iscocc){
            $url = "http://".$domain;
        }else{
            $url = "http://www.whois.net/getNB.cfm?domain_name=".$domain;
        }
        $ch = curl_init($url);
        curl_setopt_array($ch, $options);
        $content = curl_exec($ch);
        curl_close($ch);
        if(!preg_match("/\b(a|A)vailable\b/",$content)):
            return true;
        else:
            return false;
        endif;
    }
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Don't use ereg, it's deprecated. I would say to use preg_match, but in this case you should be using one of the filter functions, specifically filter_var with the FILTER_VALIDATE_EMAIL filter.

What's the invalid e-mail address? We need one to test, and it might as well be that one, since it's invalid. Any that we might pick, such as nobody@nyet.net, could fail your test.

Rather than relying on a WHOIS (which will be problematic for various reasons), try one of the DNS network functions, such as dns_get_record. If a domain name has an A, AAAA, A6, MX or CNAME record, or a SRV record for _stmp._tcp.$domain, it's a valid host.
 

Shadow121

Member
Messages
901
Reaction score
0
Points
16
I got it working, thanks :D

I also found out that the domain check wasn't the only problem =P
 
Top