<?php
//echo "发送邮件";
send_mail('
[email protected]','发信标题','aaaaa');
function send_mail($to, $subject = '未标题', $body)
{
$loc_host = "mail.qq.com"; //发信主机名
$smtp_acc = "
[email protected]"; //SMTP认证的用户名
$smtp_pass = "aaaaaaa";//SMTP认证的密码
$smtp_host = "smtp.qq.com"; //SMTP服务器地址
$from = "
[email protected]"; //发信人E-mail地址
$headers = "Content-Type: text/plain; charset=\"gb2312\"\r\nContent-Transfer-Encoding:base64"; //编码
$lb = "\r\n"; //换行符
echo "$body";
$hdr = explode($lb, $headers); //解析后的Header
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:"),
);
echo "2";
//5.发送MAIL FROM,期待返回250
$smtp[] = array("MAIL FROM: <".$from.">".$lb, "250", "MAIL FROM error:"); echo "3";
//6.发送RCPT TO,期待返回250
$smtp[] = array("RCPT TO: <".$to.">".$lb, "250", "RCPT TO error: "); echo "4";
//7.发送DATA, 期待返回354
$smtp[] = array("DATA".$lb, "354" , "DATA error: ");echo "5";
//8.0发送FROM
$smtp[] = array("From: ".$from.$lb, "", "");echo "6";
//8.2发送To
$smtp[] = array("To: ".$to.$lb, "", "");echo "7";
//8.1发送标题
$smtp[] = array("Subject: ".$subject.$lb, "");echo "8";
//8.3发送其他Header内容
foreach($hdr as $h){ $smtp[] = array($h.$lb, "", "");}echo "9";
//8.4发送一个空行, 结束Header发送
$smtp[] = array($lb, "", "");echo "10.";
//8.5发送信件的主题
if($bdy)
{
foreach($bdy as $b)
{
$smtp[] = array(base64_encode($b.$lb).$lb,"","");
}
}echo "11.";
//9.发送“.”表示信件结束,期待返回250
$smtp[] = array(".".$lb, "250", "DATA(end) error: ");echo "12.";
//10.发送QUIT,退出,期待返回221
$smtp[] = array("QUIT".$lb, "221", "QUIT error: ");echo "13.";
//打开SMTP服务器端口
$fp = @fsockopen($smtp_host, 25);echo "14.".$fp;
if(!$fp) echo "<b>Error:</b> Cannot connect to ".$smtp_host."<br />";echo "15.";
while($result = @fgets($fp, 1024))
{
if(substr($result, 3, 1) == " "){ break; }
}echo "16.";
$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; }echo "17.";
}echo "18.";
if(!strstr($req[1], substr($result, 0, 3)))
{
$result_str .= $req[2].$result."<br />";
}
}
}echo "19.";
@fclose($fp);
return $result_str;
}
?>
帮忙看一下这个程序有错误吗?如何该成能发送HTML格式的邮件?
谢谢!!!