php define('SMTP_STATUS_NOT_CONNECTED',1,TRU
E);
define('SMTP_STATUS_CONNECTED',2,TRU
E); 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”,TRU
E); $this->timeout=
5; $this->status=SMTP_STATUS_NOT_CONNECTED;
$this->host=‘localhost';
$this->port=2
5; $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$ob
j; }
else
{ $this->connection=fsockopen($this->host,$this->port,$errno,$errstr,$this->timeout
); socket_set_timeout($this->connection,250000
); $gree
Ting=$this->get_data(
); if(is_
resource($this->connection))
{ $this->status=2;
return$this->auth?$this->ehlo():$this->
Helo(
); }
else
{ $this->errors[]=‘Failedtoconnecttoserver:‘.$errstr;
return
falSE;
}
}
} /
** *参数为数组
*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())return
falSE;
} $this->mail($this->from
); if(is_array($this->recipients))
{ foreach($this->recipientsas$
value) { $this->rcpt($
value);
}
}
else
{ $this->rcpt($this->recipients
); } if(!$this->data())return
falSE; $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!';
return
falSE;
}
} function
Helo()
{ 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)
); return
falSE;
}
} 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)
); return
falSE;
}
} 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))//Send
password
ANDsubstr(trim($error=$this->get_data()),3)===‘235′)
{ returnTRUE;
}
else
{ $this->errors[]=‘AUTHcommandfailed:‘.trim(substr(trim($error),3)
); return
falSE;
}
} functionmail($from)
{ if($this->is_connected()
AND$this->send_data('MAILFROM:<'.$from.'>')
ANDsubstr(trim($this->get_data()),2)===‘250′)
{ returnTRUE;
}
else
{ return
falSE;
}
} 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)
); return
falSE;
}
} 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)
); return
falSE;
}
} 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
{ return
falSE;
}
} 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===tru
E) echonl2br($return.CRLF
); return$return; }
else
{ return
falSE;
}
} functionset($var,$
value) { $this->$var=$value;
returnTRUE;
}
}//Endofclass
?>