File indexing completed on 2024-12-22 05:36:51

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Mail
0017  * @subpackage Transport
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Mail_Transport_Abstract
0025  */
0026 // require_once 'Zend/Mail/Transport/Abstract.php';
0027 
0028 
0029 /**
0030  * File transport
0031  *
0032  * Class for saving outgoing emails in filesystem
0033  *
0034  * @category   Zend
0035  * @package    Zend_Mail
0036  * @subpackage Transport
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_Mail_Transport_File extends Zend_Mail_Transport_Abstract
0041 {
0042     /**
0043      * Target directory for saving sent email messages
0044      *
0045      * @var string
0046      */
0047     protected $_path;
0048 
0049     /**
0050      * Callback function generating a file name
0051      *
0052      * @var string|array
0053      */
0054     protected $_callback;
0055 
0056     /**
0057      * Constructor
0058      *
0059      * @param  array|Zend_Config $options OPTIONAL (Default: null)
0060      * @return void
0061      */
0062     public function __construct($options = null)
0063     {
0064         if ($options instanceof Zend_Config) {
0065             $options = $options->toArray();
0066         } elseif (!is_array($options)) {
0067             $options = array();
0068         }
0069 
0070         // Making sure we have some defaults to work with
0071         if (!isset($options['path'])) {
0072             $options['path'] = sys_get_temp_dir();
0073         }
0074         if (!isset($options['callback'])) {
0075             $options['callback'] = array($this, 'defaultCallback');
0076         }
0077 
0078         $this->setOptions($options);
0079     }
0080 
0081     /**
0082      * Sets options
0083      *
0084      * @param  array $options
0085      * @return void
0086      */
0087     public function setOptions(array $options)
0088     {
0089         if (isset($options['path']) && is_dir($options['path'])) {
0090             $this->_path = $options['path'];
0091         }
0092         if (isset($options['callback']) && is_callable($options['callback'])) {
0093             $this->_callback = $options['callback'];
0094         }
0095     }
0096 
0097     /**
0098      * Saves e-mail message to a file
0099      *
0100      * @return void
0101      * @throws Zend_Mail_Transport_Exception on not writable target directory
0102      * @throws Zend_Mail_Transport_Exception on file_put_contents() failure
0103      */
0104     protected function _sendMail()
0105     {
0106         $file = $this->_path . DIRECTORY_SEPARATOR . call_user_func($this->_callback, $this);
0107 
0108         if (!is_writable(dirname($file))) {
0109             // require_once 'Zend/Mail/Transport/Exception.php';
0110             throw new Zend_Mail_Transport_Exception(sprintf(
0111                 'Target directory "%s" does not exist or is not writable',
0112                 dirname($file)
0113             ));
0114         }
0115 
0116         $email = $this->header . $this->EOL . $this->body;
0117 
0118         if (!file_put_contents($file, $email)) {
0119             // require_once 'Zend/Mail/Transport/Exception.php';
0120             throw new Zend_Mail_Transport_Exception('Unable to send mail');
0121         }
0122     }
0123 
0124     /**
0125      * Default callback for generating filenames
0126      *
0127      * @param Zend_Mail_Transport_File File transport instance
0128      * @return string
0129      */
0130     public function defaultCallback($transport)
0131     {
0132         return 'ZendMail_' . $_SERVER['REQUEST_TIME'] . '_' . mt_rand() . '.tmp';
0133     }
0134 }