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