Location: PHPKode > scripts > XS PHP Library > xs-php-library/inc/mailreader.inc.php
<?php
//
// +--------------------------------------------------------------------------+
// |                                                                          |
// |                   XS PHP Library  Generic Classes Library                |
// |                                                                          |
// |                   Copyright (c) 2001-2002 XSPHPLib Group.                |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Distributed under the terms of the GNU Lesser General Public License as  |
// | published by the Free Software Foundation version 2.1                    |
// | See the GNU Lesser General Public License for more details. You should   |
// | have received a copy of the GNU Lesser General Public License along with |
// | this package; if not, write to the Free Software Foundation, Inc.,       |
// | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Authors: Robert Bala <hide@address.com>                                   |
// |                                                                          |
// +--------------------------------------------------------------------------+
//
// $Id: mailreader.inc.php,v 1.1.1.1 2002/11/26 23:00:40 rbala Exp $

/**
 * MailReader class.
 *
 * MailReader class is a helper class which enables simple fetching mail
 * messages from POP3 server. When the connection to the POP3 server will be
 * opened multiple messages can be fetched using the MailReader::read() method.
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @package net
 * @version $Id: mailreader.inc.php,v 1.1.1.1 2002/11/26 23:00:40 rbala Exp $
 */
class MailReader extends Object {

	/**
	 * IP address or host name.
     * @access private
	 * @var string
	 */
    var $_host;
    
	/**
	 * TCP port number.
     * @access private
	 * @var int
	 */
    var $_port;
    
	/**
	 * The username used to login to POP3 server.
     * @access private
	 * @var string
	 */
    var $_user;
    
	/**
	 * The password used to login to POP3 server.
     * @access private
	 * @var string
	 */
    var $_pass;
    
	/**
     * The POP3 access class.
     * @access private
     * @var object
     */
    var $_pop3;

    /**
     * MailReader class constructor.
     *
     * Creates the new instance of MailReader class and sets up basic properties.
     *
     * @access public
     * @param string $host IP address or host name.
     * @param string $user the username to login.
     * @param string $pass the password to login.
     * @param int $port TCP port number, defaults to 110.
     * @return void
     */
    function MailReader($host, $user, $pass, $port=110) {
        Object::Object();
        $this->_host = $host;
        $this->_port = $port;
        $this->_user = $user;
        $this->_pass = $pass;
        $this->_pop3 = null;
    }
    
    /**
     * Attempt to open connection to the POP3 server.
     *
     * If called when the connection is already established, it returns error
     * object otherwise it returns true on success or a error object with an
     * error message on any kind of failure.
     *
     * @access public
     * @return mixed
     */
    function open() {
        if (!$this->isOpened()) {
            $pop3 = new POP3($this->_host, $this->_port);
            if (object_isError($pop3)) {
                return new Error('open(): MailReader unable to establish connection.');
            }
            if (object_isError($pop3->open())) {
                return new Error('open(): MailReader unable to establish connection.');
            }
            if (object_isError($pop3->auth($this->_user, $this->_pass))) {
                return new Error('open(): MailReader user authorization failed.');
            }
            $this->_pop3 =& $pop3;
        } else {
            return new Error('open(): MailReader socket connection already established.');
        }
        return true;
    }
    
    /**
     * Attempt to close connection from the POP3 server.
     *
     * If called when the connection is not established, it returns error
     * object otherwise it returns true on success or a error object with an
     * error message on any kind of failure.
     *
     * @access public
     * @return mixed
     */
    function close() {
        if ($this->isOpened()) {
            if (object_isError($this->_pop3->close())) {
                return new Error('close(): MailReader unable to disconnect.');
            }
        } else {
            return new Error('close(): MailReader socket connection not established.');
        }
        return true;
    }
    
    /**
     * Attempt to fetch mail message from the POP3 server.
     *
     * Returns the {@link Mail} object on success or a error object with an
     * error message on any kind of failure.
     *
     * @access public
     * @param int $index the message index to retrieve.
     * @return mixed
     */
    function &read($index) {
        if ($this->isOpened()) {
            $content = $this->_pop3->retr($index);
            if (object_isError($content)) {
                return new Error('read(): MailReader unable to fetch message.');
            }
            $mail = new Mail;
            if (object_isError($mail)) {
                return new Error('read(): MailReader unable to process message.');
            }
            $mail->parseMessage($content);
        } else {
            return new Error('read(): MailReader socket connection not established.');
        }
		return $mail;
    }
    
    /**
     * Finds whether the socket connection is opened.
     *
     * Returns true if the connection is established, false otherwise.
     *
     * @access public
     * @return boolean
     */
    function isOpened() {
        return (isset($this->_pop3) && $this->_pop3->isOpened());
    }
    
}

?>
Return current item: XS PHP Library