/**
* @author jiangkejun
* @name sendmail for fsockopen (smtp)
* @version 1.1.10 Simple
* @since 2013.5.29 22:37
* @see SMTP Protocol http://www.doc88.com/p-115669378978.html
*/
class smtp
{
var $_conn;
var $CRLF = "\r\n"; // CR+LF符
var $_cfg = array();
var $_domain = array('@163.com', '@126.com', '@qq.com', '@139.com'); // 有效域
var $debug = false; //
function __construct( $jp ){
$this->_cfg = ( array )$jp;
}
/**
* 连接服务器
*
* @param int $timeout
*/
function connect( $timeout=30 ){
if( empty( $this->_cfg['from'] ) ){
exit( "Must fill in the sender" );
}
if( empty( $this->_cfg['host'] ) ){
$suffix = strstr( $this->_cfg['from'], '@' );
if( in_array( $suffix, $this->_domain ) )
$this->_cfg['host'] = __CLASS__ . "." . substr( $suffix, 1 );
else {
exit( "Must fill in the SMTP address" );
}
}
/// 各种validation ///
/
// var_dump($this->_cfg);
// 尝试链接通道并得到句柄
if( !$this->_conn )
$this->_conn = @fsockopen( $this->_cfg['host'],
$this->_cfg['port'],
$errno, $errstr,
$timeout );
// 释放连接成功的Response...
$first = $this->getResponseOfLine();
}
/**
* 发送
*/
public function send(){
///----- HELO -----///
$this->_puts( "HELO localhost" . $this->CRLF, 250 );
///----- AUTH LOGIN -----///
$this->_puts( "AUTH LOGIN" . $this->CRLF, 334 );
$this->_puts( base64_encode( $this->_cfg['user'] ) . $this->CRLF, 334 );
$this->_puts( base64_encode( $this->_cfg['pass'] ) . $this->CRLF, 235 );
///----- MAIL FROM -----///
$this->_puts( "MAIL FROM:<" . $this->_cfg['from'] . ">" . $this->CRLF, 250 );
///----- RCPT TO -----///
$this->_puts( "RCPT TO:<" . $this->_cfg['to'] . ">" . $this->CRLF, 250 );
// 250, 251
///----- DATA options -----///
$this->_puts( "DATA" . $this->CRLF, 354 );
// skip
$this->_puts( "From:" . $this->_cfg['from'] . $this->CRLF, 250, false);
$this->_puts( "Subject:" . $this->_cfg['subject'] . $this->CRLF, 250, false);
$this->_puts( "To:" . $this->_cfg['to'] . $this->CRLF, 250, false);
$this->_puts( $this->CRLF, 250, false);
$this->_puts( $this->_cfg['content'] . $this->CRLF, 250, false);
$this->_puts( $this->CRLF . "." . $this->CRLF, 250);
///----- QUIT -----///
$this->_puts( "QUIT" . $this->CRLF, 221);
$this->_close();
echo 'SUCCESS';
}
/**
* 写入流 (可重构)
*
* @param string $cmd 命令
* @param int $success_code 成功代码
* @param int|bool $skip 是否跳过捕获
*/
function _puts( $cmd, $success_code, $skip=true ){
if( !$this->_conn ) exit( "disconnected" );
fputs( $this->_conn, $cmd );
if( $skip ) {
$rply = $this->getResponseOfLine();
$code = substr( $rply, 0, 3 );
if( $code != $success_code ) {
$this->_stop( $code );
}
}
}
/**
* 关闭连接 void
*/
function _close(){
if ( $this->_conn ) {
fclose( $this->_conn );
$this->_conn = null;
}
}
/**
* 出现错误停止 void
*
* @param int $errno
*/
function _stop( $errno=0 ){
$this->_close();
if( $this->debug ){
trigger_error( "errno:$errno", E_USER_ERROR );
}
exit( "FAILURE" );
}
/**
* 服务器返回当前写入流结果
*
* @return string response
*/
private function getResponseOfLine(){
$response = "";
while( $str = @fgets( $this->_conn, 512 ) ) {
$response .= $str;
if( substr( $str, 3, 1 ) == " " ) { break; }
}
return $response;
}
}
$data= array();
$data['port']=25;
$data['user']='j******';
$data['pass']='*******';
$data['from']='XXXXXX@163.com';
$data['to'] ='XXXXXXX@qq.com';
$data['subject'] ='i`m like nike!';
$data['content'] ='yes,i`m';
$a = new smtp($data);
$a->connect();
$a->send();
简单的smtp发邮件类
最新推荐文章于 2024-04-09 09:32:14 发布