Fatal error: Using $this when not in object context
是说$this只能用于类定义中

解决方案 »

  1.   

    class SendMail{
        /**
         * smtp parameters
         * @var array(host, auth, port, username, password)
         */
        var $smtp_param;
        
        /**
         * mail headers
         * @var array
         */
        var $headers;
        
        /**
         * the body of the mail to be sent
         * @var string
         */
        var $body;
        
        /**
         * attachment of the mail
         * @var array
         */
        var $attachment;
         
        function connect($host, $username, $pwd){
            $class_name = __class__;
            $object = new $class_name;
            $object->smtp_param = array(
                'host' => $host,
                'auth' => true,
                'port' => 25,
                'username' => $username,
                'password' => $pwd );
            $this->headers = array();
            $this->attachment = array();
            return $object;
        }
        
        function setSubject($subject){
            $this->headers['Subject'] = $subject;
        }
        
        function setBody($body){
            $this->body = $body;
    }

    function addAttachment($file){
        $this->attachment[] = $file;
    }

    function addHeaders($key, $value){
        $this->headers[$key] = $value;
    }

    function sendTo($recipient){
        $mime = new Mail_mime();
        
        //$mime->setTxtBody($this->body);
        $_ = $mime->setHTMLBody($this->body);
        if(PEAR::isError($_)){
            print($_->getMessage());
            return false;
    }
    /*
    if( !empty($this->attachment) ){
        foreach( $this->attachment as $_ ){
            $r = $mime->addAttachment( $_ );
            if( PEAR::isError( $r ) ){
                print( $r->getMessage() );
                return false;
            }
    }
    }
    */
    //This function should be called once you have added the text/html/images/attachments.
    //It builds the message and returns it.
    $body = $mime->get( array ( 
        "text_encoding" => 'base64',
    "html_encoding" => 'base64',
    "head_charset" => 'GB2312',
    "text_charset" => 'GB2312',
    "html_charset" => 'GB2312' ) ) ;
        
        if ( PEAR::isError( $body ) ){
            print( $body->getMessage() );
            return false;
    }

    $headers = $mime->headers( $this->headers );

    if ( PEAR::isError( $headers ) ){
       print( $headers->getMessage() );
       return false;
    }

    $mail = Mail::factory( 'smtp', $this->smtp_param );

    if ( PEAR::isError( $mail ) ){
       print( $mail->getMessage() );
       return false;
    }

    $a = $mail->send($recipient, $headers, $body);
    if ( PEAR::isError ( $a ) ){
        print( $a->getMessage() );
        return false;
    }
    return true;
    }
    }这是源代码  “$this->headers = array();”这句报错说:Fatal error: Using $this when not in object context
      

  2.   

    这是 PEAR 的类,不应该有错的请给出完整的错误信息
      

  3.   

    Fatal error: Using $this when not in object context in /var/www/html/admin/lib/sendmail.class.php on line 61这是完整的错误信息