Mail.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: Db.php 17687 2009-08-20 12:55:34Z thomas $
  21. */
  22. /**
  23. * @see Zend_Application_Resource_ResourceAbstract
  24. */
  25. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  26. /**
  27. * Resource for setting up Mail Transport and default From & ReplyTo addresses
  28. *
  29. * @uses Zend_Application_Resource_ResourceAbstract
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage Resource
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Application_Resource_Mail extends Zend_Application_Resource_ResourceAbstract
  37. {
  38. /**
  39. * @var Zend_Mail_Transport_Abstract
  40. */
  41. protected $_transport;
  42. public function init() {
  43. return $this->getMail();
  44. }
  45. /**
  46. *
  47. * @return Zend_Mail_Transport_Abstract|null
  48. */
  49. public function getMail()
  50. {
  51. if (null === $this->_transport) {
  52. $options = $this->getOptions();
  53. if(isset($options['transport']) &&
  54. !is_numeric($options['transport']))
  55. {
  56. $this->_transport = $this->_setupTransport($options['transport']);
  57. if(!isset($options['transport']['register']) ||
  58. (isset($options['transport']['register']) &&
  59. !is_numeric($options['transport']['register']) &&
  60. (bool) $options['transport']['register'] == true))
  61. {
  62. Zend_Mail::setDefaultTransport($this->_transport);
  63. }
  64. }
  65. $this->_setDefaults('from');
  66. $this->_setDefaults('replyTo');
  67. }
  68. return $this->_transport;
  69. }
  70. protected function _setDefaults($type) {
  71. $key = strtolower('default' . $type);
  72. $options = $this->getOptions();
  73. if(isset($options[$key]['email']) &&
  74. !is_numeric($options[$key]['email']))
  75. {
  76. $method = array('Zend_Mail', 'setDefault' . ucfirst($type));
  77. if(isset($options[$key]['name']) &&
  78. !is_numeric($options[$key]['name']))
  79. {
  80. call_user_func($method, $options[$key]['email'],
  81. $options[$key]['name']);
  82. } else {
  83. call_user_func($method, $options[$key]['email']);
  84. }
  85. }
  86. }
  87. protected function _setupTransport($options)
  88. {
  89. $transportName = ucfirst(strtolower($options['type']));
  90. unset($options['type']);
  91. if(!Zend_Loader_Autoloader::autoload($transportName)) {
  92. $transportName = 'Zend_Mail_Transport_' . $transportName;
  93. if(!Zend_Loader_Autoloader::autoload($transportName)) {
  94. throw new Zend_Application_Resource_Exception(
  95. "Specified Mail Transport '{$transportName}'"
  96. . 'could not be found'
  97. );
  98. }
  99. }
  100. switch($transportName) {
  101. case 'Zend_Mail_Transport_Smtp':
  102. if(!isset($options['host'])) {
  103. throw new Zend_Application_Resource_Exception(
  104. 'A host is necessary for smtp transport,'
  105. .' but none was given');
  106. }
  107. $transport = new $transportName($options['host'], $options);
  108. break;
  109. case 'Zend_Mail_Transport_Sendmail':
  110. default:
  111. $transport = new $transportName($options);
  112. break;
  113. }
  114. return $transport;
  115. }
  116. }