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