Advertisement
Guest User

Email validation

a guest
Aug 30th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2.  
  3.     function validate_email ($email, $fromAddress = '[email protected]', $socketTimeout = 1) {
  4.  
  5.         // Validate email syntax
  6.         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  7.             return FALSE;
  8.         }
  9.  
  10.         // Extract domain portion of email address
  11.         $parts = explode('@', $email);
  12.         $domain = array_pop($parts);
  13.  
  14.         // Look up MX for domain
  15.         if (!getmxrr($domain, $mxHosts)) {
  16.             return FALSE;
  17.         }
  18.  
  19.         // Loop MX hosts until we find one we can connect to
  20.         foreach ($mxHosts as $host) {
  21.  
  22.             if (ip2long($host) !== FALSE) {
  23.                 // $host is already an IP - should never happen but just in case
  24.                 $ip = $host;
  25.             } else {
  26.                 // Lookup IP for $host
  27.                 $ip = gethostbyname($host);
  28.                 if ($ip === $host) {
  29.                     continue;
  30.                 }
  31.             }
  32.  
  33.             // Try and connect
  34.             if (!$sock = fsockopen($ip, 25, $errNo, $errStr, $socketTimeout)) {
  35.                 continue;
  36.             }
  37.  
  38.             // Set timeout for read ops
  39.             $secs = floor($socketTimeout);
  40.             $usecs = $socketTimeout - $secs;
  41.             stream_set_timeout($sock, $secs, $usecs);
  42.  
  43.         }
  44.  
  45.         // If we haven't made a connection we've failed! Must do better.
  46.         if (!$sock) {
  47.             return FALSE;
  48.         }
  49.  
  50.         // Have a little chat in SMTP. If any operation fails, consider the email address invalid
  51.         while ($response = fgets($sock)) { // Should be a 220 intro
  52.             if (substr($response, 0, 3) !== '220') {
  53.                 return FALSE;
  54.             }
  55.             if (trim($response[3]) === '') {
  56.                 break;
  57.             }
  58.         }
  59.         if (!fwrite($sock, "HELO just.visiting\n")) { // Send HELO, should get a 250
  60.             return FALSE;
  61.         }
  62.         while ($response = fgets($sock)) {
  63.             if (substr($response, 0, 3) !== '250') {
  64.                 return FALSE;
  65.             }
  66.             if (trim($response[3]) === '') {
  67.                 break;
  68.             }
  69.         }
  70.         if (!fwrite($sock, "MAIL FROM: <$fromAddress>\n")) { // MAIL FROM, should get a 250
  71.             return FALSE;
  72.         }
  73.         while ($response = fgets($sock)) {
  74.             if (substr($response, 0, 3) !== '250') {
  75.                 return FALSE;
  76.             }
  77.             if (trim($response[3]) === '') {
  78.                 break;
  79.             }
  80.         }
  81.         if (!fwrite($sock, "RCPT TO: <$email>\n")) { // RCPT TO, should get a 250 if the address exists
  82.             return FALSE;
  83.         }
  84.         while ($response = fgets($sock)) {
  85.             if (substr($response, 0, 3) !== '250') {
  86.                 return FALSE;
  87.             }
  88.             if (trim($response[3]) === '') {
  89.                 break;
  90.             }
  91.         }
  92.         fwrite($sock, "QUIT\n");
  93.         fclose($sock);
  94.  
  95.         // Success!
  96.         return TRUE;
  97.  
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement