php a simple smtp class
php a simple smtp class
发布时间:2016-12-29 来源:查字典编辑
摘要:smtp.class.php复制代码代码如下:

smtp.class.php

复制代码 代码如下:

<?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',“rn”,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,0,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.'.',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()),0,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()),0,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()),0,3)===‘250′)

{

returnTRUE;

}

else

{

$this->errors[]=‘EHLOcommandfailed,output:‘.trim(substr(trim($error),3));

returnFALSE;

}

}

functionauth()

{

if(is_resource($this->connection)

AND$this->send_data('AUTHLOGIN')

ANDsubstr(trim($error=$this->get_data()),0,3)===‘334′

AND$this->send_data(base64_encode($this->user))//Sendusername

ANDsubstr(trim($error=$this->get_data()),0,3)===‘334′

AND$this->send_data(base64_encode($this->pass))//Sendpassword

ANDsubstr(trim($error=$this->get_data()),0,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()),0,2)===‘250′)

{

returnTRUE;

}

else

{

returnFALSE;

}

}

functionrcpt($to)

{

if($this->is_connected()

AND$this->send_data('RCPTTO:<'.$to.'>')

ANDsubstr(trim($error=$this->get_data()),0,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()),0,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教程学习
热门php教程学习
编程开发子分类