smtp送信のphp関数を収集し,テストした.

3211 ワード

echo send_mail('[email protected]','    ','    '); 

function send_mail($to, $subject = 'No subject', $body) { 
$loc_host = "#"; //      ,    
$smtp_acc = "#"; //Smtp      , 
$smtp_pass="password"; //Smtp     ,    pop3   
$smtp_host="#"; //SMTP     ,   smtp.tom.com 
$from="#"; //   Email  ,         
$headers = "Content-Type: text/plain; charset=\"gb2312\"\r
Content-Transfer-Encoding: base64"; $lb="\r
"; //linebreak $hdr = explode($lb,$headers); // hdr if($body) {$bdy = preg_replace("/^\./","..",explode($lb,$body));}// Body $smtp = array( //1、EHLO, 220 250 array("EHLO ".$loc_host.$lb,"220,250","HELO error: "), //2、 Auth Login, 334 array("AUTH LOGIN".$lb,"334","AUTH error:"), //3、 Base64 , 334 array(base64_encode($smtp_acc).$lb,"334","AUTHENTIFICATION error : "), //4、 Base64 , 235 array(base64_encode($smtp_pass).$lb,"235","AUTHENTIFICATION error : ")); //5、 Mail From, 250 $smtp[] = array("MAIL FROM: <".$from.">".$lb,"250","MAIL FROM error: "); //6、 Rcpt To。 250 $smtp[] = array("RCPT TO: <".$to.">".$lb,"250","RCPT TO error: "); //7、 DATA, 354 $smtp[] = array("DATA".$lb,"354","DATA error: "); //8.0、 From $smtp[] = array("From: ".$from.$lb,"",""); //8.2、 To $smtp[] = array("To: ".$to.$lb,"",""); //8.1、 $smtp[] = array("Subject: ".$subject.$lb,"",""); //8.3、 Header foreach($hdr as $h) {$smtp[] = array($h.$lb,"","");} //8.4、 , Header $smtp[] = array($lb,"",""); //8.5、 if($bdy) {foreach($bdy as $b) {$smtp[] = array(base64_encode($b.$lb).$lb,"","");}} //9、 “.” , 250 $smtp[] = array(".".$lb,"250","DATA(end)error: "); //10、 Quit, , 221 $smtp[] = array("QUIT".$lb,"221","QUIT error: "); // smtp $fp = @fsockopen($smtp_host, 25); if (!$fp) echo "<b>Error:</b> Cannot conect to ".$smtp_host."<br>"; while($result = @fgets($fp, 1024)){if(substr($result,3,1) == " ") { break; }} $result_str=""; // smtp / foreach($smtp as $req){ // @fputs($fp, $req[0]); // , if($req[1]){ // while($result = @fgets($fp, 1024)){ if(substr($result,3,1) == " ") { break; } }; if (!strstr($req[1],substr($result,0,3))){ $result_str.=$req[2].$result."<br>"; } } } // @fclose($fp); return $result_str; } ?>