1. <?php
  2. if(strtoupper(substr(PHP_OS, 0, 3) == 'WIN')){
  3.     // getmxrr() - fix for windows users
  4.     function getmxrr($hostname, &$mxhosts){
  5.         $mxhosts = array();
  6.         exec('nslookup -type=mx '.$hostname, $result_arr);
  7.         foreach($result_arr as $line){
  8.             if(preg_match("/.*mail exchanger = (.*)/", $line, $matches)) $mxhosts[] = $matches[1];
  9.         }
  10.         return (count($mxhosts) > 0);
  11.     }
  12.     // usleep() - fix for php5 windows users
  13.     if(version_compare(phpversion(), "5.0.0", ">=")){
  14.         function usleep($usecs){
  15.             $temp = gettimeofday();
  16.             $start = (int)$temp["usec"];
  17.             while(true){
  18.                 $temp = gettimeofday();
  19.                 $stop = (int)$temp["usec"];
  20.                 if(($stop-$start) >= $usecs) break;
  21.             }
  22.         }
  23.     }
  24. }
  25.  
  26. /** 
  27.  * Return an array with tow values: boolean and string
  28.  * 
  29.  * @author    Laurentiu Tanase <expertphp@yahoo.com> 
  30.  * @version   2.3 
  31.  * @param     string  $to         To e-mail address 
  32.  * @param     string  $subject    Mail Subject 
  33.  * @param     string  $message    Message ( Mixed ) 
  34.  * @param     string  $from       From e-mail address 
  35.  * @param     string  $header     Additional headers 
  36.  * @param     integer $timeout    Time out connection 
  37.  */
  38.  
  39. function smtp_mail($to, $subject, $message, $from, $header = false, $timeout = 30){
  40.  
  41.     $exp_to = explode("@", $to);
  42.     getmxrr($exp_to[1], $mxhost);
  43.     $iparr = array();
  44.  
  45.     foreach($mxhost as $hostname){
  46.         $iphost = gethostbyname($hostname);
  47.         if($hostname != $iphost && $hostname != $exp_to[1]) $iparr[] = $iphost;
  48.     }
  49.  
  50.     if(count($iparr) > 0){
  51.  
  52.         $ret = array(false, "Can not contact MX host !");
  53.         foreach($iparr as $ipaddr){
  54.  
  55.             if($connect = @fsockopen($ipaddr, 25, $err_num, $err_msg, $timeout)){
  56.  
  57.                 $set = true;
  58.  
  59.                 $rcv0 = fgets($connect, 1024);
  60.                 if(substr($rcv0, 0, 3) != "220"){
  61.                     fclose($connect);
  62.                     $ret = array(false, "Response 0 error: ".$rcv0);
  63.                     $set = false;
  64.                 }
  65.                 
  66.                 if($set){
  67.                     $exp_from = explode("@", $from);
  68.                     fputs($connect, "HELO ".$exp_from[1]."\r\n");
  69.                     $rcv1 = fgets($connect, 1024);
  70.                     if(substr($rcv1, 0, 3) != "250"){
  71.                         fclose($connect);
  72.                         $ret = array(false, "Response 1 error: ".$rcv1);
  73.                         $set = false;
  74.                     }
  75.                 }
  76.                 
  77.                 if($set){
  78.                     fputs($connect, "MAIL FROM:<".$from.">\r\n");
  79.                     $rcv2 = fgets($connect, 1024);
  80.                     if(substr($rcv2, 0, 3) != "250"){
  81.                         fclose($connect);
  82.                         $ret = array(false, "Response 2 error: ".$rcv2);
  83.                         $set = false;
  84.                     }
  85.                 }
  86.  
  87.                 if($set){
  88.                     fputs($connect, "RCPT TO:<".$to.">\r\n");
  89.                     $rcv3 = fgets($connect, 1024);
  90.                     if(substr($rcv3, 0, 3) != "250"){
  91.                         fclose($connect);
  92.                         $ret = array(false, "Response 3 error: ".$rcv3);
  93.                         $set = false;
  94.                     }
  95.                 }
  96.                 
  97.                 if($set){
  98.                     fputs($connect, "DATA\r\n");
  99.                     $rcv4 = fgets($connect, 1024);
  100.                     if(substr($rcv4, 0, 3) != "354"){
  101.                         fclose($connect);
  102.                         $ret = array(false, "Response 4 error: ".$rcv4);
  103.                         $set = false;
  104.                     }
  105.                 }
  106.                 
  107.                 if($set){
  108.                     if(!$header){
  109.                         $header = "From: \"".$exp_from[0]."\" <".$from.">\r\n".
  110.                             "To: \"".$exp_to[0]."\" <".$to.">\r\n".
  111.                             "Date: ".date("r")."\r\n".
  112.                             "Subject: ".$subject."\r\n";
  113.                     }
  114.  
  115.                     $rep = array(".\r\n", ".\n", ".\r");
  116.                     fputs($connect, $header."\r\n".str_replace($rep, ". \r\n", $message)." \r\n");
  117.                     fputs($connect, ".\r\n");
  118.                     $rcv5 = fgets($connect, 1024);
  119.                     if(substr($rcv5, 0, 3) != "250"){
  120.                         fclose($connect);
  121.                         $ret = array(false, "Response 5 error: ".$rcv5);
  122.                         $set = false;
  123.                     }
  124.                     
  125.                     fputs($connect, "QUIT\r\n");
  126.                     usleep(1);
  127.                     $rcv6 = fgets($connect, 1024);
  128.                     usleep(1);
  129.                     fclose($connect);
  130.                 }
  131.                 
  132.                 if($set){
  133.                     $ret = array(true, "Response 6 success: ".$rcv5." | ".$rcv6);
  134.                     break;
  135.                 }
  136.  
  137.             }
  138.         }
  139.  
  140.         return $ret;
  141.  
  142.     }else return array(false, "Can not find MX zone !");
  143.  
  144. } // End smtp_mail() -----------------------------
  145. ?>