标签:fputs blank 主机 设置 reg 域名 put 前言 foreach
1 <?php 2 function send_mail($name, $to, $subject = ‘No subject‘, $body) { 3 $loc_host = "ooo"; //主机名,随意 4 $smtp_acc = "service@XXX.com"; //你的邮箱 5 $smtp_pass="XXXXX"; //邮箱登陆密码,不是授权码,163个人邮箱才是授权码。 6 $smtp_host="smtp.exmail.qq.com"; //腾讯企业邮箱地址 7 $from="service@xxx.com"; //你的邮箱 8 $headers = "Content-Type: text/html; charset=\"utf-8\"\r\nContent-Transfer-Encoding: base64"; 9 $lb="\r\n"; //linebreak 10 $hdr = explode($lb,$headers); 11 if($body) { 12 $bdy = preg_replace("/^\./","..",explode($lb,$body)); 13 } 14 15 $smtp = array( 16 17 array("EHLO ".$loc_host.$lb,"220,250","HELO error: "), 18 19 array("AUTH LOGIN".$lb,"334","AUTH error:"), 20 21 array(base64_encode($smtp_acc).$lb,"334","AUTHENTIFICATION error : "), 22 23 array(base64_encode($smtp_pass).$lb,"235","AUTHENTIFICATION error : ") 24 ); 25 26 $smtp[] = array("MAIL FROM: <".$from.">".$lb,"250","MAIL FROM error: "); 27 28 $smtp[] = array("RCPT TO: <".$to.">".$lb,"250","RCPT TO error: "); 29 30 $smtp[] = array("DATA".$lb,"354","DATA error: "); 31 32 $smtp[] = array("From: ".$name."<".$from.">".$lb,"",""); 33 34 $smtp[] = array("To: ".$to.$lb,"",""); 35 36 $smtp[] = array("Subject: ".$subject.$lb,"",""); 37 38 foreach($hdr as $h) {$smtp[] = array($h.$lb,"","");} 39 40 $smtp[] = array($lb,"",""); 41 42 if($bdy) {foreach($bdy as $b) {$smtp[] = array(base64_encode($b.$lb).$lb,"","");}} 43 44 $smtp[] = array(".".$lb,"250","DATA(end)error: "); 45 46 $smtp[] = array("QUIT".$lb,"221","QUIT error: "); 47 48 49 $fp = @fsockopen($smtp_host, 25); 50 if (!$fp) echo "Error: Cannot conect to ".$smtp_host.""; 51 while($result = @fgets($fp, 1024)){ 52 if(substr($result,3,1) == " ") { break; } 53 } 54 55 $result_str=""; 56 57 foreach($smtp as $req){ 58 59 @fputs($fp, $req[0]); 60 61 if($req[1]){ 62 63 while($result = @fgets($fp, 1024)){ 64 if(substr($result,3,1) == " ") { break; } 65 }; 66 if (!strstr($req[1],substr($result,0,3))){ 67 $result_str.=$req[2].$result.""; 68 } 69 } 70 } 71 72 @fclose($fp); 73 return 1; 74 } 75 76 77 ?>
1 <?php 2 header("Content-Type: text/html; charset=utf-8"); 3 include_once("mail.php"); 4 send_mail("发件人", "收件邮箱","邮件标题"],"内容"); 5 echo "<h1>邮件已发送</h1>"; 6 ?>
夹缝中求生存-在一无所有的php虚拟主机环境下利用smtp发送邮件(二)
标签:fputs blank 主机 设置 reg 域名 put 前言 foreach
原文地址:http://www.cnblogs.com/scrazy/p/7643659.html