日期:2011-01-27  浏览次数:20389 次

PHP 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 正文中如有图片,请用绝对路径的引用 "httP://host/path/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="??????";
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