| 1 |
<?php |
| 2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
| 3 |
|
| 4 |
/** |
| 5 |
* SMTP MX |
| 6 |
* |
| 7 |
* SMTP MX implementation of the PEAR Mail interface. Requires the Net_SMTP class. |
| 8 |
* |
| 9 |
* PHP version 5 |
| 10 |
* |
| 11 |
* LICENSE: |
| 12 |
* |
| 13 |
* Copyright (c) 2010-2017 gERD Schaufelberger |
| 14 |
* All rights reserved. |
| 15 |
* |
| 16 |
* Redistribution and use in source and binary forms, with or without |
| 17 |
* modification, are permitted provided that the following conditions |
| 18 |
* are met: |
| 19 |
* |
| 20 |
* 1. Redistributions of source code must retain the above copyright |
| 21 |
* notice, this list of conditions and the following disclaimer. |
| 22 |
* |
| 23 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 24 |
* notice, this list of conditions and the following disclaimer in the |
| 25 |
* documentation and/or other materials provided with the distribution. |
| 26 |
* |
| 27 |
* 3. Neither the name of the copyright holder nor the names of its |
| 28 |
* contributors may be used to endorse or promote products derived from |
| 29 |
* this software without specific prior written permission. |
| 30 |
* |
| 31 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 34 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 35 |
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 36 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 37 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 38 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 39 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 40 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 41 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 |
* |
| 43 |
* @category Mail |
| 44 |
* @package Mail_smtpmx |
| 45 |
* @author gERD Schaufelberger <[email protected]> |
| 46 |
* @copyright 2010-2017 gERD Schaufelberger |
| 47 |
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
| 48 |
* @version CVS: $Id$ |
| 49 |
* @link http://pear.php.net/package/Mail/ |
| 50 |
*/ |
| 51 |
|
| 52 |
require_once 'Net/SMTP.php'; |
| 53 |
|
| 54 |
/** |
| 55 |
* SMTP MX implementation of the PEAR Mail interface. Requires the Net_SMTP class. |
| 56 |
* |
| 57 |
* |
| 58 |
* @access public |
| 59 |
* @author gERD Schaufelberger <[email protected]> |
| 60 |
* @package Mail |
| 61 |
* @version $Revision$ |
| 62 |
*/ |
| 63 |
class Mail_smtpmx extends Mail { |
| 64 |
|
| 65 |
/** |
| 66 |
* SMTP connection object. |
| 67 |
* |
| 68 |
* @var object |
| 69 |
* @access private |
| 70 |
*/ |
| 71 |
var $_smtp = null; |
| 72 |
|
| 73 |
/** |
| 74 |
* The port the SMTP server is on. |
| 75 |
* @var integer |
| 76 |
* @see getservicebyname() |
| 77 |
*/ |
| 78 |
var $port = 25; |
| 79 |
|
| 80 |
/** |
| 81 |
* Hostname or domain that will be sent to the remote SMTP server in the |
| 82 |
* HELO / EHLO message. |
| 83 |
* |
| 84 |
* @var string |
| 85 |
* @see posix_uname() |
| 86 |
*/ |
| 87 |
var $mailname = 'localhost'; |
| 88 |
|
| 89 |
/** |
| 90 |
* SMTP connection timeout value. NULL indicates no timeout. |
| 91 |
* |
| 92 |
* @var integer |
| 93 |
*/ |
| 94 |
var $timeout = 10; |
| 95 |
|
| 96 |
/** |
| 97 |
* use either PEAR:Net_DNS or getmxrr |
| 98 |
* |
| 99 |
* @var boolean |
| 100 |
*/ |
| 101 |
var $withNetDns = true; |
| 102 |
|
| 103 |
/** |
| 104 |
* PEAR:Net_DNS_Resolver |
| 105 |
* |
| 106 |
* @var object |
| 107 |
*/ |
| 108 |
var $resolver; |
| 109 |
|
| 110 |
/** |
| 111 |
* Whether to use VERP or not. If not a boolean, the string value |
| 112 |
* will be used as the VERP separators. |
| 113 |
* |
| 114 |
* @var mixed boolean or string |
| 115 |
*/ |
| 116 |
var $verp = false; |
| 117 |
|
| 118 |
/** |
| 119 |
* Whether to use VRFY or not. |
| 120 |
* |
| 121 |
* @var boolean $vrfy |
| 122 |
*/ |
| 123 |
var $vrfy = false; |
| 124 |
|
| 125 |
/** |
| 126 |
* Switch to test mode - don't send emails for real |
| 127 |
* |
| 128 |
* @var boolean $test |
| 129 |
*/ |
| 130 |
var $test = false; |
| 131 |
|
| 132 |
/** |
| 133 |
* Turn on Net_SMTP debugging? |
| 134 |
* |
| 135 |
* @var boolean $debug |
| 136 |
*/ |
| 137 |
var $debug = false; |
| 138 |
|
| 139 |
/** |
| 140 |
* Set debug_handler on Net_SMTP |
| 141 |
* |
| 142 |
* @var string $debug_handler |
| 143 |
*/ |
| 144 |
var $debug_handler = null; |
| 145 |
|
| 146 |
/** |
| 147 |
* internal error codes |
| 148 |
* |
| 149 |
* translate internal error identifier to PEAR-Error codes and human |
| 150 |
* readable messages. |
| 151 |
* |
| 152 |
* @var array $errorCode |
| 153 |
* @todo as I need unique error-codes to identify what exactly went wrond |
| 154 |
* I did not use intergers as it should be. Instead I added a "namespace" |
| 155 |
* for each code. This avoids conflicts with error codes from different |
| 156 |
* classes. How can I use unique error codes and stay conform with PEAR? |
| 157 |
*/ |
| 158 |
var $errorCode = array( |
| 159 |
'not_connected' => array( |
| 160 |
'code' => 1, |
| 161 |
'msg' => 'Could not connect to any mail server ({HOST}) at port {PORT} to send mail to {RCPT}.' |
| 162 |
), |
| 163 |
'failed_vrfy_rcpt' => array( |
| 164 |
'code' => 2, |
| 165 |
'msg' => 'Recipient "{RCPT}" could not be veryfied.' |
| 166 |
), |
| 167 |
'failed_set_from' => array( |
| 168 |
'code' => 3, |
| 169 |
'msg' => 'Failed to set sender: {FROM}.' |
| 170 |
), |
| 171 |
'failed_set_rcpt' => array( |
| 172 |
'code' => 4, |
| 173 |
'msg' => 'Failed to set recipient: {RCPT}.' |
| 174 |
), |
| 175 |
'failed_send_data' => array( |
| 176 |
'code' => 5, |
| 177 |
'msg' => 'Failed to send mail to: {RCPT}.' |
| 178 |
), |
| 179 |
'no_from' => array( |
| 180 |
'code' => 5, |
| 181 |
'msg' => 'No from address has be provided.' |
| 182 |
), |
| 183 |
'send_data' => array( |
| 184 |
'code' => 7, |
| 185 |
'msg' => 'Failed to create Net_SMTP object.' |
| 186 |
), |
| 187 |
'no_mx' => array( |
| 188 |
'code' => 8, |
| 189 |
'msg' => 'No MX-record for {RCPT} found.' |
| 190 |
), |
| 191 |
'no_resolver' => array( |
| 192 |
'code' => 9, |
| 193 |
'msg' => 'Could not start resolver! Install PEAR:Net_DNS or switch off "netdns"' |
| 194 |
), |
| 195 |
'failed_rset' => array( |
| 196 |
'code' => 10, |
| 197 |
'msg' => 'RSET command failed, SMTP-connection corrupt.' |
| 198 |
), |
| 199 |
); |
| 200 |
|
| 201 |
/** |
| 202 |
* Constructor. |
| 203 |
* |
| 204 |
* Instantiates a new Mail_smtp:: object based on the parameters |
| 205 |
* passed in. It looks for the following parameters: |
| 206 |
* mailname The name of the local mail system (a valid hostname which matches the reverse lookup) |
| 207 |
* port smtp-port - the default comes from getservicebyname() and should work fine |
| 208 |
* timeout The SMTP connection timeout. Defaults to 30 seconds. |
| 209 |
* vrfy Whether to use VRFY or not. Defaults to false. |
| 210 |
* verp Whether to use VERP or not. Defaults to false. |
| 211 |
* test Activate test mode? Defaults to false. |
| 212 |
* debug Activate SMTP and Net_DNS debug mode? Defaults to false. |
| 213 |
* debug_handler Set SMTP debug handler function. Defaults to null. |
| 214 |
* netdns whether to use PEAR:Net_DNS or the PHP build in function getmxrr, default is true |
| 215 |
* |
| 216 |
* If a parameter is present in the $params array, it replaces the |
| 217 |
* default. |
| 218 |
* |
| 219 |
* @access public |
| 220 |
* @param array Hash containing any parameters different from the |
| 221 |
* defaults. |
| 222 |
* @see _Mail_smtpmx() |
| 223 |
*/ |
| 224 |
function __construct($params) |
| 225 |
{ |
| 226 |
if (isset($params['mailname'])) { |
| 227 |
$this->mailname = $params['mailname']; |
| 228 |
} else { |
| 229 |
// try to find a valid mailname |
| 230 |
if (function_exists('posix_uname')) { |
| 231 |
$uname = posix_uname(); |
| 232 |
$this->mailname = $uname['nodename']; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// port number |
| 237 |
if (isset($params['port'])) { |
| 238 |
$this->port = $params['port']; |
| 239 |
} else { |
| 240 |
$this->port = getservbyname('smtp', 'tcp'); |
| 241 |
} |
| 242 |
|
| 243 |
if (isset($params['timeout'])) $this->timeout = $params['timeout']; |
| 244 |
if (isset($params['vrfy'])) $this->vrfy = (bool)$params['vrfy']; |
| 245 |
if (isset($params['verp'])) $this->verp = $params['verp']; |
| 246 |
if (isset($params['test'])) $this->test = (bool)$params['test']; |
| 247 |
if (isset($params['peardebug'])) $this->debug = (bool)$params['peardebug']; |
| 248 |
if (isset($params['debug'])) $this->debug = (bool)$params['debug']; |
| 249 |
if (isset($params['debug_handler'])) $this->debug_handler = $params['debug_handler']; |
| 250 |
if (isset($params['netdns'])) $this->withNetDns = $params['netdns']; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Constructor wrapper for PHP4 |
| 255 |
* |
| 256 |
* @access public |
| 257 |
* @param array Hash containing any parameters different from the defaults |
| 258 |
* @see __construct() |
| 259 |
*/ |
| 260 |
function Mail_smtpmx($params) |
| 261 |
{ |
| 262 |
$this->__construct($params); |
| 263 |
register_shutdown_function(array(&$this, '__destruct')); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Destructor implementation to ensure that we disconnect from any |
| 268 |
* potentially-alive persistent SMTP connections. |
| 269 |
*/ |
| 270 |
function __destruct() |
| 271 |
{ |
| 272 |
if (is_object($this->_smtp)) { |
| 273 |
$this->_smtp->disconnect(); |
| 274 |
$this->_smtp = null; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Implements Mail::send() function using SMTP direct delivery |
| 280 |
* |
| 281 |
* @access public |
| 282 |
* @param mixed $recipients in RFC822 style or array |
| 283 |
* @param array $headers The array of headers to send with the mail. |
| 284 |
* @param string $body The full text of the message body, |
| 285 |
* @return mixed Returns true on success, or a PEAR_Error |
| 286 |
*/ |
| 287 |
function send($recipients, $headers, $body) |
| 288 |
{ |
| 289 |
if (!is_array($headers)) { |
| 290 |
return PEAR::raiseError('$headers must be an array'); |
| 291 |
} |
| 292 |
|
| 293 |
$result = $this->_sanitizeHeaders($headers); |
| 294 |
if (is_a($result, 'PEAR_Error')) { |
| 295 |
return $result; |
| 296 |
} |
| 297 |
|
| 298 |
// Prepare headers |
| 299 |
$headerElements = $this->prepareHeaders($headers); |
| 300 |
if (is_a($headerElements, 'PEAR_Error')) { |
| 301 |
return $headerElements; |
| 302 |
} |
| 303 |
list($from, $textHeaders) = $headerElements; |
| 304 |
|
| 305 |
// use 'Return-Path' if possible |
| 306 |
if (!empty($headers['Return-Path'])) { |
| 307 |
$from = $headers['Return-Path']; |
| 308 |
} |
| 309 |
if (!isset($from)) { |
| 310 |
return $this->_raiseError('no_from'); |
| 311 |
} |
| 312 |
|
| 313 |
// Prepare recipients |
| 314 |
$recipients = $this->parseRecipients($recipients); |
| 315 |
if (is_a($recipients, 'PEAR_Error')) { |
| 316 |
return $recipients; |
| 317 |
} |
| 318 |
|
| 319 |
foreach ($recipients as $rcpt) { |
| 320 |
list($user, $host) = explode('@', $rcpt); |
| 321 |
|
| 322 |
$mx = $this->_getMx($host); |
| 323 |
if (is_a($mx, 'PEAR_Error')) { |
| 324 |
return $mx; |
| 325 |
} |
| 326 |
|
| 327 |
if (empty($mx)) { |
| 328 |
$info = array('rcpt' => $rcpt); |
| 329 |
return $this->_raiseError('no_mx', $info); |
| 330 |
} |
| 331 |
|
| 332 |
$connected = false; |
| 333 |
foreach ($mx as $mserver => $mpriority) { |
| 334 |
$this->_smtp = new Net_SMTP($mserver, $this->port, $this->mailname); |
| 335 |
|
| 336 |
// configure the SMTP connection. |
| 337 |
if ($this->debug) { |
| 338 |
$this->_smtp->setDebug(true, $this->debug_handler); |
| 339 |
} |
| 340 |
|
| 341 |
// attempt to connect to the configured SMTP server. |
| 342 |
$res = $this->_smtp->connect($this->timeout); |
| 343 |
if (is_a($res, 'PEAR_Error')) { |
| 344 |
$this->_smtp = null; |
| 345 |
continue; |
| 346 |
} |
| 347 |
|
| 348 |
// connection established |
| 349 |
if ($res) { |
| 350 |
$connected = true; |
| 351 |
break; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
if (!$connected) { |
| 356 |
$info = array( |
| 357 |
'host' => implode(', ', array_keys($mx)), |
| 358 |
'port' => $this->port, |
| 359 |
'rcpt' => $rcpt, |
| 360 |
); |
| 361 |
return $this->_raiseError('not_connected', $info); |
| 362 |
} |
| 363 |
|
| 364 |
// Verify recipient |
| 365 |
if ($this->vrfy) { |
| 366 |
$res = $this->_smtp->vrfy($rcpt); |
| 367 |
if (is_a($res, 'PEAR_Error')) { |
| 368 |
$info = array('rcpt' => $rcpt); |
| 369 |
return $this->_raiseError('failed_vrfy_rcpt', $info); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
// mail from: |
| 374 |
$args['verp'] = $this->verp; |
| 375 |
$res = $this->_smtp->mailFrom($from, $args); |
| 376 |
if (is_a($res, 'PEAR_Error')) { |
| 377 |
$info = array('from' => $from); |
| 378 |
return $this->_raiseError('failed_set_from', $info); |
| 379 |
} |
| 380 |
|
| 381 |
// rcpt to: |
| 382 |
$res = $this->_smtp->rcptTo($rcpt); |
| 383 |
if (is_a($res, 'PEAR_Error')) { |
| 384 |
$info = array('rcpt' => $rcpt); |
| 385 |
return $this->_raiseError('failed_set_rcpt', $info); |
| 386 |
} |
| 387 |
|
| 388 |
// Don't send anything in test mode |
| 389 |
if ($this->test) { |
| 390 |
$result = $this->_smtp->rset(); |
| 391 |
$res = $this->_smtp->rset(); |
| 392 |
if (is_a($res, 'PEAR_Error')) { |
| 393 |
return $this->_raiseError('failed_rset'); |
| 394 |
} |
| 395 |
|
| 396 |
$this->_smtp->disconnect(); |
| 397 |
$this->_smtp = null; |
| 398 |
return true; |
| 399 |
} |
| 400 |
|
| 401 |
// Send data |
| 402 |
$res = $this->_smtp->data($body, $textHeaders); |
| 403 |
if (is_a($res, 'PEAR_Error')) { |
| 404 |
$info = array('rcpt' => $rcpt); |
| 405 |
return $this->_raiseError('failed_send_data', $info); |
| 406 |
} |
| 407 |
|
| 408 |
$this->_smtp->disconnect(); |
| 409 |
$this->_smtp = null; |
| 410 |
} |
| 411 |
|
| 412 |
return true; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Recieve mx rexords for a spciefied host |
| 417 |
* |
| 418 |
* The MX records |
| 419 |
* |
| 420 |
* @access private |
| 421 |
* @param string $host mail host |
| 422 |
* @return mixed sorted |
| 423 |
*/ |
| 424 |
function _getMx($host) |
| 425 |
{ |
| 426 |
$mx = array(); |
| 427 |
|
| 428 |
if ($this->withNetDns) { |
| 429 |
$res = $this->_loadNetDns(); |
| 430 |
if (is_a($res, 'PEAR_Error')) { |
| 431 |
return $res; |
| 432 |
} |
| 433 |
|
| 434 |
$response = $this->resolver->query($host, 'MX'); |
| 435 |
if (!$response) { |
| 436 |
return false; |
| 437 |
} |
| 438 |
|
| 439 |
foreach ($response->answer as $rr) { |
| 440 |
if ($rr->type == 'MX') { |
| 441 |
$mx[$rr->exchange] = $rr->preference; |
| 442 |
} |
| 443 |
} |
| 444 |
} else { |
| 445 |
$mxHost = array(); |
| 446 |
$mxWeight = array(); |
| 447 |
|
| 448 |
if (!getmxrr($host, $mxHost, $mxWeight)) { |
| 449 |
return false; |
| 450 |
} |
| 451 |
for ($i = 0; $i < count($mxHost); ++$i) { |
| 452 |
$mx[$mxHost[$i]] = $mxWeight[$i]; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
asort($mx); |
| 457 |
return $mx; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* initialize PEAR:Net_DNS_Resolver |
| 462 |
* |
| 463 |
* @access private |
| 464 |
* @return boolean true on success |
| 465 |
*/ |
| 466 |
function _loadNetDns() |
| 467 |
{ |
| 468 |
if (is_object($this->resolver)) { |
| 469 |
return true; |
| 470 |
} |
| 471 |
|
| 472 |
if (!include_once 'Net/DNS.php') { |
| 473 |
return $this->_raiseError('no_resolver'); |
| 474 |
} |
| 475 |
|
| 476 |
$this->resolver = new Net_DNS_Resolver(); |
| 477 |
if ($this->debug) { |
| 478 |
$this->resolver->debug = 1; |
| 479 |
} |
| 480 |
|
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* raise standardized error |
| 486 |
* |
| 487 |
* include additional information in error message |
| 488 |
* |
| 489 |
* @access private |
| 490 |
* @param string $id maps error ids to codes and message |
| 491 |
* @param array $info optional information in associative array |
| 492 |
* @see _errorCode |
| 493 |
*/ |
| 494 |
function _raiseError($id, $info = array()) |
| 495 |
{ |
| 496 |
$code = $this->errorCode[$id]['code']; |
| 497 |
$msg = $this->errorCode[$id]['msg']; |
| 498 |
|
| 499 |
// include info to messages |
| 500 |
if (!empty($info)) { |
| 501 |
$search = array(); |
| 502 |
$replace = array(); |
| 503 |
|
| 504 |
foreach ($info as $key => $value) { |
| 505 |
array_push($search, '{' . strtoupper($key) . '}'); |
| 506 |
array_push($replace, $value); |
| 507 |
} |
| 508 |
|
| 509 |
$msg = str_replace($search, $replace, $msg); |
| 510 |
} |
| 511 |
|
| 512 |
return PEAR::raiseError($msg, $code); |
| 513 |
} |
| 514 |
|
| 515 |
} |