source: trunk/library/Zend/Mail/Transport/Sendmail.php @ 5146

Revision 5146, 6.4 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus. Library: adicionando arquivos.

Line 
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: Sendmail.php 23484 2010-12-10 03:57:59Z mjh_ca $
21 */
22
23
24/**
25 * @see Zend_Mail_Transport_Abstract
26 */
27require_once 'Zend/Mail/Transport/Abstract.php';
28
29
30/**
31 * Class for sending eMails via the PHP internal mail() function
32 *
33 * @category   Zend
34 * @package    Zend_Mail
35 * @subpackage Transport
36 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license    http://framework.zend.com/license/new-bsd     New BSD License
38 */
39class Zend_Mail_Transport_Sendmail extends Zend_Mail_Transport_Abstract
40{
41    /**
42     * Subject
43     * @var string
44     * @access public
45     */
46    public $subject = null;
47
48
49    /**
50     * Config options for sendmail parameters
51     *
52     * @var string
53     */
54    public $parameters;
55
56    /**
57     * EOL character string
58     * @var string
59     * @access public
60     */
61    public $EOL = PHP_EOL;
62
63    /**
64     * error information
65     * @var string
66     */
67    protected $_errstr;
68
69    /**
70     * Constructor.
71     *
72     * @param  string|array|Zend_Config $parameters OPTIONAL (Default: null)
73     * @return void
74     */
75    public function __construct($parameters = null)
76    {
77        if ($parameters instanceof Zend_Config) {
78            $parameters = $parameters->toArray();
79        }
80
81        if (is_array($parameters)) {
82            $parameters = implode(' ', $parameters);
83        }
84
85        $this->parameters = $parameters;
86    }
87
88
89    /**
90     * Send mail using PHP native mail()
91     *
92     * @access public
93     * @return void
94     * @throws Zend_Mail_Transport_Exception if parameters is set
95     *         but not a string
96     * @throws Zend_Mail_Transport_Exception on mail() failure
97     */
98    public function _sendMail()
99    {
100        if ($this->parameters === null) {
101            set_error_handler(array($this, '_handleMailErrors'));
102            $result = mail(
103                $this->recipients,
104                $this->_mail->getSubject(),
105                $this->body,
106                $this->header);
107            restore_error_handler();
108        } else {
109            if(!is_string($this->parameters)) {
110                /**
111                 * @see Zend_Mail_Transport_Exception
112                 *
113                 * Exception is thrown here because
114                 * $parameters is a public property
115                 */
116                require_once 'Zend/Mail/Transport/Exception.php';
117                throw new Zend_Mail_Transport_Exception(
118                    'Parameters were set but are not a string'
119                );
120            }
121
122            set_error_handler(array($this, '_handleMailErrors'));
123            $result = mail(
124                $this->recipients,
125                $this->_mail->getSubject(),
126                $this->body,
127                $this->header,
128                $this->parameters);
129            restore_error_handler();
130        }
131
132        if ($this->_errstr !== null || !$result) {
133            /**
134             * @see Zend_Mail_Transport_Exception
135             */
136            require_once 'Zend/Mail/Transport/Exception.php';
137            throw new Zend_Mail_Transport_Exception('Unable to send mail. ' . $this->_errstr);
138        }
139    }
140
141
142    /**
143     * Format and fix headers
144     *
145     * mail() uses its $to and $subject arguments to set the To: and Subject:
146     * headers, respectively. This method strips those out as a sanity check to
147     * prevent duplicate header entries.
148     *
149     * @access  protected
150     * @param   array $headers
151     * @return  void
152     * @throws  Zend_Mail_Transport_Exception
153     */
154    protected function _prepareHeaders($headers)
155    {
156        if (!$this->_mail) {
157            /**
158             * @see Zend_Mail_Transport_Exception
159             */
160            require_once 'Zend/Mail/Transport/Exception.php';
161            throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
162        }
163
164        // mail() uses its $to parameter to set the To: header, and the $subject
165        // parameter to set the Subject: header. We need to strip them out.
166        if (0 === strpos(PHP_OS, 'WIN')) {
167            // If the current recipients list is empty, throw an error
168            if (empty($this->recipients)) {
169                /**
170                 * @see Zend_Mail_Transport_Exception
171                 */
172                require_once 'Zend/Mail/Transport/Exception.php';
173                throw new Zend_Mail_Transport_Exception('Missing To addresses');
174            }
175        } else {
176            // All others, simply grab the recipients and unset the To: header
177            if (!isset($headers['To'])) {
178                /**
179                 * @see Zend_Mail_Transport_Exception
180                 */
181                require_once 'Zend/Mail/Transport/Exception.php';
182                throw new Zend_Mail_Transport_Exception('Missing To header');
183            }
184
185            unset($headers['To']['append']);
186            $this->recipients = implode(',', $headers['To']);
187        }
188
189        // Remove recipient header
190        unset($headers['To']);
191
192        // Remove subject header, if present
193        if (isset($headers['Subject'])) {
194            unset($headers['Subject']);
195        }
196
197        // Prepare headers
198        parent::_prepareHeaders($headers);
199
200        // Fix issue with empty blank line ontop when using Sendmail Trnasport
201        $this->header = rtrim($this->header);
202    }
203
204    /**
205     * Temporary error handler for PHP native mail().
206     *
207     * @param int    $errno
208     * @param string $errstr
209     * @param string $errfile
210     * @param string $errline
211     * @param array  $errcontext
212     * @return true
213     */
214    public function _handleMailErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
215    {
216        $this->_errstr = $errstr;
217        return true;
218    }
219
220}
Note: See TracBrowser for help on using the repository browser.