+ Reply to Thread
Results 1 to 3 of 3

Thread: [PHP] Validate E-mail & Domain Checking

  1. #1
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    [PHP] Validate E-mail & Domain Checking

    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 Code:
    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 by Shadow121; 08-24-2010 at 11:53 AM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: [PHP] Validate E-mail & Domain Checking

    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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Re: [PHP] Validate E-mail & Domain Checking

    I got it working, thanks :D

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

+ Reply to Thread

Similar Threads

  1. Can\\\'t validate email
    By glennsdesign in forum Free Hosting
    Replies: 1
    Last Post: 03-07-2010, 07:04 AM
  2. Can anybody help me to validate this site?
    By ciril tomy in forum Review My Site
    Replies: 1
    Last Post: 09-01-2009, 05:42 PM
  3. How can I validate my contact form?
    By ciril tomy in forum Review My Site
    Replies: 1
    Last Post: 08-11-2009, 10:19 AM
  4. Checking mail query
    By sabinkumar in forum Free Hosting
    Replies: 2
    Last Post: 11-26-2008, 07:44 AM
  5. Validate mp3 url link
    By oracle in forum Programming Help
    Replies: 3
    Last Post: 03-24-2008, 02:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers