Location: PHPKode > scripts > XS PHP Library > xs-php-library/inc/mailwriter.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: mailwriter.inc.php,v 1.1.1.1 2002/11/26 23:00:40 rbala Exp $

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

	/**
	 * IP address or host name.
     * @access private
	 * @var string
	 */
    var $_host;
    
	/**
	 * TCP port number.
     * @access private
	 * @var int
	 */
    var $_port;
    
	/**
	 * Indicates whether to use SMTP authentication or not.
     * @access private
	 * @var boolean
	 */
    var $_auth;
    
	/**
	 * 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 SMTP access class.
     * @access private
     * @var object
     */
    var $_smtp;

    /**
     * MailWriter class constructor.
     *
     * Creates the new instance of MailWriter class and sets up basic properties.
     *
     * @access public
     * @param string $host IP address or host name.
     * @param boolean $auth indicates whether to use SMTP authentication or not, defaults to false.
     * @param string $user the username to login, defaults to "".
     * @param string $pass the password to login, defaults to "".
     * @param int $port TCP port number, defaults to 25.
     * @return void
     */
    function MailWriter($host, $auth=false, $user='', $pass='', $port=25) {
        Object::Object();
        $this->_host = $host;
        $this->_port = $port;
        $this->_auth = $auth;
        $this->_user = $user;
        $this->_pass = $pass;
        $this->_smtp = null;
    }
    
    /**
     * Attempt to open connection to the SMTP 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()) {
            $smtp = new SMTP($this->_host, $this->_port);
            if (object_isError($smtp)) {
                return new Error('open(): MailWriter unable to establish connection.');
            }
            if (object_isError($smtp->open())) {
                return new Error('open(): MailWriter unable to establish connection.');
            }
            if ($this->_auth) {
                if (object_isError($smtp->auth($this->_user, $this->_pass))) {
                    return new Error('open(): MailWriter user authorization failed.');
                }
            }
            $this->_smtp =& $smtp;
        } else {
            return new Error('open(): MailWriter socket connection already established.');
        }
        return true;
    }
    
    /**
     * Attempt to close connection from the SMTP 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->_smtp->close())) {
                return new Error('close(): MailWriter unable disconnect.');
            }
        } else {
            return new Error('close(): MailWriter socket connection not established.');
        }
        return true;
    }

    /**
     * Attempt to send mail message throught the SMTP server.
     *
     * Returns true on success or a error object with an error message on any
     * kind of failure.
     *
     * @access public
     * @param object $mail the valid {@link Mail} object.
     * @return mixed
     */
    function write(&$mail) {
        if ($this->isOpened()) {
            if (!object_isClass($mail, 'mail')) {
                return new Error('write(): MailWriter invalid mail object.');
            }
            if (!$mail->getFrom()) {
                return new Error('write(): MailWriter from address has not been set.');
            }
            if (object_isError($this->_smtp->mail($mail->getFrom()))) {
                return new Error('close(): MailWriter invalid sender address.');
            }
            if (!$mail->getTo()) {
                return new Error('write(): MailWriter to address has not been set.');
            }
            $rcpts = explode(',', $mail->readRcpts());
            for ($i = 0; $i < count($rcpts); $i++) {
                if (object_isError($this->_smtp->rcpt($rcpts[$i]))) {
                    return new Error('write(): MailWriter invalid recipient address.');
                }
            }
            if (object_isError($this->_smtp->data($mail->mailHeaders() . $mail->mailMessage()))) {
                return new Error('close(): MailWriter unable to send data.');
            }
        } else {
            return new Error('write(): MailWriter socket connection not established.');
        }
		return true;
    }
    
    /**
     * 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->_smtp) && $this->_smtp->isOpened());
    }
    
}

?>
Return current item: XS PHP Library