| 1 |
<?php |
| 2 |
/** |
| 3 |
* PEAR's Mail:: interface. |
| 4 |
* |
| 5 |
* PHP version 5 |
| 6 |
* |
| 7 |
* LICENSE: |
| 8 |
* |
| 9 |
* Copyright (c) 1997-2017, Chuck Hagenbuch & Richard Heyes |
| 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 |
* 1. Redistributions of source code must retain the above copyright |
| 17 |
* notice, this list of conditions and the following disclaimer. |
| 18 |
* |
| 19 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 20 |
* notice, this list of conditions and the following disclaimer in the |
| 21 |
* documentation and/or other materials provided with the distribution. |
| 22 |
* |
| 23 |
* 3. Neither the name of the copyright holder nor the names of its |
| 24 |
* contributors may be used to endorse or promote products derived from |
| 25 |
* this software without specific prior written permission. |
| 26 |
* |
| 27 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 |
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 |
* |
| 39 |
* @category Mail |
| 40 |
* @package Mail |
| 41 |
* @author Chuck Hagenbuch <[email protected]> |
| 42 |
* @copyright 1997-2017 Chuck Hagenbuch |
| 43 |
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
| 44 |
* @version CVS: $Id$ |
| 45 |
* @link http://pear.php.net/package/Mail/ |
| 46 |
*/ |
| 47 |
|
| 48 |
require_once 'PEAR.php'; |
| 49 |
|
| 50 |
/** |
| 51 |
* PEAR's Mail:: interface. Defines the interface for implementing |
| 52 |
* mailers under the PEAR hierarchy, and provides supporting functions |
| 53 |
* useful in multiple mailer backends. |
| 54 |
* |
| 55 |
* @version $Revision$ |
| 56 |
* @package Mail |
| 57 |
*/ |
| 58 |
class Mail |
| 59 |
{ |
| 60 |
/** |
| 61 |
* Line terminator used for separating header lines. |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
public $sep = "\r\n"; |
| 65 |
|
| 66 |
/** |
| 67 |
* Provides an interface for generating Mail:: objects of various |
| 68 |
* types |
| 69 |
* |
| 70 |
* @param string $driver The kind of Mail:: object to instantiate. |
| 71 |
* @param array $params The parameters to pass to the Mail:: object. |
| 72 |
* |
| 73 |
* @return object Mail a instance of the driver class or if fails a PEAR Error |
| 74 |
*/ |
| 75 |
public static function factory($driver, $params = array()) |
| 76 |
{ |
| 77 |
$driver = strtolower($driver); |
| 78 |
@include_once 'Mail/' . $driver . '.php'; |
| 79 |
$class = 'Mail_' . $driver; |
| 80 |
if (class_exists($class)) { |
| 81 |
$mailer = new $class($params); |
| 82 |
return $mailer; |
| 83 |
} else { |
| 84 |
return PEAR::raiseError('Unable to find class for driver ' . $driver); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Implements Mail::send() function using php's built-in mail() |
| 90 |
* command. |
| 91 |
* |
| 92 |
* @param mixed $recipients Either a comma-seperated list of recipients |
| 93 |
* (RFC822 compliant), or an array of recipients, |
| 94 |
* each RFC822 valid. This may contain recipients not |
| 95 |
* specified in the headers, for Bcc:, resending |
| 96 |
* messages, etc. |
| 97 |
* |
| 98 |
* @param array $headers The array of headers to send with the mail, in an |
| 99 |
* associative array, where the array key is the |
| 100 |
* header name (ie, 'Subject'), and the array value |
| 101 |
* is the header value (ie, 'test'). The header |
| 102 |
* produced from those values would be 'Subject: |
| 103 |
* test'. |
| 104 |
* |
| 105 |
* @param string $body The full text of the message body, including any |
| 106 |
* Mime parts, etc. |
| 107 |
* |
| 108 |
* @return mixed Returns true on success, or a PEAR_Error |
| 109 |
* containing a descriptive error message on |
| 110 |
* failure. |
| 111 |
* |
| 112 |
* @deprecated use Mail_mail::send instead |
| 113 |
*/ |
| 114 |
public function send($recipients, $headers, $body) |
| 115 |
{ |
| 116 |
if (!is_array($headers)) { |
| 117 |
return PEAR::raiseError('$headers must be an array'); |
| 118 |
} |
| 119 |
|
| 120 |
$result = $this->_sanitizeHeaders($headers); |
| 121 |
if (is_a($result, 'PEAR_Error')) { |
| 122 |
return $result; |
| 123 |
} |
| 124 |
|
| 125 |
// if we're passed an array of recipients, implode it. |
| 126 |
if (is_array($recipients)) { |
| 127 |
$recipients = implode(', ', $recipients); |
| 128 |
} |
| 129 |
|
| 130 |
// get the Subject out of the headers array so that we can |
| 131 |
// pass it as a seperate argument to mail(). |
| 132 |
$subject = ''; |
| 133 |
if (isset($headers['Subject'])) { |
| 134 |
$subject = $headers['Subject']; |
| 135 |
unset($headers['Subject']); |
| 136 |
} |
| 137 |
|
| 138 |
// flatten the headers out. |
| 139 |
list(, $text_headers) = Mail::prepareHeaders($headers); |
| 140 |
|
| 141 |
return mail($recipients, $subject, $body, $text_headers); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Sanitize an array of mail headers by removing any additional header |
| 146 |
* strings present in a legitimate header's value. The goal of this |
| 147 |
* filter is to prevent mail injection attacks. |
| 148 |
* |
| 149 |
* @param array $headers The associative array of headers to sanitize. |
| 150 |
*/ |
| 151 |
protected function _sanitizeHeaders(&$headers) |
| 152 |
{ |
| 153 |
foreach ($headers as $key => $value) { |
| 154 |
$headers[$key] = |
| 155 |
preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', |
| 156 |
'', $value); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Take an array of mail headers and return a string containing |
| 162 |
* text usable in sending a message. |
| 163 |
* |
| 164 |
* @param array $headers The array of headers to prepare, in an associative |
| 165 |
* array, where the array key is the header name (ie, |
| 166 |
* 'Subject'), and the array value is the header |
| 167 |
* value (ie, 'test'). The header produced from those |
| 168 |
* values would be 'Subject: test'. |
| 169 |
* |
| 170 |
* @return mixed Returns false if it encounters a bad address, |
| 171 |
* otherwise returns an array containing two |
| 172 |
* elements: Any From: address found in the headers, |
| 173 |
* and the plain text version of the headers. |
| 174 |
*/ |
| 175 |
protected function prepareHeaders($headers) |
| 176 |
{ |
| 177 |
$lines = array(); |
| 178 |
$from = null; |
| 179 |
|
| 180 |
foreach ($headers as $key => $value) { |
| 181 |
if (strcasecmp($key, 'From') === 0) { |
| 182 |
include_once 'Mail/RFC822.php'; |
| 183 |
$parser = new Mail_RFC822(); |
| 184 |
$addresses = $parser->parseAddressList($value, 'localhost', false); |
| 185 |
if (is_a($addresses, 'PEAR_Error')) { |
| 186 |
return $addresses; |
| 187 |
} |
| 188 |
|
| 189 |
$from = $addresses[0]->mailbox . '@' . $addresses[0]->host; |
| 190 |
|
| 191 |
// Reject envelope From: addresses with spaces. |
| 192 |
if (strstr($from, ' ')) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
$lines[] = $key . ': ' . $value; |
| 197 |
} elseif (strcasecmp($key, 'Received') === 0) { |
| 198 |
$received = array(); |
| 199 |
if (is_array($value)) { |
| 200 |
foreach ($value as $line) { |
| 201 |
$received[] = $key . ': ' . $line; |
| 202 |
} |
| 203 |
} |
| 204 |
else { |
| 205 |
$received[] = $key . ': ' . $value; |
| 206 |
} |
| 207 |
// Put Received: headers at the top. Spam detectors often |
| 208 |
// flag messages with Received: headers after the Subject: |
| 209 |
// as spam. |
| 210 |
$lines = array_merge($received, $lines); |
| 211 |
} else { |
| 212 |
// If $value is an array (i.e., a list of addresses), convert |
| 213 |
// it to a comma-delimited string of its elements (addresses). |
| 214 |
if (is_array($value)) { |
| 215 |
$value = implode(', ', $value); |
| 216 |
} |
| 217 |
$lines[] = $key . ': ' . $value; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return array($from, join($this->sep, $lines)); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Take a set of recipients and parse them, returning an array of |
| 226 |
* bare addresses (forward paths) that can be passed to sendmail |
| 227 |
* or an smtp server with the rcpt to: command. |
| 228 |
* |
| 229 |
* @param mixed Either a comma-seperated list of recipients |
| 230 |
* (RFC822 compliant), or an array of recipients, |
| 231 |
* each RFC822 valid. |
| 232 |
* |
| 233 |
* @return mixed An array of forward paths (bare addresses) or a PEAR_Error |
| 234 |
* object if the address list could not be parsed. |
| 235 |
*/ |
| 236 |
protected function parseRecipients($recipients) |
| 237 |
{ |
| 238 |
include_once 'Mail/RFC822.php'; |
| 239 |
|
| 240 |
// if we're passed an array, assume addresses are valid and |
| 241 |
// implode them before parsing. |
| 242 |
if (is_array($recipients)) { |
| 243 |
$recipients = implode(', ', $recipients); |
| 244 |
} |
| 245 |
|
| 246 |
// Parse recipients, leaving out all personal info. This is |
| 247 |
// for smtp recipients, etc. All relevant personal information |
| 248 |
// should already be in the headers. |
| 249 |
$Mail_RFC822 = new Mail_RFC822(); |
| 250 |
$addresses = $Mail_RFC822->parseAddressList($recipients, 'localhost', false); |
| 251 |
|
| 252 |
// If parseAddressList() returned a PEAR_Error object, just return it. |
| 253 |
if (is_a($addresses, 'PEAR_Error')) { |
| 254 |
return $addresses; |
| 255 |
} |
| 256 |
|
| 257 |
$recipients = array(); |
| 258 |
if (is_array($addresses)) { |
| 259 |
foreach ($addresses as $ob) { |
| 260 |
$recipients[] = $ob->mailbox . '@' . $ob->host; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
return $recipients; |
| 265 |
} |
| 266 |
|
| 267 |
} |