need help to verify e-mail address

Discussion in 'PHP' started by srikanthever4u, Oct 21, 2008.

  1. #1
    verify e-mail address ?
    plz tell me how to verify email address using dns
     
    srikanthever4u, Oct 21, 2008 IP
  2. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is a function I've been using in a few projects, I just added the last few lines for you to also check if the hostname is valid. :)

    function isValidEmail($address) {
      $valid_tlds = array('arpa', 'biz', 'com', 'edu', 'gov', 'int', 'mil', 'net', 'org',
        'ad', 'ae', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'as', 'at', 'au',
        'aw', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'bj', 'bm', 'bn', 'bo',
        'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cc', 'cf', 'cd', 'cg', 'ch', 'ci',
        'ck', 'cl', 'cm', 'cn', 'co', 'cr', 'cs', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj',
        'dk', 'dm', 'do', 'dz', 'ec', 'ee', 'eg', 'eh', 'er', 'es', 'et', 'fi', 'fj', 'fk',
        'fm', 'fo', 'fr', 'fx', 'ga', 'gb', 'gd', 'ge', 'gf', 'gh', 'gi', 'gl', 'gm', 'gn',
        'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu',
        'id', 'ie', 'il', 'in', 'io', 'iq', 'ir', 'is', 'it', 'jm', 'jo', 'jp', 'ke', 'kg',
        'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk',
        'lr', 'ls', 'lt', 'lu', 'lv', 'ly', 'ma', 'mc', 'md', 'mg', 'mh', 'mk', 'ml', 'mm',
        'mn', 'mo', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'mv', 'mw', 'mx', 'my', 'mz', 'na',
        'nc', 'ne', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nt', 'nu', 'nz', 'om', 'pa',
        'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'pt', 'pw', 'py', 'qa', 're',
        'ro', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl',
        'sm', 'sn', 'so', 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tf', 'tg', 'th',
        'tj', 'tk', 'tm', 'tn', 'to', 'tp', 'tr', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk',
        'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws', 'ye',
        'yt', 'yu', 'za', 'zm', 'zr', 'zw', 
        'eu', 'info');
    
      $address = strtolower($address);
    
      if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $address)) {
        return false;
      } 
    
      $name_domain = explode("@", $address);
    
      if (count($name_domain) != 2) { return false; }
    
      $domain_parts = explode(".", $name_domain[1]);
      if (count($domain_parts) < 2) { return false; }
      if (!in_array($domain_parts[count($domain_parts) - 1], $valid_tlds)) {
        return false;
      }
    
      if (gethostbyname($name_domain[1]) == $name_domain[1]) {
        return false;
      }
    
      return true;
    }
    PHP:
     
    keyaa, Oct 21, 2008 IP
  3. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    do you mean verifying that it's a valid email, or using activation codes?
     
    Kyosys, Oct 21, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I think he want to verify if the email exist?
     
    ads2help, Oct 21, 2008 IP
  5. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    He wanted a solution via DNS, which is what the above code does. It allows nothing more than checking the validity of a domain, not of an e-mail address. (The function also checks if the format of the given e-mail address is valid)
    I wouldn't be surprised if he actually meant to check the existance of an e-mail address via SMTP, which theoretically could be done, but due to different mailserver implementations and often missing error codes in case an e-mail address doesn't exist on a server this, imo, cannot be implemented in a reliable way.
     
    keyaa, Oct 21, 2008 IP
  6. srikanthever4u

    srikanthever4u Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    i need to verify email address through dns,mail server

    like this site:

    www.verify-email.org
     
    srikanthever4u, Oct 21, 2008 IP
  7. srikanthever4u

    srikanthever4u Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    hi it working for dns check,mail sever checking after how to valid that user?
    i will try for fsocket program .
    in this process
    not connect to the RCPT TO
    any one know help me


    <?php
    $email1="srikanth.warriorz@gmail.com";
    validate_email($email1);
    function validate_email($email)
    {
    $mailparts=explode("@",$email);
    echo $hostname = $mailparts[1];
    echo "<br>";
    // validate email address syntax
    $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
    $b_valid_syntax=eregi($exp, $email);

    // get mx addresses by getmxrr
    $b_mx_avail=getmxrr( $hostname, $mx_records, $mx_weight );
    $b_server_found=0;

    if($b_valid_syntax && $b_mx_avail)
    {
    // copy mx records and weight into array $mxs
    $mxs=array();

    for($i=0;$i<count($mx_records);$i++)
    {
    echo $mxs[$mx_weight[$i]]=$mx_records[$i];echo "<br>";
    }

    // sort array mxs to get servers with highest prio
    ksort ($mxs, SORT_NUMERIC );
    reset ($mxs);

    while (list ($mx_weight, $mx_host) = each ($mxs) )
    {
    echo "<br>";
    echo "<b>we</b><br>".$mx_weight."<br><b>host</b><br>".$mx_host;
    echo "<br>";
    if($b_server_found == 0)
    {
    //try connection on port 25
    $fp = @fsockopen($mx_host,25, $errno, $errstr, 2);
    if($fp)
    {
    echo "server connected";echo "<br>";
    $ms_resp="";
    // say HELO to mailserver
    echo $ms_resp.=send_command($fp, "HELO yahoo.com");
    echo "<br>";
    // initialize sending mail
    $ms_resp.=send_command($fp, "MAIL FROM:<support@yahoo.com>");
    // try receipent address, will return 250 when ok..
    echo $rcpt_text=send_command($fp, "RCPT TO:<".$email.">");
    echo $ms_resp.=$rcpt_text;

    if(substr( $rcpt_text, 0, 3) == "250")
    echo $b_server_found=1;

    // quit mail server connection
    $ms_resp.=send_command($fp, "QUIT");
    echo $ms_resp;
    fclose($fp);
    }
    }
    }


    }
    return $b_server_found;
    }

    function send_command($fp, $out){

    fwrite($fp, $out . "\r\n");
    return get_data($fp);
    }

    function get_data($fp){
    $s="";
    stream_set_timeout($fp, 2);

    for($i=0;$i<2;$i++)
    $s.=fgets($fp, 1024);

    return $s;
    }

    // support windows platforms
    if (!function_exists ('getmxrr') ) {
    function getmxrr($hostname, &$mxhosts, &$mxweight) {
    if (!is_array ($mxhosts) ) {
    $mxhosts = array ();
    }

    if (!empty ($hostname) ) {
    $output = "";
    @exec ("nslookup.exe -type=MX $hostname.", $output);
    $imx=-1;

    foreach ($output as $line) {
    $imx++;
    $parts = "";
    if (preg_match ("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts) ) {
    $mxweight[$imx] = $parts[1];
    $mxhosts[$imx] = $parts[2];
    }
    }
    return ($imx!=-1);
    }
    return false;
    }
    }

    ?>
     
    srikanthever4u, Oct 22, 2008 IP
  8. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    You do it like this :)
     
    elias_sorensen, Oct 23, 2008 IP
  9. srikanthever4u

    srikanthever4u Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    it not working
     
    srikanthever4u, Oct 23, 2008 IP
  10. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #10
    And you're totaly sure that your script is working?
    That if I made works perfectly.

    You can test it by replacing the function with:
    function validate_email($email){
    if($email == "error"){
    return false;
    }else{
    return true;
    }

    It should echo "E-mail is not valid" if you write "error" as the email, if not, it shouldn't return anything :)
     
    elias_sorensen, Oct 23, 2008 IP
  11. srikanthever4u

    srikanthever4u Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #11
    hi
    it is working for gmail only
    i want test for all domain types and popp3 ,yahoo is not working
     
    srikanthever4u, Oct 23, 2008 IP