<?php
/*
*版本:1.4.2
*作者:鱼尾唯一
*主页:www.fishwei.com
*QQ群:33918040 有付出才会有收获!
*联系:[email protected] (MSN/E-mail) 14778648(QQ)
*描述:利用socket套接字+SMTP指令编写的mail()函数,由本群 古董 同志指导 o(∩_∩)o...哈哈
*改进:1.1->使发送的邮件支持HTML,但是貌似不支持CSS!通过设定$htmlsupport=0,取消对HTML支持,默认为支持!
1.2->a.添加了 $mailfrom 参数,原因-仅仅靠smtp地址判断是不准确的!
b.发信时间修改成了脚本执行时的服务器时间
c.添加为HTML格式的新建提供了编码设置
1.3->修改了错误提示,格式为错误步骤+错误代码+错误描述!
1.4->支持邮件群发,多个邮件用分号隔开,根据 No.1 的意见,修改了部分冗余代码!
*/
function mail_error_report($content,$errno,$error,$adv=""){
$adv = $adv?("<b>提 示:</b>".$adv):$adv;
return "<b>出 错 啦!</b><br />
<b>错误步骤:</b>".$content."<br />
<b>错误代码:</b>".$errno."<br />
<b>错误描述:</b>".$error."<br />".$adv;
}
function socket_mail($smtphost,$name,$psw,$mailfrom,$mailtos,$mail_subject,$mail_content,$htmlsupport=1,$charset="GBK",$smtpport=25){
$fp = @fsockopen($smtphost, $smtpport, $errno, $error, 10);
if (!$fp){
echo mail_error_report("打开到".$smtphost."SMTP服务器的连接",$errno,$error,"请核查SMTP服务器名,网络连接情况,以及是否有防火墙阻止!");
return false;
}else{
$content = fgets($fp, 200);
fwrite($fp, "helo $name\n");
$content = fgets($fp,200);
$errno = intval(substr($content,0,3));
$error = substr($content,4,200);
if($errno==250){
fwrite($fp, "auth login\n");
$content = fgets($fp,200);
$errno = intval(substr($content,0,3));
$error = substr($content,4,200);
}else{
return mail_error_report("用户有效性验证",$errno,$error);
}
if($errno==334){
fwrite($fp, (base64_encode($name))."\n");
$content = fgets($fp,200);
$errno = intval(substr($content,0,3));
$error = substr($content,4,200);
}else{
return mail_error_report("登录命令",$errno,$error);
}
if($errno==334){
fwrite($fp, (base64_encode($psw))."\n");
$content = fgets($fp,200);
$errno = intval(substr($content,0,3));
$error = substr($content,4,200);
}else{
return mail_error_report("用户名验证",$errno,$error);
}
if($errno==235){
fwrite($fp, "mail from: <$mailfrom>\n");
$content = fgets($fp,200);
$errno = intval(substr($content,0,3));
$error = substr($content,4,200);
}else{
return mail_error_report("用户密码验证",$errno,$error);
}
if($errno==250){
$mailtos = explode(";",$mailtos);
foreach($mailtos as $mailto){
fwrite($fp, "rcpt to: <$mailto>\n");
$content = fgets($fp,200);
$errno = intval(substr($content,0,3));
//只要有一个通过,就可以发信了,下面用的是$mailto_errno作为$errno验证;
if($errno==250){
$mailto_errno = $errno;
$mailto_error .= "发送到 <b>".$mailto."</b> 的信件成功!<br />\n";
}else{
$mailto_error .= "发送到 <b>".$mailto."</b> 的信件地址存在问题!<br />\n";
}
$error = substr($content,4,200);
}
}else{
return mail_error_report("发件地址认证",$errno,$error);
}
if($mailto_errno==250){
fwrite($fp, "data\n");
$content = fgets($fp,200);
$errno = intval(substr($content,0,3));
$error = substr($content,4,200);
}else{
return mail_error_report("收件地址",$errno,$error);
}
if($errno==354){
$time = date('D, j M Y G:i:s');
$header = "From: $mailfrom\n";
$header.= "To: $mailto\n";
$header.= "Date: $time +0800\n";
$header.= "Subject: $mail_subject\n";
if(!$htmlsupport){
$header.= "\n";
}else{
$header.= "Content-Type: text/html;charset:$charset;\n\n";
}
$mail = $header.$mail_content;
fwrite($fp, $mail."\n.\n");
}else{
return mail_error_report("邮件写入命令",$errno,$error);
}
fclose($fp);
return $mailto_error;
}
}
// sina邮箱 网易邮箱(126的SMTP不是注册就开通的!) QQ邮箱 SMTP通过测试!
// yahoo 非增值用户 ,不支持SMTP! 貌似hotmail也不支持!
$smtphost = "";
$name = "";
$psw = "";
//发件人地址务必与所登录的服务器保持域名一致,否则可能出错!
$mailfrom = "";
//多个邮件仅用分号(";")隔开,切勿使用其他字符,尤其是回车符!
$mailto = "";
$subject = "你好,这里是测试邮件!";
$content = '<b>这里是测试邮件!目前已经开始支持HTML</b><br />From : <a href="http://www.fishwei.com">http://www.fishwei.com</a>';
$rs = socket_mail($smtphost,$name,$psw,$mailfrom,$mailto,($subject.$i),$content);
echo $rs."<br />";
?>
|