Mail.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 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. (isset($options['transport']['register']) &&
  63. !is_numeric($options['transport']['register']) &&
  64. (bool) $options['transport']['register'] == true))
  65. {
  66. Zend_Mail::setDefaultTransport($this->_transport);
  67. }
  68. }
  69. $this->_setDefaults('from');
  70. $this->_setDefaults('replyTo');
  71. }
  72. return $this->_transport;
  73. }
  74. protected function _setDefaults($type) {
  75. $key = strtolower('default' . $type);
  76. $options = $this->getOptions();
  77. if(isset($options[$key]['email']) &&
  78. !is_numeric($options[$key]['email']))
  79. {
  80. $method = array('Zend_Mail', 'setDefault' . ucfirst($type));
  81. if(isset($options[$key]['name']) &&
  82. !is_numeric($options[$key]['name']))
  83. {
  84. call_user_func($method, $options[$key]['email'],
  85. $options[$key]['name']);
  86. } else {
  87. call_user_func($method, $options[$key]['email']);
  88. }
  89. }
  90. }
  91. protected function _setupTransport($options)
  92. {
  93. $transportName = ucfirst(strtolower($options['type']));
  94. unset($options['type']);
  95. if(!Zend_Loader_Autoloader::autoload($transportName)) {
  96. $transportName = 'Zend_Mail_Transport_' . $transportName;
  97. if(!Zend_Loader_Autoloader::autoload($transportName)) {
  98. throw new Zend_Application_Resource_Exception(
  99. "Specified Mail Transport '{$transportName}'"
  100. . 'could not be found'
  101. );
  102. }
  103. }
  104. switch($transportName) {
  105. case 'Zend_Mail_Transport_Smtp':
  106. if(!isset($options['host'])) {
  107. throw new Zend_Application_Resource_Exception(
  108. 'A host is necessary for smtp transport,'
  109. .' but none was given');
  110. }
  111. $transport = new $transportName($options['host'], $options);
  112. break;
  113. case 'Zend_Mail_Transport_Sendmail':
  114. default:
  115. $transport = new $transportName($options);
  116. break;
  117. }
  118. return $transport;
  119. }
  120. }