File.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_Mail
  17. * @subpackage Transport
  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_Mail_Transport_Abstract
  24. */
  25. require_once 'Zend/Mail/Transport/Abstract.php';
  26. /**
  27. * File transport
  28. *
  29. * Class for saving outgoing emails in filesystem
  30. *
  31. * @category Zend
  32. * @package Zend_Mail
  33. * @subpackage Transport
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Mail_Transport_File extends Zend_Mail_Transport_Abstract
  38. {
  39. /**
  40. * Target directory for saving sent email messages
  41. *
  42. * @var string
  43. */
  44. protected $_path;
  45. /**
  46. * Callback function generating a file name
  47. *
  48. * @var string|array
  49. */
  50. protected $_callback;
  51. /**
  52. * Constructor
  53. *
  54. * @param array|Zend_Config $options OPTIONAL (Default: null)
  55. * @return void
  56. */
  57. public function __construct($options = null)
  58. {
  59. if ($options instanceof Zend_Config) {
  60. $options = $options->toArray();
  61. } elseif (!is_array($options)) {
  62. $options = array();
  63. }
  64. // Making sure we have some defaults to work with
  65. if (!isset($options['path'])) {
  66. $options['path'] = sys_get_temp_dir();
  67. }
  68. if (!isset($options['callback'])) {
  69. $options['callback'] = array($this, 'defaultCallback');
  70. }
  71. $this->setOptions($options);
  72. }
  73. /**
  74. * Sets options
  75. *
  76. * @param array $options
  77. * @return void
  78. */
  79. public function setOptions(array $options)
  80. {
  81. if (isset($options['path']) && is_dir($options['path'])) {
  82. $this->_path = $options['path'];
  83. }
  84. if (isset($options['callback']) && is_callable($options['callback'])) {
  85. $this->_callback = $options['callback'];
  86. }
  87. }
  88. /**
  89. * Saves e-mail message to a file
  90. *
  91. * @return void
  92. * @throws Zend_Mail_Transport_Exception on not writable target directory
  93. * @throws Zend_Mail_Transport_Exception on file_put_contents() failure
  94. */
  95. protected function _sendMail()
  96. {
  97. $file = $this->_path . DIRECTORY_SEPARATOR . call_user_func($this->_callback, $this);
  98. if (!is_writable(dirname($file))) {
  99. require_once 'Zend/Mail/Transport/Exception.php';
  100. throw new Zend_Mail_Transport_Exception(sprintf(
  101. 'Target directory "%s" does not exist or is not writable',
  102. dirname($file)
  103. ));
  104. }
  105. $email = $this->header . $this->EOL . $this->body;
  106. if (!file_put_contents($file, $email)) {
  107. require_once 'Zend/Mail/Transport/Exception.php';
  108. throw new Zend_Mail_Transport_Exception('Unable to send mail');
  109. }
  110. }
  111. /**
  112. * Default callback for generating filenames
  113. *
  114. * @param Zend_Mail_Transport_File File transport instance
  115. * @return string
  116. */
  117. public function defaultCallback($transport)
  118. {
  119. return 'ZendMail_' . $_SERVER['REQUEST_TIME'] . '_' . mt_rand() . '.tmp';
  120. }
  121. }