Mail.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2014 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. foreach($options as $key => $option) {
  54. $options[strtolower($key)] = $option;
  55. }
  56. $this->setOptions($options);
  57. if(isset($options['transport']) &&
  58. !is_numeric($options['transport']))
  59. {
  60. $this->_transport = $this->_setupTransport($options['transport']);
  61. if(!isset($options['transport']['register']) ||
  62. $options['transport']['register'] == '1' ||
  63. (isset($options['transport']['register']) &&
  64. !is_numeric($options['transport']['register']) &&
  65. (bool) $options['transport']['register'] == true))
  66. {
  67. Zend_Mail::setDefaultTransport($this->_transport);
  68. }
  69. }
  70. $this->_setDefaults('from');
  71. $this->_setDefaults('replyTo');
  72. }
  73. return $this->_transport;
  74. }
  75. protected function _setDefaults($type) {
  76. $key = strtolower('default' . $type);
  77. $options = $this->getOptions();
  78. if(isset($options[$key]['email']) &&
  79. !is_numeric($options[$key]['email']))
  80. {
  81. $method = array('Zend_Mail', 'setDefault' . ucfirst($type));
  82. if(isset($options[$key]['name']) &&
  83. !is_numeric($options[$key]['name']))
  84. {
  85. call_user_func($method, $options[$key]['email'],
  86. $options[$key]['name']);
  87. } else {
  88. call_user_func($method, $options[$key]['email']);
  89. }
  90. }
  91. }
  92. protected function _setupTransport($options)
  93. {
  94. if(!isset($options['type'])) {
  95. $options['type'] = 'sendmail';
  96. }
  97. $transportName = $options['type'];
  98. if(!Zend_Loader_Autoloader::autoload($transportName))
  99. {
  100. $transportName = ucfirst(strtolower($transportName));
  101. if(!Zend_Loader_Autoloader::autoload($transportName))
  102. {
  103. $transportName = 'Zend_Mail_Transport_' . $transportName;
  104. if(!Zend_Loader_Autoloader::autoload($transportName)) {
  105. throw new Zend_Application_Resource_Exception(
  106. "Specified Mail Transport '{$transportName}'"
  107. . 'could not be found'
  108. );
  109. }
  110. }
  111. }
  112. unset($options['type']);
  113. unset($options['register']); //@see ZF-11022
  114. switch($transportName) {
  115. case 'Zend_Mail_Transport_Smtp':
  116. if(!isset($options['host'])) {
  117. throw new Zend_Application_Resource_Exception(
  118. 'A host is necessary for smtp transport,'
  119. .' but none was given');
  120. }
  121. $transport = new $transportName($options['host'], $options);
  122. break;
  123. case 'Zend_Mail_Transport_Sendmail':
  124. default:
  125. $transport = new $transportName($options);
  126. break;
  127. }
  128. return $transport;
  129. }
  130. }