3 * SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class.
9 * Copyright (c) 2010, Chuck Hagenbuch
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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
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.
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/
48 * SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class.
51 * @version $Revision: 294747 $
53 require_once 'smtp.php';
55 class Mail_smtpraw extends Mail_smtp {
59 function Mail_smtpraw($params)
61 parent::Mail_smtp($params);
67 * Implements Mail::send() function using SMTP.
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
75 * @param array $headers IGNORED
77 * @param string $body The full text of the message body, including any
80 * @param string $from The sender email
82 * @return mixed Returns true on success, or a PEAR_Error
83 * containing a descriptive error message on
87 function send($recipients, $headers, $body, $from)
89 /* If we don't already have an SMTP object, create one. */
90 $result = &$this->getSMTPObject();
91 if (PEAR::isError($result)) {
95 if (!is_array($headers)) {
96 return PEAR::raiseError('$headers must be an array');
100 $this->_smtp->rset();
101 return PEAR::raiseError('No From: address has been provided',
102 PEAR_MAIL_SMTP_ERROR_FROM);
106 if (!empty($this->_extparams)) {
107 foreach ($this->_extparams as $key => $val) {
108 $params .= ' ' . $key . (is_null($val) ? '' : '=' . $val);
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)
120 $recipients = $this->parseRecipients($recipients);
121 if (is_a($recipients, 'PEAR_Error')) {
122 $this->_smtp->rset();
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)
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();
141 if (preg_match("/Ok: queued as (.*)/", $args, $queued)) {
142 $this->queued_as = $queued[1];
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();
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)
157 /* If persistent connections are disabled, destroy our SMTP object. */
158 if ($this->persist === false) {