File indexing completed on 2024-12-22 05:36:51

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Mail
0017  * @subpackage Transport
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * @see Zend_Mime
0026  */
0027 // require_once 'Zend/Mime.php';
0028 
0029 /**
0030  * @see Zend_Mail_Protocol_Smtp
0031  */
0032 // require_once 'Zend/Mail/Protocol/Smtp.php';
0033 
0034 /**
0035  * @see Zend_Mail_Transport_Abstract
0036  */
0037 // require_once 'Zend/Mail/Transport/Abstract.php';
0038 
0039 
0040 /**
0041  * SMTP connection object
0042  *
0043  * Loads an instance of Zend_Mail_Protocol_Smtp and forwards smtp transactions
0044  *
0045  * @category   Zend
0046  * @package    Zend_Mail
0047  * @subpackage Transport
0048  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0049  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0050  */
0051 class Zend_Mail_Transport_Smtp extends Zend_Mail_Transport_Abstract
0052 {
0053     /**
0054      * EOL character string used by transport
0055      * @var string
0056      * @access public
0057      */
0058     public $EOL = "\n";
0059 
0060     /**
0061      * Remote smtp hostname or i.p.
0062      *
0063      * @var string
0064      */
0065     protected $_host;
0066 
0067 
0068     /**
0069      * Port number
0070      *
0071      * @var integer|null
0072      */
0073     protected $_port;
0074 
0075 
0076     /**
0077      * Local client hostname or i.p.
0078      *
0079      * @var string
0080      */
0081     protected $_name = 'localhost';
0082 
0083 
0084     /**
0085      * Authentication type OPTIONAL
0086      *
0087      * @var string
0088      */
0089     protected $_auth;
0090 
0091 
0092     /**
0093      * Config options for authentication
0094      *
0095      * @var array
0096      */
0097     protected $_config;
0098 
0099 
0100     /**
0101      * Instance of Zend_Mail_Protocol_Smtp
0102      *
0103      * @var Zend_Mail_Protocol_Smtp
0104      */
0105     protected $_connection;
0106 
0107 
0108     /**
0109      * Constructor.
0110      *
0111      * @param  string $host OPTIONAL (Default: 127.0.0.1)
0112      * @param  array|null $config OPTIONAL (Default: null)
0113      * @return void
0114      *
0115      * @todo Someone please make this compatible
0116      *       with the SendMail transport class.
0117      */
0118     public function __construct($host = '127.0.0.1', Array $config = array())
0119     {
0120         if (isset($config['name'])) {
0121             $this->_name = $config['name'];
0122         }
0123         if (isset($config['port'])) {
0124             $this->_port = $config['port'];
0125         }
0126         if (isset($config['auth'])) {
0127             $this->_auth = $config['auth'];
0128         }
0129 
0130         $this->_host = $host;
0131         $this->_config = $config;
0132     }
0133 
0134 
0135     /**
0136      * Class destructor to ensure all open connections are closed
0137      *
0138      * @return void
0139      */
0140     public function __destruct()
0141     {
0142         if ($this->_connection instanceof Zend_Mail_Protocol_Smtp) {
0143             try {
0144                 $this->_connection->quit();
0145             } catch (Zend_Mail_Protocol_Exception $e) {
0146                 // ignore
0147             }
0148             $this->_connection->disconnect();
0149         }
0150     }
0151 
0152 
0153     /**
0154      * Sets the connection protocol instance
0155      *
0156      * @param Zend_Mail_Protocol_Abstract $client
0157      *
0158      * @return void
0159      */
0160     public function setConnection(Zend_Mail_Protocol_Abstract $connection)
0161     {
0162         $this->_connection = $connection;
0163     }
0164 
0165 
0166     /**
0167      * Gets the connection protocol instance
0168      *
0169      * @return Zend_Mail_Protocol|null
0170      */
0171     public function getConnection()
0172     {
0173         return $this->_connection;
0174     }
0175 
0176     /**
0177      * Send an email via the SMTP connection protocol
0178      *
0179      * The connection via the protocol adapter is made just-in-time to allow a
0180      * developer to add a custom adapter if required before mail is sent.
0181      *
0182      * @return void
0183      * @todo Rename this to sendMail, it's a public method...
0184      */
0185     public function _sendMail()
0186     {
0187         // If sending multiple messages per session use existing adapter
0188         if (!($this->_connection instanceof Zend_Mail_Protocol_Smtp)) {
0189             // Check if authentication is required and determine required class
0190             $connectionClass = 'Zend_Mail_Protocol_Smtp';
0191             if ($this->_auth) {
0192                 $connectionClass .= '_Auth_' . ucwords($this->_auth);
0193             }
0194             if (!class_exists($connectionClass)) {
0195                 // require_once 'Zend/Loader.php';
0196                 Zend_Loader::loadClass($connectionClass);
0197             }
0198             $this->setConnection(new $connectionClass($this->_host, $this->_port, $this->_config));
0199             $this->_connection->connect();
0200             $this->_connection->helo($this->_name);
0201         } else {
0202             // Reset connection to ensure reliable transaction
0203             $this->_connection->rset();
0204         }
0205 
0206         // Set sender email address
0207         $this->_connection->mail($this->_mail->getReturnPath());
0208 
0209         // Set recipient forward paths
0210         foreach ($this->_mail->getRecipients() as $recipient) {
0211             $this->_connection->rcpt($recipient);
0212         }
0213 
0214         // Issue DATA command to client
0215         $this->_connection->data($this->header . Zend_Mime::LINEEND . $this->body);
0216     }
0217 
0218     /**
0219      * Format and fix headers
0220      *
0221      * Some SMTP servers do not strip BCC headers. Most clients do it themselves as do we.
0222      *
0223      * @access  protected
0224      * @param   array $headers
0225      * @return  void
0226      * @throws  Zend_Transport_Exception
0227      */
0228     protected function _prepareHeaders($headers)
0229     {
0230         if (!$this->_mail) {
0231             /**
0232              * @see Zend_Mail_Transport_Exception
0233              */
0234             // require_once 'Zend/Mail/Transport/Exception.php';
0235             throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
0236         }
0237 
0238         unset($headers['Bcc']);
0239 
0240         // Prepare headers
0241         parent::_prepareHeaders($headers);
0242     }
0243 }