PHP   发布时间:2019-11-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php a simple smtp class大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

smtp.class.php


title"> 代码如下:
php define('SMTP_STATUS_NOT_CONNECTED',1,TRUE);
define('SMTP_STATUS_CONNECTED',2,TRUE); classsmtp
{ var$connection;
var$recipients;
var$headers;
var$timeout;
var$errors;
var$status;
var$body;
var$from;
var$host;
var$port;
var$Helo;
var$auth;
var$user;
var$pass;
var$debug; /**
*参数为一个数组
*hostSMTP服务器的主机默认:localhost
*portSMTP服务器的端口默认:25
*Helo发送HelO命令的名称默认:localhost
*userSMTP服务器的用户名默认:空值
*passSMTP服务器的登陆密码默认:空值
*timeout连接超时的时间默认:5
*@returnbool
*/ functionsmtp($params=array())
{ if(!defined('CRLF'))define('CRLF',“\r\n”,TRUE); $this->timeout=5;
$this->status=SMTP_STATUS_NOT_CONNECTED;
$this->host=‘localhost';
$this->port=25;
$this->auth=falSE;
$this->user=”;
$this->pass=”;
$this->errors=array();
$this->debug=false;
foreach($paramsas$key=>$value)
{
$this->$key=$value;
} $this->Helo=$this->host; //如果没有设置用户名则不验证
$this->auth=(”==$this->user)?falSE:TRUE;
} functionconnect($params=array())
{ if(!isset($this->status))
{
$obj=newsmtp($params); if($obj->connect())
{
$obj->status=SMTP_STATUS_CONNECTED;
} return$obj; }
else
{ $this->connection=fsockopen($this->host,$this->port,$errno,$errstr,$this->timeout);
socket_set_timeout($this->connection,250000); $greeTing=$this->get_data(); if(is_resource($this->connection))
{
$this->status=2;
return$this->auth?$this->ehlo():$this->Helo();
}
else
{
$this->errors[]=‘Failedtoconnecttoserver:‘.$errstr;
returnfalSE;
}
}
} /**
*参数为数组
*recipients接收人的数组
*from发件人的地址,也将作为回复地址
*headers头部信息的数组
*body邮件的主体
*/ functionsend($params=array())
{ foreach($paramsas$key=>$value)
{
$this->set($key,$value);
} if($this->is_connected())
{
//服务器是否需要验证
if($this->auth)
{
if(!$this->auth())returnfalSE;
} $this->mail($this->from); if(is_array($this->recipients))
{
foreach($this->recipientsas$value)
{
$this->rcpt($value);
}
}
else
{
$this->rcpt($this->recipients);
} if(!$this->data())returnfalSE; $headers=str_replace(CRLF.'.',CRLF.'..',trim(implode(CRLF,$this->headers)));
$body=str_replace(CRLF.'.',$this->body);
$body=$body[0]==‘.'?‘.'.$body:$body; $this->send_data($headers);
$this->send_data(”);
$this->send_data($body);
$this->send_data('.'); return(substr(trim($this->get_data()),3)===‘250′);
}
else
{
$this->errors[]=‘Notconnected!';
returnfalSE;
}
} functionHelo()
{
if(is_resource($this->connection)
AND$this->send_data('HelO‘.$this->Helo)
ANDsubstr(trim($error=$this->get_data()),3)===‘250′)
{
returnTRUE; }
else
{
$this->errors[]=‘HelOcommandfailed,output:‘.trim(substr(trim($error),3));
returnfalSE;
}
} functionehlo()
{
if(is_resource($this->connection)
AND$this->send_data('EHLO‘.$this->Helo)
ANDsubstr(trim($error=$this->get_data()),3)===‘250′)
{
returnTRUE;
}
else
{
$this->errors[]=‘EHLOcommandfailed,3));
returnfalSE;
}
} functionauth()
{
if(is_resource($this->connection)
AND$this->send_data('AUTHLOGIN')
ANDsubstr(trim($error=$this->get_data()),3)===‘334′
AND$this->send_data(base64_encode($this->user))//Sendusername
ANDsubstr(trim($error=$this->get_data()),3)===‘334′
AND$this->send_data(base64_encode($this->pass))//Sendpassword
ANDsubstr(trim($error=$this->get_data()),3)===‘235′)
{
returnTRUE;
}
else
{
$this->errors[]=‘AUTHcommandfailed:‘.trim(substr(trim($error),3));
returnfalSE;
}
} functionmail($from)
{ if($this->is_connected()
AND$this->send_data('MAILFROM:<'.$from.'>')
ANDsubstr(trim($this->get_data()),2)===‘250′)
{
returnTRUE;
}
else
{
returnfalSE;
}
} functionrcpt($to)
{
if($this->is_connected()
AND$this->send_data('RCPTTO:<'.$to.'>')
ANDsubstr(trim($error=$this->get_data()),2)===‘25′)
{
returnTRUE;
}
else
{
$this->errors[]=trim(substr(trim($error),3));
returnfalSE;
}
} functiondata()
{ if($this->is_connected()
AND$this->send_data('DATA')
ANDsubstr(trim($error=$this->get_data()),3)===‘354′)
{
returnTRUE;
}
else
{
$this->errors[]=trim(substr(trim($error),3));
returnfalSE;
}
} functionis_connected()
{
return(is_resource($this->connection)AND($this->status===SMTP_STATUS_CONNECTED));
} functionsend_data($data)
{
if(is_resource($this->connection))
{
if($this->debug)
echonl2br($data.CRLF);
returnfwrite($this->connection,$data.CRLF,strlen($data)+2);
}
else
{
returnfalSE;
}
} function&get_data()
{ $return=”;
$line=”; if(is_resource($this->connection))
{
while(strpos($return,CRLF)===falSEORsubstr($line,3,1)!==‘‘)
{
$line=fgets($this->connection,512);
$return.=$line;
}
if($this->debug===truE)
echonl2br($return.CRLF);
return$return; }
else
{
returnfalSE;
}
} functionset($var,$value)
{
$this->$var=$value;
returnTRUE;
}
}//Endofclass
?>

大佬总结

以上是大佬教程为你收集整理的php a simple smtp class全部内容,希望文章能够帮你解决php a simple smtp class所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:aphpsmtp