简单的smtp发邮件类

/**   
 * @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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值