Mail.php 4.8 KB

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