home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / validemail.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  1.9 KB  |  79 lines

  1. ValidateEmail 
  2.  
  3. This code is used to validate that an email address can actually accept email. Call validateEmail() and pass it in an email address string like "jon@clearink.com". It will return an array. $return[0] is whether it was a valid email or not and $return[1] is the SMTP error message if there was 
  4.  
  5.  
  6. <?php
  7.  
  8. /*
  9.  By: Jon S. Stevens jon@clearink.com
  10.  Copyright 1998-1999 Jon S. Stevens, Clear Ink
  11.  This code has all the normal disclaimers.
  12.  It is free for any use, just keep the credits intact.
  13. */
  14.  
  15. function validateEmail ( $email )
  16. {
  17.     global $SERVER_NAME;
  18.     $return = array ( false, "" );
  19.     list ( $user, $domain )  = split ( "@", $email, 2 );
  20.     $tld = $domain;
  21.     if ( checkdnsrr ( $tld, "MX" ) )
  22.     {
  23.         if ( getmxrr ( $tld, $mxhosts, $weight ) )
  24.         {
  25.             for ( $i = 0; $i < count ( $mxhosts ); $i++ )
  26.             {
  27.                 $fp = fsockopen ( $mxhosts[$i], 25 );
  28.                 if ( $fp )
  29.                 {
  30.                     $s = 0;
  31.                     $c = 0;
  32.                     $out = "";
  33.                     set_socket_blocking ( $fp, false );
  34.                     do
  35.                     {
  36.                         $out = fgets ( $fp, 2500 );
  37.                         if ( ereg ( "^220", $out ) )
  38.                         {
  39.                             $s = 0;
  40.                             $out = "";
  41.                             $c++;
  42.                         }
  43.                         else if ( ( $c > 0 ) && ( $out == "" ) )
  44.                         { break; }
  45.                         else
  46.                         { $s++;    }
  47.                         if ( $s == 9999 ) { break; }
  48.                     
  49.                     } while ( $out == "" );
  50.                     set_socket_blocking ( $fp, true );
  51.  
  52.                     fputs ( $fp, "HELO $SERVER_NAME\n" );
  53.                     $output = fgets ( $fp, 2000 );
  54.                     fputs ( $fp, "MAIL FROM: <info@" . $tld . ">\n" );
  55.                     $output = fgets ( $fp, 2000 );
  56.                     fputs ( $fp, "RCPT TO: <$email>\n" );                
  57.                     $output = fgets ( $fp, 2000 );
  58.                     if ( ereg ( "^250", $output ) )
  59.                     {
  60.                         $return[0] = true;
  61.                     }
  62.                     else
  63.                     {
  64.                         $return[0] = false;
  65.                         $return[1] = $output;
  66.                     }
  67.                     fputs ( $fp, "QUIT\n" );
  68.                     fclose( $fp );
  69.  
  70.                     if ( $return[0] == true )
  71.                     { break; }
  72.                 }
  73.             }
  74.         }
  75.     }
  76.     return $return;
  77. }
  78. ?>
  79.