test_smtp.php
require("smtp.php");
$smtp=新的 smtp_class;
$smtp->host_name="mail.xiaocui.com";
$smtp->localhost="localhost";
$from="webmaster@xiaocui.com";
$to="root@xiaocui.com";
if($smtp->SendMessage(
$来自,
数组(
$至
),
数组(
“来自:$来自”,
“至:$至”,
“主题:测试 Manuel Lemos 的 SMTP 类”
),
“Hello $to,nn这只是为了让您知道您的 SMTP 类工作正常。nnBye.n”))
echo "消息已发送至 $to OK.n";
其他
echo "无法将消息发送到 $to.nError: ".$smtp->error."n"
?>
smtp.php
类 smtp_class
{
var $host_name="";
var $host_port=25;
var $localhost="";
var $timeout=0;
var $error="";
var $debug=1;
var $esmtp=1;
var $esmtp_host="";
var $esmtp_extensions=array();
var $maximum_piped_recipients=100;
/* 私有变量 - 请勿访问 */
var $state="已断开连接";
var $connection=0;
var $pending_recipients=0;
/* 私有方法 - 不要调用 */
函数 OutputDebug($message)
{
echo $message,"
n";
}
函数 GetLine()
{
for($line="";;)
{
if(feof($this->连接))
{
$this->error="从套接字读取时到达流末尾";
返回(0);
}
if(($data=fgets($this->连接,100))==false)
{
$this->error="无法从套接字读取行";
返回(0);
}
$line.=$data;
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="rn")
{
$line=substr($line,0,$length-2);
if($this->调试)
$this->OutputDebug("
返回($行);
}
}
}
函数 PutLine($line)
{
if($this->调试)
$this->OutputDebug("> $line");
if(!fputs($this->connection,"$linern"))
{
$this->error="无法将行写入套接字";
返回(0);
}
返回(1);
}
函数 PutData($data)
{
if(strlen($data))
{
if($this->调试)
$this->OutputDebug("> $data");
if(!fputs($this->连接,$data))
{
$this->error="无法将数据写入套接字";
返回(0);
}
}
返回(1);
}
函数VerifyResultLines($code,$responses="")
{
if(GetType($responses)!="array")
$responses=array();
取消设置($match_code);
while(($line=$this->GetLine($this->connection)))
{
if(IsSet($match_code))
{
if(strcmp(strtok($line," -"),$match_code))
{
$this->error=$line;
返回(0);
}
}
其他
{
$match_code=strtok($line," -");
if(GetType($code)=="数组")
{
for($codes=0;$codes
if($codes>=count($code))
{
$this->error=$line;
返回(0);
}
}
其他
{
if(strcmp($match_code,$code))
{
$this->error=$line;
返回(0);
}
}
}
$responses[]=strtok("");
if(!strcmp($match_code,strtok($line," ")))
返回(1);
}
返回(-1);
}
函数 FlushRecipients()
{
if($this->pending_sender)
{
if($this->VerifyResultLines("250")
返回(0);
$this->pending_sender=0;
}
for(;$this->pending_recipients;$this->pending_recipients--)
{
if($this->VerifyResultLines(array("250","251")) 返回(0);
}
返回(1);
}
/* 公共方法 */
函数 Connect()
{
$this->error=$error="";
$this->esmtp_host="";
$this->esmtp_extensions=array();
if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port))))
{
开关($错误)
{
情况-3:
$this->error="无法创建-3套接字";
返回(0);
情况-4:
$this->error="-4 主机名 "".$host_name."" 上的 dns 查找失败";
返回(0);
情况-5:
$this->error="-5 连接被拒绝或超时";
返回(0);
情况-6:
$this->error="-6 fdopen() 调用失败";
返回(0);
案例-7:
$this->error="-7 setvbuf() 调用失败";
返回(0);
默认值:
$this->error=$error." 无法连接到主机 "".$this->host_name.""";
返回(0);
}
}
其他
{
if(!strcmp($localhost=$this->localhost,"")
&& !strcmp($localhost=getenv("SERVER_NAME"),"")
&& !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
$成功=0;
if($this->VerifyResultLines("220")>0)
{
if($this->esmtp)
{
$responses=array();
if($this->PutLine("EHLO $localhost")
&& $this->VerifyResultLines("250",&$responses)>0)
{
$this->esmtp_host=strtok($responses[0]," ");
for($response=1;$response
{
$extension=strtoupper(strtok($responses[$response]," "));
$this->esmtp_extensions[$extension]=strtok("");
}
$成功=1;
}
}
if(!$成功
&& $this->PutLine("HELO $localhost")
&& $this->VerifyResultLines("250")>0)
$成功=1;
}
if($成功)
{
$this->state="已连接";
返回(1);
}
其他
{
fclose($this->连接);
$this->连接=0;
$this->state="已断开连接";
返回(0);
}
}
}
函数 MailFrom($sender)
{
if(strcmp($this->state,"已连接"))
{
$this->error="连接未处于初始状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("邮件来自:"))
返回(0);
if(!IsSet($this->esmtp_extensions["PIPELINING"])
&& $this->VerifyResultLines("250")
返回(0);
$this->state="SenderSet";
if(IsSet($this->esmtp_extensions["PIPELINING"]))
$this->pending_sender=1;
$this->pending_recipients=0;
返回(1);
}
函数 SetRecipient($recipient)
{
开关($this->状态)
{
案例“SenderSet”:
案例“RecipientSet”:
休息;
默认值:
$this->error="连接未处于收件人设置状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("RCPT TO:"))
返回(0);
if(IsSet($this->esmtp_extensions["PIPELINING"]))
{
$this->pending_recipients ;
if($this->pending_recipients>=$this->maximum_piped_recipients)
{
if(!$this->FlushRecipients())
返回(0);
}
}
其他
{
if($this->VerifyResultLines(array("250","251"))
返回(0);
}
$this->state="RecipientSet";
返回(1);
}
函数 StartData()
{
if(strcmp($this->state,"RecipientSet"))
{
$this->error="连接未处于开始发送数据状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("DATA"))
返回(0);
if($this->pending_recipients)
{
if(!$this->FlushRecipients())
返回(0);
}
if($this->VerifyResultLines("354")
返回(0);
$this->state="SendingData";
返回(1);
}
函数PrepareData($data,&$output)
{
$length=strlen(&$data);
for($output="",$position=0;$position
{
$next_position=$length;
for($current=$position;$current
{
开关($数据[$当前])
{
案例“n”:
$next_position=$current 1;
休息 2;
案例“r”:
$next_position=$current 1;
if($data[$next_position]=="n")
$下一个位置;
休息 2;
}
}
if($data[$position]==".")
$output.=".";
$output.=substr(&$data,$position,$current-$position)."rn";
$position=$next_position;
}
}
函数 SendData($data)
{
if(strcmp($this->state,"SendingData"))
{
$this->error="连接未处于发送数据状态";
返回(0);
}
$this->error="";
return($this->PutData(&$data));
}
函数 EndSendingData()
{
if(strcmp($this->state,"SendingData"))
{
$this->error="连接未处于发送数据状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("rn.")
|| $this->VerifyResultLines("250")
返回(0);
$this->state="已连接";
返回(1);
}
函数 ResetConnection()
{
开关($this->状态)
{
案例“已连接”:
返回(1);
案例“发送数据”:
$this->error="发送数据时无法重置连接";
返回(0);
案例“已断开连接”:
$this->error="在建立连接之前无法重置连接";
返回(0);
}
$this->error="";
if(!$this->PutLine("RSET")
|| $this->VerifyResultLines("250")
返回(0);
$this->state="已连接";
返回(1);
}
函数断开连接($quit=1)
{
if(!strcmp($this->state,"已断开连接"))
{
$this->error="之前未建立 SMTP 连接";
返回(0);
}
$this->error="";
if(!strcmp($this->state,"已连接")
&& $退出
&& (!$this->PutLine("QUIT")
|| $this->VerifyResultLines("221")
返回(0);
fclose($this->连接);
$this->连接=0;
$this->state="已断开连接";
返回(1);
}
函数 SendMessage($sender,$recipients,$headers,$body)
{
if(($success=$this->Connect()))
{
if(($success=$this->MailFrom($sender)))
{
for($recipient=0;$recipient
{
if(!($success=$this->SetRecipient($recipients[$recipient])))
休息;
}
如果($成功
&& ($success=$this->StartData()))
{
for($header_data="",$header=0;$header
$header_data.=$headers[$header]."rn";
if(($success=$this->SendData($header_data."rn")))
{
$this->PrepareData($body,&$body_data);
$success=$this->SendData($body_data);
}
if($成功)
$success=$this->EndSendingData();
}
}
$disconnect_success=$this->断开连接($success);
if($成功)
$成功=$disconnect_success;
}
返回($成功);
}
};
?>