web/lib/Zend/Application/Resource/Mail.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Application
    16  * @package    Zend_Application
    17  * @subpackage Resource
    17  * @subpackage Resource
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Mail.php 24593 2012-01-05 20:35:02Z matthew $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Application_Resource_ResourceAbstract
    24  * @see Zend_Application_Resource_ResourceAbstract
    25  */
    25  */
    30  *
    30  *
    31  * @uses       Zend_Application_Resource_ResourceAbstract
    31  * @uses       Zend_Application_Resource_ResourceAbstract
    32  * @category   Zend
    32  * @category   Zend
    33  * @package    Zend_Application
    33  * @package    Zend_Application
    34  * @subpackage Resource
    34  * @subpackage Resource
    35  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    35  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  */
    37  */
    38 class Zend_Application_Resource_Mail extends Zend_Application_Resource_ResourceAbstract
    38 class Zend_Application_Resource_Mail extends Zend_Application_Resource_ResourceAbstract
    39 {
    39 {
    40 
    40 
    41     /**
    41     /**
    42      * @var Zend_Mail_Transport_Abstract
    42      * @var Zend_Mail_Transport_Abstract
    43      */
    43      */
    44     protected $_transport;
    44     protected $_transport;
    45 
    45 
    46     public function init() {
    46     public function init()
       
    47     {
    47         return $this->getMail();
    48         return $this->getMail();
    48     }
    49     }
    49 
    50 
    50     /**
    51     /**
    51      *
    52      *
    53      */
    54      */
    54     public function getMail()
    55     public function getMail()
    55     {
    56     {
    56         if (null === $this->_transport) {
    57         if (null === $this->_transport) {
    57             $options = $this->getOptions();
    58             $options = $this->getOptions();
    58             foreach($options as $key => $option) {
    59             foreach ($options as $key => $option) {
    59                 $options[strtolower($key)] = $option;
    60                 $options[strtolower($key)] = $option;
    60             }
    61             }
    61             $this->setOptions($options);
    62             $this->setOptions($options);
    62 
    63 
    63             if(isset($options['transport']) &&
    64             if (isset($options['transport'])
    64                !is_numeric($options['transport']))
    65                 && !is_numeric($options['transport'])
    65             {
    66             ) {
    66                 $this->_transport = $this->_setupTransport($options['transport']);
    67                 $this->_transport = $this->_setupTransport($options['transport']);
    67                 if(!isset($options['transport']['register']) ||
    68                 if (!isset($options['transport']['register'])
    68                    $options['transport']['register'] == '1' ||
    69                     || $options['transport']['register'] == '1'
    69                    (isset($options['transport']['register']) &&
    70                     || (isset($options['transport']['register'])
    70                         !is_numeric($options['transport']['register']) &&
    71                         && !is_numeric($options['transport']['register'])
    71                         (bool) $options['transport']['register'] == true))
    72                         && (bool)$options['transport']['register'] == true)
    72                 {
    73                 ) {
    73                     Zend_Mail::setDefaultTransport($this->_transport);
    74                     Zend_Mail::setDefaultTransport($this->_transport);
    74                 }
    75                 }
    75             }
    76             }
    76 
    77 
    77             $this->_setDefaults('from');
    78             $this->_setDefaults('from');
    79         }
    80         }
    80 
    81 
    81         return $this->_transport;
    82         return $this->_transport;
    82     }
    83     }
    83 
    84 
    84     protected function _setDefaults($type) {
    85     protected function _setDefaults($type)
       
    86     {
    85         $key = strtolower('default' . $type);
    87         $key = strtolower('default' . $type);
    86         $options = $this->getOptions();
    88         $options = $this->getOptions();
    87 
    89 
    88         if(isset($options[$key]['email']) &&
    90         if (isset($options[$key]['email'])
    89            !is_numeric($options[$key]['email']))
    91             && !is_numeric($options[$key]['email'])
    90         {
    92         ) {
    91             $method = array('Zend_Mail', 'setDefault' . ucfirst($type));
    93             $method = array('Zend_Mail', 'setDefault' . ucfirst($type));
    92             if(isset($options[$key]['name']) &&
    94             if (isset($options[$key]['name'])
    93                !is_numeric($options[$key]['name']))
    95                 && !is_numeric(
    94             {
    96                     $options[$key]['name']
    95                 call_user_func($method, $options[$key]['email'],
    97                 )
    96                                         $options[$key]['name']);
    98             ) {
       
    99                 call_user_func(
       
   100                     $method, $options[$key]['email'], $options[$key]['name']
       
   101                 );
    97             } else {
   102             } else {
    98                 call_user_func($method, $options[$key]['email']);
   103                 call_user_func($method, $options[$key]['email']);
    99             }
   104             }
   100         }
   105         }
   101     }
   106     }
   102 
   107 
   103     protected function _setupTransport($options)
   108     protected function _setupTransport($options)
   104     {
   109     {
   105         if(!isset($options['type'])) {
   110         if (!isset($options['type'])) {
   106             $options['type'] = 'sendmail';
   111             $options['type'] = 'sendmail';
   107         }
   112         }
   108         
   113 
   109         $transportName = $options['type'];
   114         $transportName = $options['type'];
   110         if(!Zend_Loader_Autoloader::autoload($transportName))
   115         if (!Zend_Loader_Autoloader::autoload($transportName)) {
   111         {
       
   112             $transportName = ucfirst(strtolower($transportName));
   116             $transportName = ucfirst(strtolower($transportName));
   113 
   117 
   114             if(!Zend_Loader_Autoloader::autoload($transportName))
   118             if (!Zend_Loader_Autoloader::autoload($transportName)) {
   115             {
       
   116                 $transportName = 'Zend_Mail_Transport_' . $transportName;
   119                 $transportName = 'Zend_Mail_Transport_' . $transportName;
   117                 if(!Zend_Loader_Autoloader::autoload($transportName)) {
   120                 if (!Zend_Loader_Autoloader::autoload($transportName)) {
   118                     throw new Zend_Application_Resource_Exception(
   121                     throw new Zend_Application_Resource_Exception(
   119                         "Specified Mail Transport '{$transportName}'"
   122                         "Specified Mail Transport '{$transportName}'"
   120                         . 'could not be found'
   123                         . 'could not be found'
   121                     );
   124                     );
   122                 }
   125                 }
   126         unset($options['type']);
   129         unset($options['type']);
   127         unset($options['register']); //@see ZF-11022
   130         unset($options['register']); //@see ZF-11022
   128 
   131 
   129         switch($transportName) {
   132         switch($transportName) {
   130             case 'Zend_Mail_Transport_Smtp':
   133             case 'Zend_Mail_Transport_Smtp':
   131                 if(!isset($options['host'])) {
   134                 if (!isset($options['host'])) {
   132                     throw new Zend_Application_Resource_Exception(
   135                     throw new Zend_Application_Resource_Exception(
   133                         'A host is necessary for smtp transport,'
   136                         'A host is necessary for smtp transport,'
   134                         .' but none was given');
   137                         . ' but none was given'
       
   138                     );
   135                 }
   139                 }
   136 
   140 
   137                 $transport = new $transportName($options['host'], $options);
   141                 $transport = new $transportName($options['host'], $options);
   138                 break;
   142                 break;
   139             case 'Zend_Mail_Transport_Sendmail':
   143             case 'Zend_Mail_Transport_Sendmail':