版主所见所用过的最好的用PHP写的SMTP类,支持附件(多个),支持HTML。快来用吧!(作者:boss

smtp

<?
/***********************************
PHP MIME SMTP ver 1.0 Powered by Boss_ch, Unigenius soft ware co. Ltd
All rights reserved, Copyright 2000 ;
本类用 PHP 通过 smtp  sock 操作发送 MIME 类型的邮件,可以发送
HTML 格式的正文、附件,采用 base64 编码
本版本是针对个人的发送,与多人群发版本不同的是,每发送到一个人,就重新进行一次编码,在接收端的用户看来,只是发送给他一个人的。
针对多人群发的情况,只发送一次,通过多个 RCPT TO 命令发送到不同的人信箱中,
说明:
请把 $hostname 设为你有权限的 默认 smtp 服务器或是在 new 时指定
把 $charset 改成你的默认 字符集
Html 正文中如有图片,请用绝对路径的引用 "image.gif";
  并连上网,以保证程序能读取到图片的数据信息
如果是通过表单提交过来的 Html 正文,请先用 StripSlashes($html_body) 把正文内容进行预处理
  Html 中用到的样式表文件,请不要用 <link >之类 的引用,直接把样式表定义放在
<style></style>标签中

转载请保留此版权信息, Bugs Report : boss_ch@china.com
*************************************/
if(!isset($__smtp_class__)){
$__smtp_class__=1;

class smtp
{
var $hostname="";
var $port=25;
var $connection=0;
var $debug=1;

var $timeout=30;
var $err_str;
var $err_no;

var $autocode=true;
var $charset="GB2312";
var $subject="";
var $body="";
var $attach="";
var $temp_text_body;
var $temp_html_body;
var $temp_body_images;

var $bound_begin="=====powered_by_boss_chen_";
var $bound_end="_046484063883_=====";

Function smtp($server="smtp.china.com"",$port=25,$time_out=20)
{$this->hostname=$server;
$this->port=$port;
$this->timeout=$time_out;
return true;
}

Function outdebug($message)
{
echo htmlspecialchars($message)."<br>\n";
}

function command($command,$return_lenth=1,$return_code='2')
{
if ($this->connection==0)
{
$this->err_str="没有连接到任何服务器,请检查网络连接";
return false;
}
if ($this->debug)
$this->outdebug(">>> $command");
if (!fputs($this->connection,"$command \r\n"))
{
$this->err_str="无法发送命令".$command;
return false;
}
else
{
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug("$resp");
if (substr($resp,0,$return_lenth)!=$return_code)
{
$this->err_str=$command." 命令服务器返回无效:".$resp;
return false;
}
else
return true;
}
}

Function open()
{
if($this->hostname=="")
{$this->err_str="无效的主机名!!";
return false;
}

if ($this->debug) echo "$this->hostname,$this->port,&$err_no, &$err_str, $this->timeout<BR>";
if (!$this->connection=fsockopen($this->hostname,$this->port,&$err_no, &$err_str, $this->timeout))
{
$this->err_str="连接到 SMTP 服务器失败,错误信息:".$err_str."错误号:".$err_no;
return false;
}
else
{
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug("$resp");
if (substr($resp,0,1)!="2")
{$this->err_str="服务器返回无效的信息:".$resp."请检查SMTP服务器是否正确";
return false;
}
return true;
}
}

Function Close()
{
if($this->connection!=0)
{
fclose($this->connection);
$this->connection=0;
}
}

Function Buildhead($from_name,$to_name,$from_mail,$to_mail,$subject)
{
if (empty($from_name))
$from_name=$from_mail;
if (empty($to_name)) $to_name=$to_mail;
$this->subject="From: =?$this->charset?B?".base64_encode($from_name)."?=<$from_mail>\r\n";
$this->subject.="To: =?$this->charset?B?".base64_encode($to_name)."?=<$to_mail>\r\n";
$subject=ereg_replace("\n","",$subject);
$this->subject.="Subject: =?$this->charset?B?".base64_encode($subject)."?=\r\n";
if ($this->debug) echo nl2br(htmlspecialchars($this->subject));
return true;
}

Function parse_html_body($html_body=null)
{
$passed="";
$image_count=0;
$this->temp_body_images=array();
while (eregi("\<*img([^\>]+)src[[:space:]]*=[[:space:]]*([^ ]+)",$html_body,$reg))
{

$pos=@strpos($html_body,$reg[0]);
$passed.=substr($html_body,0,$pos);
$html_body=substr($html_body,$pos+strlen($reg[0]));
$image_tag=$reg[2];
$image_att=$reg[1];
$tag_len=strlen($image_tag);
if ($image_tag[0]=="'" or $image_tag[0]=='"')
$image_tag=substr($image_tag,1);
if (substr($image_tag,strlen($imgage_tag)-1,1)=="'" or substr($image_tag,strlen($imgage_tag)-1,1)=='"')
$image_tag=substr($image_tag,0,strlen($imgage_tag)-1);
//echo $image_tag."<br>";
$cid=md5(uniqid(rand()));
$cid=substr($cid,0,15)."@unigenius.com";
$passed.="<img ".$image_att."src=\"cid:".$cid."\"";
$end_pos=@strpos($html_body,'>');
$passed.=substr($html_body,0,$end_pos);
$html_body=substr($html_body,$end_pos);
// 把图片数据读出来保存到一个数据;

$img_file_con=fopen($image_tag,"r");
unset($image_data);
while ($tem_buffer=AddSlashes(fread($img_file_con,16777216)))
$image_data.=$tem_buffer;
fclose($img_file_con);
$image_exe_name=substr($image_tag,strrpos($image_tag,'.')+1,3);
switch (strtolower($image_exe_name))
{
case "jpg":
case "jpeg":
$content_type="image/jpeg";
break;
case "gif":
$content_type="image/gif";
break;
case "png":
$content_type="image/x-png";
break;
case "tif":
$content_type="image/tif";
break;
default:
$content_type="image/";
break;
}

$this->temp_body_images[$image_count][name]=basename($image_tag);
$this->temp_body_images[$image_count][type]=$content_type;
$this->temp_body_images[$image_count][cid]=$cid;
$this->temp_body_images[$image_count][data]=$image_data;
$image_count++;
}
$this->temp_html_body=$passed.$html_body;
return true;

}

function build_content($bound_level=0,$text_body,$html_body,$hava_att=false)
{
if ($html_body)
{
if (eregi("\<*img[[:space:]]+src[[:space:]]*=[[:space:]]*([^ ]+)",$html_body,$reg))
{
$bound_level++;
if ($text_body)
{
$this->body.="Content-Type: multipart/related;
type=\"multipart/alternative\";
boundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
}
else
{
$this->body.="Content-Type: multipart/related;
boundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";

} // 对于是否 text 正文 、 html正文 有没有,须有不同的 MIME 头
if (!$hava_att) $this->body.="This is a multi-part message in MIME format.\r\n\r\n";
// 正文标识,如果是已经有附件的编码,则在正文 中不需要这一句
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->parse_html_body($html_body);
if ($text_body)
{
$this->body.="Content-Type: multipart/alternative;
boundary=\"";
$bound_level++;
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/plain;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/html;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else
{
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/html;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
} //正文编码,有或没有 text 部分,编成不同的格式。
for ($i=0;$i<count($this->temp_body_images);$i++)
{
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type:".$this->temp_body_images[$i][type].";
name=\"";
$this->body.=$this->temp_body_images[$i][name]."\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="Content-ID: <".$this->temp_body_images[$i][cid].">\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_body_images[$i][data])))."\r\n";
}
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else // 有或没有图片,以上是有图片的处理,下面是没有图片的处理
{
$this->temp_html_body=$html_body;
if ($text_body)
{
$bound_level++;
$this->body.="Content-Type: multipart/alternative;
boundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";

if (!$hava_att) $this->body.="\r\nThis is a multi-part message in MIME format.\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/plain;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/html;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else
{
$this->body.="Content-Type: text/html;\r\n";
$this->body.="charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
} //正文编码,有或没有 text 部分,编成不同的格式。

} // end else
}
else // 如果没有 html 正文,只有 text 正文 
{
$this->body.="Content-Type: text/plain;
charset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64 \r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
}
} // end function default

Function Buildbody($text_body=null,$html_body=null,$att=null)
{
$this->body="MIME-Version: 1.0\r\n";
if (null==$att or (@count($att)==0)) //如果没有附件,查看正文的类型 ;
{
$encode_level=0;
$this->build_content($encode_level,$text_body,$html_body);
} // 如果没有附件,
// ********************************************************
else //如果有附件,
{
$bound_level=0;
$this->body.="Content-Type: multipart/mixed;
boundary=\"";
$bound_level++;

$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
$this->body.="This is a multi-part message in MIME format.\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->build_content($bound_level,$text_body,$html_body,true); // 编入正文部分

$num=count($att);
for ($i=0;$i<$num;$i++)
{
$file_name=$att[$i][name];
$file_source=$att[$i][source];
$file_type=$att[$i][type];
$file_size=$att[$i][size];

if (file_exists($file_source))
{
$file_data=addslashes(fread($fp=fopen($file_source,"r"), filesize($file_source)));
$file_data=chunk_split(base64_encode(StripSlashes($file_data)));
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: $file_type;\r\n name=\"$file_name\"\r\nContent-Transfer-Encoding: base64\r\n";
$this->body.="Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";
$this->body.=$file_data."\r\n";
}
} //end for

$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
} // end else

if ($this->debug) echo nl2br(htmlspecialchars($this->body));

return true;
}

function send($from_name,$to_name,$from_mail,$to_mail,$subject,$text_body=false,$html_body=false,$att=false)
{

if (empty($from_mail) or empty($to_mail))
{
$this->err_str="没有指定正确的邮件地址:发送人:".$from_mail."接收人:".$to_mail;
return false;
}

if (gettype($to_mail)!="array")
$to_mail=split(",",$to_mail); //如果不是数组,转换成数组,哪怕只有一个发送对象;
if (gettype($to_name)!="array")
$to_name=split(",",$to_name); //如果不是数组,转换成数组,哪怕只有一个发送对象;

$this->Buildbody($text_body,$html_body,$att);
// 所有信件的内容是一样的,可以只编一次,而对于不同的收信人,需要不同的 Head

if (!$this->open()) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;
// 与服务器建立链接
if (!$this->open()) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;

for ($i=0;$i<count($to_mail);$i++)
{
$this->Buildhead($from_name,$to_name[$i],$from_mail,$to_mail[$i],$subject);
if (!$this->command("RSET",3,"250")) return false;
if (!$this->command("MAIL FROM:".$from_mail,3,"250")) return false;
if (!$this->command("RCPT TO:".$to_mail[$i],3,"250")) return false;
if (!$this->command("DATA",3,"354")) return false;
// 准备发送邮件
if ($this->debug) $this->outdebug("sending subject;");
if (!fputs($this->connection,$this->subject)) { $this->err_str="发送邮件头时出错!"; return false;}
if ($this->debug) $this->outdebug("sending body;");
if (!fputs($this->connection,$this->body)) { $this->err_str="发送正文时出错!"; return false;}
if (!fputs($this->connection,".\r\n")) { $this->err_str="发送正文时出错!"; return false;} //正文发送完毕,退出;
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug("$resp");
if (substr($resp,0,1)!="2")
{
$this->err_str="发送完后,服务器没有响应!!";
return false;
}
// 发送邮件
}
if (!$this->command("QUIT",3,"221")) return false;
$this->close();
return true;
}

}//end class define
}//end if(!isset($__smtp_class__))
?>

时间: 2024-08-04 01:24:00

版主所见所用过的最好的用PHP写的SMTP类,支持附件(多个),支持HTML。快来用吧!(作者:boss的相关文章

蛮横的版主以及他们如何影响你

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 我还是在上大学的时候接触到的电脑,那时候很喜欢去聊天室,一聊就是一个通宵,真不知道那个时候怎么会这么喜欢聊天,现在看来,那时候真是在糟蹋我的美好时光,后来慢慢的就发现,聊天室越来越少的人在聊天,或者说是私聊,要不就是一大堆的广告,慢慢的也就不再聊天室混了.后来,开始玩起了论坛,其实我根本就不会玩,就是喜欢到处去看看帖子,觉得这个东西满有意思的

首页四格,首页五格For6.0(GBK)(UTF-8)[12种组合][9-18][版主安装测试通过]_php实例

下载万次的首页四格,首页五格For6.0(GBK)(UTF-8)[12种组合][9-18][版主安装测试通过] 引用: 本插件由版主sakurakawaii于07年9月8日15:30分 在Windows XP Discuz!6.0.0标准模版 IE6 Mysql4.1下测试安装无错 本测试仅代表此插件安装无错,不包括今后长期使用中可能出现的问题引用: 声明:本程序引用了部分5.0四格的代码,若是源码作者有意见请短信我,一定删除发布! 经过大量修改和flash设置增加了好多自定义设置,此插件可以说

关于SEOWHY版主王莹莹抄袭事件的联想

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 前一晚闹得沸沸扬扬的抄袭事件似乎平淡了这件事本质来讲并不是多轰动的事件,但是由于事件有一定的代表性,所以很多SEO人都开始关注和讨论过程是关于 SEOWHY一个版主名叫王莹莹的女生,88年生年轻女子抄袭一个SEO工作者TIAMO的SEO研究文章,是关于医疗网站的排名研究的. 首先二人在职业上有一定的共性,王莹莹自称是石家庄红十字医院的文案,

论坛版主拟实行实名制追踪引网友热议

本报讯(记者/夏小荔)佛山市公安局网监部门拟推行网站论坛版主实名制的消息一经报道(详情见本报6月2日<佛山观察>C04版),引起了众多网友及社会各界的热议.面对即将出台的新规,许多网友趋向于实名制下"深思熟虑,谨慎发言". 据了解,在南海区一社区论坛,近期有调查"实名制下,你愿意实名发帖吗?"其中,逾50%的网友选择需要"深思熟虑,谨慎发言",约25%的网友则表示"风险很大,宁可不说". 对于即将推出"版

论坛版主及管理员培育发展心得

一个好坛子不是一两个人能支撑起来的.我做过很多论坛的管理员,把所有精力放在对外推广和自己制造马甲疯狂转贴上面.转贴一页又一页,还是没有人气,其实这是用过去草根站长建站的思维来做论坛.我们评价一个论坛的好坏标准,首先还是是否有足够的互动性,创进棋觉得好的管理员当治大不致细.管人不管事.调动别人而不让自己在台前疲于奔命.管理员的真正身份应该做好幕后推手,管理论坛核心就是管理和发展斑竹,服务好斑竹板斧.当斑竹和板斧把论坛当家了的话,论坛在一定时间内想不上去都不可能. 那么什么样的人具备好版主的潜质?首

瞧,那些校园论坛版主的网络生活

从本科生读到博士生,杨春雷曾经获得过一串儿的奖励.前不久,他又拿到了一份奖学金--专门奖给校园论坛优秀版主的社会捐助奖学金--这他从未想到过. 6月16日晚7点,北京理工大学的一个小礼堂,神州正方奖学金现场答辩正式开始.19名校内BBS版主每人进行5分钟的演讲,并回答评委提问.当howardcc.yoyoflower.八爪鱼.故垒西边等人走上讲台时,他们受到了明星般的礼遇,热烈的掌声.欢呼声响彻礼堂. 台下的观众是他们的"粉丝",但很多人彼此并不认识,然而,这些选手的ID在校内的&qu

子窗体刷新父窗体的问题完全解决了,谢谢E版主

实际上我是对showdialog不熟悉所致.光想到传送数据了,就重新了showdialog事件,这样虽然传送了数据,但是却不能使用form.owner方法了.现在的解决方法是:在主窗体中用showdialog(me)调用子窗体,在子窗体中使用:dim f as new form() 'form是主窗体f = me.owner这时就可以调用主窗体的各个控件了,获取数据也很容易了.再次感谢E版主的大力帮助. 

迅雷快传版主权限有多大价值

  根据业内网盘的收费标准:迅雷快传每年每100GB,费用300块来计算,无限使用的版主权限+无限扩容的存储空间=多少价值?按最保守的计算,该价值至少在万元以上.

迅雷快传如何申请版主权限

  迅雷快传申请版主权限步骤: 1.在快传上传一些您想分享的资源,获取下载外链; 2.在您所属的版块发布资源分享帖,贴上快传的下载外链; 3.为了防止他人冒充,请在帖子内容增加一句"想轻松分享文件吗?迅雷快传邀请您享受极速上传.下载体验.一起来玩吧~"; 4.把该帖子的地址复制至版主申请页下方,并完善其他信息,通过审批即可免费开通版主权限了. 开通版主权限后,即与用户申请时的迅雷帐号绑定,请妥善保管好迅雷帐号.