Advertisement
Guest User

Validate Mail

a guest
Jan 29th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.20 KB | None | 0 0
  1. function validate_mail($email){
  2.     $mailparts=explode("@",$email);
  3.     $hostname = $mailparts[1];
  4.     // validate email address syntax
  5.     $exp = "/^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$/i";
  6.     $b_valid_syntax=preg_match($exp, $email);
  7.     // get mx addresses by getmxrr
  8.     $b_mx_avail=getmxrr( $hostname, $mx_records, $mx_weight );
  9.     $b_server_found=0;
  10.     if($b_valid_syntax && $b_mx_avail)
  11.     {
  12.         // copy mx records and weight into array $mxs
  13.         $mxs=array();
  14.         for($i=0;$i<count($mx_records);$i++)
  15.         {
  16.             $mxs[$mx_weight[$i]]=$mx_records[$i];
  17.         }
  18.         // sort array mxs to get servers with highest prio
  19.         ksort ($mxs, SORT_NUMERIC );
  20.         reset ($mxs);
  21.         while (list ($mx_weight, $mx_host) = each ($mxs) )
  22.         {
  23.             if($b_server_found == 0)
  24.             {
  25.                 $code = CheckMX($mx_host, $email);
  26.                 if ($code == 451)
  27.                     $code = CheckMX($mx_host, $eMail);
  28.                 if ($code == 250)
  29.                     $b_server_found=1;
  30.             }
  31.         }
  32.     }
  33.     return $b_server_found;
  34. }
  35. function CheckMX($mx_host, $eMail)
  36. {
  37.     $code = 0;
  38.     $fp = @fsockopen($mx_host, 25, $errno, $errstr, 2);
  39.     if ($fp)    {
  40.         senda_command($fp, 'HELO wiset.it');
  41.         senda_command($fp, 'MAIL FROM:<noreply@wiset.it>');
  42.         $erg = senda_command($fp, 'RCPT TO:<'.$eMail.'>');
  43.         fclose($fp);
  44.         $code = intval(substr($erg, 0, 3));
  45.     }
  46.     return $code;
  47. }
  48. function senda_command($fp, $out)
  49. {
  50.     fwrite($fp, $out . "\r\n");
  51.     return geta_data($fp);
  52. }
  53.  
  54. function geta_data($fp)
  55. {
  56.     $s="";
  57.     stream_set_timeout($fp, 2);
  58.     for($i=0;$i<2;$i++)
  59.         $s.=fgets($fp, 1024);
  60.     return $s;
  61. }
  62. // support windows platforms
  63. if (!function_exists ('getmxrr') ) {
  64.   function getmxrr($hostname, &$mxhosts, &$mxweight) {
  65.     if (!is_array ($mxhosts) ) {
  66.       $mxhosts = array ();
  67.     }
  68.     if (!empty ($hostname) ) {
  69.       $output = "";
  70.       @exec ("nslookup.exe -type=MX $hostname.", $output);
  71.       $imx=-1;
  72.  
  73.       foreach ($output as $line) {
  74.         $imx++;
  75.         $parts = "";
  76.         if (preg_match ("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts) ) {
  77.           $mxweight[$imx] = $parts[1];
  78.           $mxhosts[$imx] = $parts[2];
  79.         }
  80.       }
  81.       return ($imx!=-1);
  82.     }
  83.     return false;
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement