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 /**
0025  * @see Zend_Mail_Transport_Abstract
0026  */
0027 // require_once 'Zend/Mail/Transport/Abstract.php';
0028 
0029 
0030 /**
0031  * Class for sending eMails via the PHP internal mail() function
0032  *
0033  * @category   Zend
0034  * @package    Zend_Mail
0035  * @subpackage Transport
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Mail_Transport_Sendmail extends Zend_Mail_Transport_Abstract
0040 {
0041     /**
0042      * Subject
0043      * @var string
0044      * @access public
0045      */
0046     public $subject = null;
0047 
0048 
0049     /**
0050      * Config options for sendmail parameters
0051      *
0052      * @var string
0053      */
0054     public $parameters;
0055 
0056     /**
0057      * EOL character string
0058      * @var string
0059      * @access public
0060      */
0061     public $EOL = PHP_EOL;
0062 
0063     /**
0064      * error information
0065      * @var string
0066      */
0067     protected $_errstr;
0068 
0069     /**
0070      * Constructor.
0071      *
0072      * @param  string|array|Zend_Config $parameters OPTIONAL (Default: null)
0073      * @return void
0074      */
0075     public function __construct($parameters = null)
0076     {
0077         if ($parameters instanceof Zend_Config) {
0078             $parameters = $parameters->toArray();
0079         }
0080 
0081         if (is_array($parameters)) {
0082             $parameters = implode(' ', $parameters);
0083         }
0084 
0085         $this->parameters = $parameters;
0086     }
0087 
0088 
0089     /**
0090      * Send mail using PHP native mail()
0091      *
0092      * @access public
0093      * @return void
0094      * @throws Zend_Mail_Transport_Exception if parameters is set
0095      *         but not a string
0096      * @throws Zend_Mail_Transport_Exception on mail() failure
0097      */
0098     public function _sendMail()
0099     {
0100         if ($this->parameters === null) {
0101             set_error_handler(array($this, '_handleMailErrors'));
0102             $result = mail(
0103                 $this->recipients,
0104                 $this->_mail->getSubject(),
0105                 $this->body,
0106                 $this->header);
0107             restore_error_handler();
0108         } else {
0109             if(!is_string($this->parameters)) {
0110                 /**
0111                  * @see Zend_Mail_Transport_Exception
0112                  *
0113                  * Exception is thrown here because
0114                  * $parameters is a public property
0115                  */
0116                 // require_once 'Zend/Mail/Transport/Exception.php';
0117                 throw new Zend_Mail_Transport_Exception(
0118                     'Parameters were set but are not a string'
0119                 );
0120             }
0121 
0122             set_error_handler(array($this, '_handleMailErrors'));
0123             $result = mail(
0124                 $this->recipients,
0125                 $this->_mail->getSubject(),
0126                 $this->body,
0127                 $this->header,
0128                 $this->parameters);
0129             restore_error_handler();
0130         }
0131 
0132         if ($this->_errstr !== null || !$result) {
0133             /**
0134              * @see Zend_Mail_Transport_Exception
0135              */
0136             // require_once 'Zend/Mail/Transport/Exception.php';
0137             throw new Zend_Mail_Transport_Exception('Unable to send mail. ' . $this->_errstr);
0138         }
0139     }
0140 
0141 
0142     /**
0143      * Format and fix headers
0144      *
0145      * mail() uses its $to and $subject arguments to set the To: and Subject:
0146      * headers, respectively. This method strips those out as a sanity check to
0147      * prevent duplicate header entries.
0148      *
0149      * @access  protected
0150      * @param   array $headers
0151      * @return  void
0152      * @throws  Zend_Mail_Transport_Exception
0153      */
0154     protected function _prepareHeaders($headers)
0155     {
0156         if (!$this->_mail) {
0157             /**
0158              * @see Zend_Mail_Transport_Exception
0159              */
0160             // require_once 'Zend/Mail/Transport/Exception.php';
0161             throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
0162         }
0163 
0164         // mail() uses its $to parameter to set the To: header, and the $subject
0165         // parameter to set the Subject: header. We need to strip them out.
0166         if (0 === strpos(PHP_OS, 'WIN')) {
0167             // If the current recipients list is empty, throw an error
0168             if (empty($this->recipients)) {
0169                 /**
0170                  * @see Zend_Mail_Transport_Exception
0171                  */
0172                 // require_once 'Zend/Mail/Transport/Exception.php';
0173                 throw new Zend_Mail_Transport_Exception('Missing To addresses');
0174             }
0175         } else {
0176             // All others, simply grab the recipients and unset the To: header
0177             if (!isset($headers['To'])) {
0178                 /**
0179                  * @see Zend_Mail_Transport_Exception
0180                  */
0181                 // require_once 'Zend/Mail/Transport/Exception.php';
0182                 throw new Zend_Mail_Transport_Exception('Missing To header');
0183             }
0184 
0185             unset($headers['To']['append']);
0186             $this->recipients = implode(',', $headers['To']);
0187         }
0188 
0189         // Remove recipient header
0190         unset($headers['To']);
0191 
0192         // Remove subject header, if present
0193         if (isset($headers['Subject'])) {
0194             unset($headers['Subject']);
0195         }
0196 
0197         // Prepare headers
0198         parent::_prepareHeaders($headers);
0199 
0200         // Fix issue with empty blank line ontop when using Sendmail Trnasport
0201         $this->header = rtrim($this->header);
0202     }
0203 
0204     /**
0205      * Temporary error handler for PHP native mail().
0206      *
0207      * @param int    $errno
0208      * @param string $errstr
0209      * @param string $errfile
0210      * @param string $errline
0211      * @param array  $errcontext
0212      * @return true
0213      */
0214     public function _handleMailErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
0215     {
0216         $this->_errstr = $errstr;
0217         return true;
0218     }
0219 
0220 }