fix image text
[pear] / Mail / smtpraw.php
1 <?php
2 /**
3  * SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class.
4  *
5  * PHP versions 4 and 5
6  *
7  * LICENSE:
8  *
9  * Copyright (c) 2010, Chuck Hagenbuch
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * o Redistributions of source code must retain the above copyright
17  *   notice, this list of conditions and the following disclaimer.
18  * o Redistributions in binary form must reproduce the above copyright
19  *   notice, this list of conditions and the following disclaimer in the
20  *   documentation and/or other materials provided with the distribution.
21  * o The names of the authors may not be used to endorse or promote
22  *   products derived from this software without specific prior written
23  *   permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category    HTTP
38  * @package     HTTP_Request
39  * @author      Jon Parise <jon@php.net> 
40  * @author      Chuck Hagenbuch <chuck@horde.org>
41  * @copyright   2010 Chuck Hagenbuch
42  * @license     http://opensource.org/licenses/bsd-license.php New BSD License
43  * @version     CVS: $Id: smtp.php 294747 2010-02-08 08:18:33Z clockwerx $
44  * @link        http://pear.php.net/package/Mail/
45  */
46  
47 /**
48  * SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class.
49  * @access public
50  * @package Mail
51  * @version $Revision: 294747 $
52  */
53 require_once 'smtp.php';
54
55 class Mail_smtpraw extends Mail_smtp {
56  
57     var $debug=1;
58     
59     function Mail_smtpraw($params)
60     {
61        parent::Mail_smtp($params);
62     }
63
64      
65
66     /**
67      * Implements Mail::send() function using SMTP.
68      *
69      * @param mixed $recipients Either a comma-seperated list of recipients
70      *              (RFC822 compliant), or an array of recipients,
71      *              each RFC822 valid. This may contain recipients not
72      *              specified in the headers, for Bcc:, resending
73      *              messages, etc.
74      *
75      * @param array $headers IGNORED
76      *
77      * @param string $body The full text of the message body, including any
78      *               MIME parts, etc.
79      *
80      *  @param string $from The sender email
81      *
82      * @return mixed Returns true on success, or a PEAR_Error
83      *               containing a descriptive error message on
84      *               failure.
85      * @access public
86      */
87     function send($recipients, $headers, $body, $from)
88     {
89         /* If we don't already have an SMTP object, create one. */
90         $result = &$this->getSMTPObject();
91         if (PEAR::isError($result)) {
92             return $result;
93         }
94
95         if (!is_array($headers)) {
96             return PEAR::raiseError('$headers must be an array');
97         }
98   
99         if (!isset($from)) {
100             $this->_smtp->rset();
101             return PEAR::raiseError('No From: address has been provided',
102                                     PEAR_MAIL_SMTP_ERROR_FROM);
103         }
104
105         $params = null;
106         if (!empty($this->_extparams)) {
107             foreach ($this->_extparams as $key => $val) {
108                 $params .= ' ' . $key . (is_null($val) ? '' : '=' . $val);
109             }
110         }
111         if (PEAR::isError($res = $this->_smtp->mailFrom($from, ltrim($params)))) {
112             list($code, $error) = $this->_error(
113                     "Failed to set sender: $from", $res, PEAR_MAIL_SMTP_ERROR_SENDER);
114             $this->_smtp->rset();
115             return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_SENDER,
116                     null,null,array('smtpcode' => $code)
117             );
118         }
119
120         $recipients = $this->parseRecipients($recipients);
121         if (is_a($recipients, 'PEAR_Error')) {
122             $this->_smtp->rset();
123             return $recipients;
124         }
125
126         foreach ($recipients as $recipient) {
127             $res = $this->_smtp->rcptTo($recipient);
128             if (is_a($res, 'PEAR_Error')) {
129                 list($code, $error) = $this->_error("Failed to add recipient: $recipient", $res);
130                 $this->_smtp->rset();
131                 return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_RECIPIENT,
132                     null,null,array('smtpcode' => $code)
133                 );
134             }
135         }
136
137         /* Send the message's headers and the body as SMTP data. */
138         $res = $this->_smtp->data(str_replace("\n", "\r\n", $body));
139         list(,$args) = $this->_smtp->getResponse();
140
141         if (preg_match("/Ok: queued as (.*)/", $args, $queued)) {
142                 $this->queued_as = $queued[1];
143         }
144
145         /* we need the greeting; from it we can extract the authorative name of the mail server we've really connected to.
146          * ideal if we're connecting to a round-robin of relay servers and need to track which exact one took the email */
147         $this->greeting = $this->_smtp->getGreeting();
148
149         if (is_a($res, 'PEAR_Error')) {
150             list($code,$error) = $this->_error('Failed to send data', $res);
151             $this->_smtp->rset();
152             return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_DATA,
153                 null,null,array('smtpcode' => $code)
154             );
155         }
156
157         /* If persistent connections are disabled, destroy our SMTP object. */
158         if ($this->persist === false) {
159             $this->disconnect();
160         }
161
162         return true;
163     }
164  
165 }