web/enmi/Zend/Mail/Transport/Smtp.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Mail
       
    17  * @subpackage Transport
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Smtp.php 23424 2010-11-22 22:42:55Z bittarman $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * @see Zend_Mime
       
    26  */
       
    27 require_once 'Zend/Mime.php';
       
    28 
       
    29 /**
       
    30  * @see Zend_Mail_Protocol_Smtp
       
    31  */
       
    32 require_once 'Zend/Mail/Protocol/Smtp.php';
       
    33 
       
    34 /**
       
    35  * @see Zend_Mail_Transport_Abstract
       
    36  */
       
    37 require_once 'Zend/Mail/Transport/Abstract.php';
       
    38 
       
    39 
       
    40 /**
       
    41  * SMTP connection object
       
    42  *
       
    43  * Loads an instance of Zend_Mail_Protocol_Smtp and forwards smtp transactions
       
    44  *
       
    45  * @category   Zend
       
    46  * @package    Zend_Mail
       
    47  * @subpackage Transport
       
    48  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    49  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    50  */
       
    51 class Zend_Mail_Transport_Smtp extends Zend_Mail_Transport_Abstract
       
    52 {
       
    53     /**
       
    54      * EOL character string used by transport
       
    55      * @var string
       
    56      * @access public
       
    57      */
       
    58     public $EOL = "\n";
       
    59 
       
    60     /**
       
    61      * Remote smtp hostname or i.p.
       
    62      *
       
    63      * @var string
       
    64      */
       
    65     protected $_host;
       
    66 
       
    67 
       
    68     /**
       
    69      * Port number
       
    70      *
       
    71      * @var integer|null
       
    72      */
       
    73     protected $_port;
       
    74 
       
    75 
       
    76     /**
       
    77      * Local client hostname or i.p.
       
    78      *
       
    79      * @var string
       
    80      */
       
    81     protected $_name = 'localhost';
       
    82 
       
    83 
       
    84     /**
       
    85      * Authentication type OPTIONAL
       
    86      *
       
    87      * @var string
       
    88      */
       
    89     protected $_auth;
       
    90 
       
    91 
       
    92     /**
       
    93      * Config options for authentication
       
    94      *
       
    95      * @var array
       
    96      */
       
    97     protected $_config;
       
    98 
       
    99 
       
   100     /**
       
   101      * Instance of Zend_Mail_Protocol_Smtp
       
   102      *
       
   103      * @var Zend_Mail_Protocol_Smtp
       
   104      */
       
   105     protected $_connection;
       
   106 
       
   107 
       
   108     /**
       
   109      * Constructor.
       
   110      *
       
   111      * @param  string $host OPTIONAL (Default: 127.0.0.1)
       
   112      * @param  array|null $config OPTIONAL (Default: null)
       
   113      * @return void
       
   114      * 
       
   115      * @todo Someone please make this compatible
       
   116      *       with the SendMail transport class.
       
   117      */
       
   118     public function __construct($host = '127.0.0.1', Array $config = array())
       
   119     {
       
   120         if (isset($config['name'])) {
       
   121             $this->_name = $config['name'];
       
   122         }
       
   123         if (isset($config['port'])) {
       
   124             $this->_port = $config['port'];
       
   125         }
       
   126         if (isset($config['auth'])) {
       
   127             $this->_auth = $config['auth'];
       
   128         }
       
   129 
       
   130         $this->_host = $host;
       
   131         $this->_config = $config;
       
   132     }
       
   133 
       
   134 
       
   135     /**
       
   136      * Class destructor to ensure all open connections are closed
       
   137      *
       
   138      * @return void
       
   139      */
       
   140     public function __destruct()
       
   141     {
       
   142         if ($this->_connection instanceof Zend_Mail_Protocol_Smtp) {
       
   143             try {
       
   144                 $this->_connection->quit();
       
   145             } catch (Zend_Mail_Protocol_Exception $e) {
       
   146                 // ignore
       
   147             }
       
   148             $this->_connection->disconnect();
       
   149         }
       
   150     }
       
   151 
       
   152 
       
   153     /**
       
   154      * Sets the connection protocol instance
       
   155      *
       
   156      * @param Zend_Mail_Protocol_Abstract $client
       
   157      *
       
   158      * @return void
       
   159      */
       
   160     public function setConnection(Zend_Mail_Protocol_Abstract $connection)
       
   161     {
       
   162         $this->_connection = $connection;
       
   163     }
       
   164 
       
   165 
       
   166     /**
       
   167      * Gets the connection protocol instance
       
   168      *
       
   169      * @return Zend_Mail_Protocol|null
       
   170      */
       
   171     public function getConnection()
       
   172     {
       
   173         return $this->_connection;
       
   174     }
       
   175 
       
   176     /**
       
   177      * Send an email via the SMTP connection protocol
       
   178      *
       
   179      * The connection via the protocol adapter is made just-in-time to allow a
       
   180      * developer to add a custom adapter if required before mail is sent.
       
   181      *
       
   182      * @return void
       
   183      * @todo Rename this to sendMail, it's a public method...
       
   184      */
       
   185     public function _sendMail()
       
   186     {
       
   187         // If sending multiple messages per session use existing adapter
       
   188         if (!($this->_connection instanceof Zend_Mail_Protocol_Smtp)) {
       
   189             // Check if authentication is required and determine required class
       
   190             $connectionClass = 'Zend_Mail_Protocol_Smtp';
       
   191             if ($this->_auth) {
       
   192                 $connectionClass .= '_Auth_' . ucwords($this->_auth);
       
   193             }
       
   194             if (!class_exists($connectionClass)) {
       
   195                 require_once 'Zend/Loader.php';
       
   196                 Zend_Loader::loadClass($connectionClass);
       
   197             }
       
   198             $this->setConnection(new $connectionClass($this->_host, $this->_port, $this->_config));
       
   199             $this->_connection->connect();
       
   200             $this->_connection->helo($this->_name);
       
   201         } else {
       
   202             // Reset connection to ensure reliable transaction
       
   203             $this->_connection->rset();
       
   204         }
       
   205 
       
   206         // Set sender email address
       
   207         $this->_connection->mail($this->_mail->getReturnPath());
       
   208 
       
   209         // Set recipient forward paths
       
   210         foreach ($this->_mail->getRecipients() as $recipient) {
       
   211             $this->_connection->rcpt($recipient);
       
   212         }
       
   213 
       
   214         // Issue DATA command to client
       
   215         $this->_connection->data($this->header . Zend_Mime::LINEEND . $this->body);
       
   216     }
       
   217 
       
   218     /**
       
   219      * Format and fix headers
       
   220      *
       
   221      * Some SMTP servers do not strip BCC headers. Most clients do it themselves as do we.
       
   222      *
       
   223      * @access  protected
       
   224      * @param   array $headers
       
   225      * @return  void
       
   226      * @throws  Zend_Transport_Exception
       
   227      */
       
   228     protected function _prepareHeaders($headers)
       
   229     {
       
   230         if (!$this->_mail) {
       
   231             /**
       
   232              * @see Zend_Mail_Transport_Exception
       
   233              */
       
   234             require_once 'Zend/Mail/Transport/Exception.php';
       
   235             throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
       
   236         }
       
   237 
       
   238         unset($headers['Bcc']);
       
   239 
       
   240         // Prepare headers
       
   241         parent::_prepareHeaders($headers);
       
   242     }
       
   243 }