2 /** vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
 
   3 // +----------------------------------------------------------------------+
 
   4 // | PHP Version 5 and 7                                                  |
 
   5 // +----------------------------------------------------------------------+
 
   6 // | Copyright (c) 1997-2015 Jon Parise and Chuck Hagenbuch               |
 
   7 // +----------------------------------------------------------------------+
 
   8 // | This source file is subject to version 3.01 of the PHP license,      |
 
   9 // | that is bundled with this package in the file LICENSE, and is        |
 
  10 // | available at through the world-wide-web at                           |
 
  11 // | http://www.php.net/license/3_01.txt.                                 |
 
  12 // | If you did not receive a copy of the PHP license and are unable to   |
 
  13 // | obtain it through the world-wide-web, please send a note to          |
 
  14 // | license@php.net so we can mail you a copy immediately.               |
 
  15 // +----------------------------------------------------------------------+
 
  16 // | Authors: Chuck Hagenbuch <chuck@horde.org>                           |
 
  17 // |          Jon Parise <jon@php.net>                                    |
 
  18 // |          Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>      |
 
  19 // +----------------------------------------------------------------------+
 
  21 require_once 'PEAR.php';
 
  22 require_once 'Net/Socket.php';
 
  25  * Provides an implementation of the SMTP protocol using PEAR's
 
  29  * @author  Chuck Hagenbuch <chuck@horde.org>
 
  30  * @author  Jon Parise <jon@php.net>
 
  31  * @author  Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
 
  33  * @example basic.php A basic implementation of the Net_SMTP package.
 
  38      * The server to connect to.
 
  41     public $host = 'localhost';
 
  44      * The port to connect to.
 
  50      * The value to give when sending EHLO or HELO.
 
  53     public $localhost = 'localhost';
 
  56      * List of supported authentication methods, in preferential order.
 
  59     public $auth_methods = array();
 
  62      * Use SMTP command pipelining (specified in RFC 2920) if the SMTP
 
  65      * When pipeling is enabled, rcptTo(), mailFrom(), sendFrom(),
 
  66      * somlFrom() and samlFrom() do not wait for a response from the
 
  67      * SMTP server but return immediately.
 
  71     public $pipelining = false;
 
  74      * Number of pipelined commands.
 
  77     protected $pipelined_commands = 0;
 
  80      * Should debugging output be enabled?
 
  83     protected $debug = false;
 
  86      * Debug output handler.
 
  89     protected $debug_handler = null;
 
  92      * The socket resource being used to connect to the SMTP server.
 
  95     protected $socket = null;
 
  98      * Array of socket options that will be passed to Net_Socket::connect().
 
  99      * @see stream_context_create()
 
 102     protected $socket_options = null;
 
 105      * The socket I/O timeout value in seconds.
 
 108     protected $timeout = 0;
 
 111      * The most recent server response code.
 
 114     protected $code = -1;
 
 117      * The most recent server response arguments.
 
 120     protected $arguments = array();
 
 123      * Stores the SMTP server's greeting string.
 
 126     protected $greeting = null;
 
 129      * Stores detected features of the SMTP server.
 
 132     protected $esmtp = array();
 
 135      * Instantiates a new Net_SMTP object, overriding any defaults
 
 136      * with parameters that are passed in.
 
 138      * If you have SSL support in PHP, you can connect to a server
 
 139      * over SSL using an 'ssl://' prefix:
 
 141      *   // 465 is a common smtps port.
 
 142      *   $smtp = new Net_SMTP('ssl://mail.host.com', 465);
 
 145      * @param string  $host           The server to connect to.
 
 146      * @param integer $port           The port to connect to.
 
 147      * @param string  $localhost      The value to give when sending EHLO or HELO.
 
 148      * @param boolean $pipelining     Use SMTP command pipelining
 
 149      * @param integer $timeout        Socket I/O timeout in seconds.
 
 150      * @param array   $socket_options Socket stream_context_create() options.
 
 154     public function __construct($host = null, $port = null, $localhost = null,
 
 155         $pipelining = false, $timeout = 0, $socket_options = null
 
 163         if (isset($localhost)) {
 
 164             $this->localhost = $localhost;
 
 167         $this->pipelining      = $pipelining;
 
 168         $this->socket         = new Net_Socket();
 
 169         $this->socket_options = $socket_options;
 
 170         $this->timeout        = $timeout;
 
 172         /* Include the Auth_SASL package.  If the package is available, we
 
 173          * enable the authentication methods that depend upon it. */
 
 174         if (@include_once 'Auth/SASL.php') {
 
 175             $this->setAuthMethod('CRAM-MD5', array($this, 'authCramMD5'));
 
 176             $this->setAuthMethod('DIGEST-MD5', array($this, 'authDigestMD5'));
 
 179         /* These standard authentication methods are always available. */
 
 180         $this->setAuthMethod('LOGIN', array($this, 'authLogin'), false);
 
 181         $this->setAuthMethod('PLAIN', array($this, 'authPlain'), false);
 
 185      * Set the socket I/O timeout value in seconds plus microseconds.
 
 187      * @param integer $seconds      Timeout value in seconds.
 
 188      * @param integer $microseconds Additional value in microseconds.
 
 192     public function setTimeout($seconds, $microseconds = 0)
 
 194         return $this->socket->setTimeout($seconds, $microseconds);
 
 198      * Set the value of the debugging flag.
 
 200      * @param boolean  $debug   New value for the debugging flag.
 
 201      * @param callback $handler Debug handler callback
 
 205     public function setDebug($debug, $handler = null)
 
 207         $this->debug         = $debug;
 
 208         $this->debug_handler = $handler;
 
 212      * Write the given debug text to the current debug output handler.
 
 214      * @param string $message Debug mesage text.
 
 218     protected function debug($message)
 
 221             if ($this->debug_handler) {
 
 222                 call_user_func_array(
 
 223                     $this->debug_handler, array(&$this, $message)
 
 226                 echo "DEBUG: $message\n";
 
 232      * Send the given string of data to the server.
 
 234      * @param string $data The string of data to send.
 
 236      * @return mixed The number of bytes that were actually written,
 
 237      *               or a PEAR_Error object on failure.
 
 241     protected function send($data)
 
 243         $this->debug("Send: $data");
 
 245         $result = $this->socket->write($data);
 
 246         if (!$result || PEAR::isError($result)) {
 
 247             $msg = $result ? $result->getMessage() : "unknown error";
 
 248             return PEAR::raiseError("Failed to write to socket: $msg");
 
 255      * Send a command to the server with an optional string of
 
 256      * arguments.  A carriage return / linefeed (CRLF) sequence will
 
 257      * be appended to each command string before it is sent to the
 
 258      * SMTP server - an error will be thrown if the command string
 
 259      * already contains any newline characters. Use send() for
 
 260      * commands that must contain newlines.
 
 262      * @param string $command The SMTP command to send to the server.
 
 263      * @param string $args    A string of optional arguments to append
 
 266      * @return mixed The result of the send() call.
 
 270     protected function put($command, $args = '')
 
 273             $command .= ' ' . $args;
 
 276         if (strcspn($command, "\r\n") !== strlen($command)) {
 
 277             return PEAR::raiseError('Commands cannot contain newlines');
 
 280         return $this->send($command . "\r\n");
 
 284      * Read a reply from the SMTP server.  The reply consists of a response
 
 285      * code and a response message.
 
 287      * @param mixed $valid The set of valid response codes.  These
 
 288      *                     may be specified as an array of integer
 
 289      *                     values or as a single integer value.
 
 290      * @param bool  $later Do not parse the response now, but wait
 
 291      *                     until the last command in the pipelined
 
 294      * @return mixed True if the server returned a valid response code or
 
 295      *               a PEAR_Error object is an error condition is reached.
 
 301     protected function parseResponse($valid, $later = false)
 
 304         $this->arguments = array();
 
 307             $this->pipelined_commands++;
 
 311         for ($i = 0; $i <= $this->pipelined_commands; $i++) {
 
 312             while ($line = $this->socket->readLine()) {
 
 313                 $this->debug("Recv: $line");
 
 315                 /* If we receive an empty line, the connection was closed. */
 
 318                     return PEAR::raiseError('Connection was closed');
 
 321                 /* Read the code and store the rest in the arguments array. */
 
 322                 $code = substr($line, 0, 3);
 
 323                 $this->arguments[] = trim(substr($line, 4));
 
 325                 /* Check the syntax of the response code. */
 
 326                 if (is_numeric($code)) {
 
 327                     $this->code = (int)$code;
 
 333                 /* If this is not a multiline response, we're done. */
 
 334                 if (substr($line, 3, 1) != '-') {
 
 340         $this->pipelined_commands = 0;
 
 342         /* Compare the server's response code with the valid code/codes. */
 
 343         if (is_int($valid) && ($this->code === $valid)) {
 
 345         } elseif (is_array($valid) && in_array($this->code, $valid, true)) {
 
 349         return PEAR::raiseError('Invalid response code received from server', $this->code);
 
 353      * Issue an SMTP command and verify its response.
 
 355      * @param string $command The SMTP command string or data.
 
 356      * @param mixed  $valid   The set of valid response codes. These
 
 357      *                        may be specified as an array of integer
 
 358      *                        values or as a single integer value.
 
 360      * @return mixed True on success or a PEAR_Error object on failure.
 
 364     public function command($command, $valid)
 
 366         if (PEAR::isError($error = $this->put($command))) {
 
 369         if (PEAR::isError($error = $this->parseResponse($valid))) {
 
 377      * Return a 2-tuple containing the last response from the SMTP server.
 
 379      * @return array A two-element array: the first element contains the
 
 380      *               response code as an integer and the second element
 
 381      *               contains the response's arguments as a string.
 
 385     public function getResponse()
 
 387         return array($this->code, join("\n", $this->arguments));
 
 391      * Return the SMTP server's greeting string.
 
 393      * @return string A string containing the greeting string, or null if
 
 394      *                a greeting has not been received.
 
 398     public function getGreeting()
 
 400         return $this->greeting;
 
 404      * Attempt to connect to the SMTP server.
 
 406      * @param int  $timeout    The timeout value (in seconds) for the
 
 407      *                         socket connection attempt.
 
 408      * @param bool $persistent Should a persistent socket connection
 
 411      * @return mixed Returns a PEAR_Error with an error message on any
 
 412      *               kind of failure, or true on success.
 
 415     public function connect($timeout = null, $persistent = false)
 
 417         $this->greeting = null;
 
 419         $result = $this->socket->connect(
 
 420             $this->host, $this->port, $persistent, $timeout, $this->socket_options
 
 423         if (PEAR::isError($result)) {
 
 424             return PEAR::raiseError(
 
 425                 'Failed to connect socket: ' . $result->getMessage()
 
 430          * Now that we're connected, reset the socket's timeout value for
 
 431          * future I/O operations.  This allows us to have different socket
 
 432          * timeout values for the initial connection (our $timeout parameter)
 
 433          * and all other socket operations.
 
 435         if ($this->timeout > 0) {
 
 436             if (PEAR::isError($error = $this->setTimeout($this->timeout))) {
 
 441         if (PEAR::isError($error = $this->parseResponse(220))) {
 
 445         /* Extract and store a copy of the server's greeting string. */
 
 446         list(, $this->greeting) = $this->getResponse();
 
 448         if (PEAR::isError($error = $this->negotiate())) {
 
 456      * Attempt to disconnect from the SMTP server.
 
 458      * @return mixed Returns a PEAR_Error with an error message on any
 
 459      *               kind of failure, or true on success.
 
 462     public function disconnect()
 
 464         if (PEAR::isError($error = $this->put('QUIT'))) {
 
 467         if (PEAR::isError($error = $this->parseResponse(221))) {
 
 470         if (PEAR::isError($error = $this->socket->disconnect())) {
 
 471             return PEAR::raiseError(
 
 472                 'Failed to disconnect socket: ' . $error->getMessage()
 
 480      * Attempt to send the EHLO command and obtain a list of ESMTP
 
 481      * extensions available, and failing that just send HELO.
 
 483      * @return mixed Returns a PEAR_Error with an error message on any
 
 484      *               kind of failure, or true on success.
 
 488     protected function negotiate()
 
 490         if (PEAR::isError($error = $this->put('EHLO', $this->localhost))) {
 
 494         if (PEAR::isError($this->parseResponse(250))) {
 
 495             /* If the EHLO failed, try the simpler HELO command. */
 
 496             if (PEAR::isError($error = $this->put('HELO', $this->localhost))) {
 
 499             if (PEAR::isError($this->parseResponse(250))) {
 
 500                 return PEAR::raiseError('HELO was not accepted', $this->code);
 
 506         foreach ($this->arguments as $argument) {
 
 507             $verb      = strtok($argument, ' ');
 
 508             $len       = strlen($verb);
 
 509             $arguments = substr($argument, $len + 1, strlen($argument) - $len - 1);
 
 510             $this->esmtp[$verb] = $arguments;
 
 513         if (!isset($this->esmtp['PIPELINING'])) {
 
 514             $this->pipelining = false;
 
 521      * Returns the name of the best authentication method that the server
 
 524      * @return mixed Returns a string containing the name of the best
 
 525      *               supported authentication method or a PEAR_Error object
 
 526      *               if a failure condition is encountered.
 
 529     protected function getBestAuthMethod()
 
 531         $available_methods = explode(' ', $this->esmtp['AUTH']);
 
 533         foreach ($this->auth_methods as $method => $callback) {
 
 534             if (in_array($method, $available_methods)) {
 
 539         return PEAR::raiseError('No supported authentication methods');
 
 543      * Attempt to do SMTP authentication.
 
 545      * @param string $uid    The userid to authenticate as.
 
 546      * @param string $pwd    The password to authenticate with.
 
 547      * @param string $method The requested authentication method.  If none is
 
 548      *                       specified, the best supported method will be used.
 
 549      * @param bool   $tls    Flag indicating whether or not TLS should be attempted.
 
 550      * @param string $authz  An optional authorization identifier.  If specified, this
 
 551      *                       identifier will be used as the authorization proxy.
 
 553      * @return mixed Returns a PEAR_Error with an error message on any
 
 554      *               kind of failure, or true on success.
 
 557     public function auth($uid, $pwd , $method = '', $tls = true, $authz = '')
 
 559         /* We can only attempt a TLS connection if one has been requested,
 
 560          * we're running PHP 5.1.0 or later, have access to the OpenSSL
 
 561          * extension, are connected to an SMTP server which supports the
 
 562          * STARTTLS extension, and aren't already connected over a secure
 
 563          * (SSL) socket connection. */
 
 564         if ($tls && version_compare(PHP_VERSION, '5.1.0', '>=')
 
 565             && extension_loaded('openssl') && isset($this->esmtp['STARTTLS'])
 
 566             && strncasecmp($this->host, 'ssl://', 6) !== 0
 
 568             /* Start the TLS connection attempt. */
 
 569             if (PEAR::isError($result = $this->put('STARTTLS'))) {
 
 572             if (PEAR::isError($result = $this->parseResponse(220))) {
 
 575             if (isset($this->socket_options['ssl']['crypto_method'])) {
 
 576                 $crypto_method = $this->socket_options['ssl']['crypto_method'];
 
 578                 /* STREAM_CRYPTO_METHOD_TLS_ANY_CLIENT constant does not exist
 
 579                  * and STREAM_CRYPTO_METHOD_SSLv23_CLIENT constant is
 
 580                  * inconsistent across PHP versions. */
 
 581                 $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT
 
 582                                  | @STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
 
 583                                  | @STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
 
 585             if (PEAR::isError($result = $this->socket->enableCrypto(true, $crypto_method))) {
 
 587             } elseif ($result !== true) {
 
 588                 return PEAR::raiseError('STARTTLS failed');
 
 591             /* Send EHLO again to recieve the AUTH string from the
 
 596         if (empty($this->esmtp['AUTH'])) {
 
 597             return PEAR::raiseError('SMTP server does not support authentication');
 
 600         /* If no method has been specified, get the name of the best
 
 601          * supported method advertised by the SMTP server. */
 
 602         if (empty($method)) {
 
 603             if (PEAR::isError($method = $this->getBestAuthMethod())) {
 
 604                 /* Return the PEAR_Error object from _getBestAuthMethod(). */
 
 608             $method = strtoupper($method);
 
 609             if (!array_key_exists($method, $this->auth_methods)) {
 
 610                 return PEAR::raiseError("$method is not a supported authentication method");
 
 614         if (!isset($this->auth_methods[$method])) {
 
 615             return PEAR::raiseError("$method is not a supported authentication method");
 
 618         if (!is_callable($this->auth_methods[$method], false)) {
 
 619             return PEAR::raiseError("$method authentication method cannot be called");
 
 622         if (is_array($this->auth_methods[$method])) {
 
 623             list($object, $method) = $this->auth_methods[$method];
 
 624             $result = $object->{$method}($uid, $pwd, $authz, $this);
 
 626             $func   = $this->auth_methods[$method];
 
 627             $result = $func($uid, $pwd, $authz, $this);
 
 630         /* If an error was encountered, return the PEAR_Error object. */
 
 631         if (PEAR::isError($result)) {
 
 639      * Add a new authentication method.
 
 641      * @param string $name     The authentication method name (e.g. 'PLAIN')
 
 642      * @param mixed  $callback The authentication callback (given as the name of a
 
 643      *                         function or as an (object, method name) array).
 
 644      * @param bool   $prepend  Should the new method be prepended to the list of
 
 645      *                         available methods?  This is the default behavior,
 
 646      *                         giving the new method the highest priority.
 
 648      * @return mixed True on success or a PEAR_Error object on failure.
 
 652     public function setAuthMethod($name, $callback, $prepend = true)
 
 654         if (!is_string($name)) {
 
 655             return PEAR::raiseError('Method name is not a string');
 
 658         if (!is_string($callback) && !is_array($callback)) {
 
 659             return PEAR::raiseError('Method callback must be string or array');
 
 662         if (is_array($callback)) {
 
 663             if (!is_object($callback[0]) || !is_string($callback[1])) {
 
 664                 return PEAR::raiseError('Bad mMethod callback array');
 
 669             $this->auth_methods = array_merge(
 
 670                 array($name => $callback), $this->auth_methods
 
 673             $this->auth_methods[$name] = $callback;
 
 680      * Authenticates the user using the DIGEST-MD5 method.
 
 682      * @param string $uid   The userid to authenticate as.
 
 683      * @param string $pwd   The password to authenticate with.
 
 684      * @param string $authz The optional authorization proxy identifier.
 
 686      * @return mixed Returns a PEAR_Error with an error message on any
 
 687      *               kind of failure, or true on success.
 
 690     protected function authDigestMD5($uid, $pwd, $authz = '')
 
 692         if (PEAR::isError($error = $this->put('AUTH', 'DIGEST-MD5'))) {
 
 695         /* 334: Continue authentication request */
 
 696         if (PEAR::isError($error = $this->parseResponse(334))) {
 
 697             /* 503: Error: already authenticated */
 
 698             if ($this->code === 503) {
 
 704         $digest    = Auth_SASL::factory('digest-md5');
 
 705         $challenge = base64_decode($this->arguments[0]);
 
 706         $auth_str  = base64_encode(
 
 707             $digest->getResponse($uid, $pwd, $challenge, $this->host, "smtp", $authz)
 
 710         if (PEAR::isError($error = $this->put($auth_str))) {
 
 713         /* 334: Continue authentication request */
 
 714         if (PEAR::isError($error = $this->parseResponse(334))) {
 
 718         /* We don't use the protocol's third step because SMTP doesn't
 
 719          * allow subsequent authentication, so we just silently ignore
 
 721         if (PEAR::isError($error = $this->put(''))) {
 
 724         /* 235: Authentication successful */
 
 725         if (PEAR::isError($error = $this->parseResponse(235))) {
 
 731      * Authenticates the user using the CRAM-MD5 method.
 
 733      * @param string $uid   The userid to authenticate as.
 
 734      * @param string $pwd   The password to authenticate with.
 
 735      * @param string $authz The optional authorization proxy identifier.
 
 737      * @return mixed Returns a PEAR_Error with an error message on any
 
 738      *               kind of failure, or true on success.
 
 741     protected function authCRAMMD5($uid, $pwd, $authz = '')
 
 743         if (PEAR::isError($error = $this->put('AUTH', 'CRAM-MD5'))) {
 
 746         /* 334: Continue authentication request */
 
 747         if (PEAR::isError($error = $this->parseResponse(334))) {
 
 748             /* 503: Error: already authenticated */
 
 749             if ($this->code === 503) {
 
 755         $challenge = base64_decode($this->arguments[0]);
 
 756         $cram      = Auth_SASL::factory('cram-md5');
 
 757         $auth_str  = base64_encode($cram->getResponse($uid, $pwd, $challenge));
 
 759         if (PEAR::isError($error = $this->put($auth_str))) {
 
 763         /* 235: Authentication successful */
 
 764         if (PEAR::isError($error = $this->parseResponse(235))) {
 
 770      * Authenticates the user using the LOGIN method.
 
 772      * @param string $uid   The userid to authenticate as.
 
 773      * @param string $pwd   The password to authenticate with.
 
 774      * @param string $authz The optional authorization proxy identifier.
 
 776      * @return mixed Returns a PEAR_Error with an error message on any
 
 777      *               kind of failure, or true on success.
 
 780     protected function authLogin($uid, $pwd, $authz = '')
 
 782         if (PEAR::isError($error = $this->put('AUTH', 'LOGIN'))) {
 
 785         /* 334: Continue authentication request */
 
 786         if (PEAR::isError($error = $this->parseResponse(334))) {
 
 787             /* 503: Error: already authenticated */
 
 788             if ($this->code === 503) {
 
 794         if (PEAR::isError($error = $this->put(base64_encode($uid)))) {
 
 797         /* 334: Continue authentication request */
 
 798         if (PEAR::isError($error = $this->parseResponse(334))) {
 
 802         if (PEAR::isError($error = $this->put(base64_encode($pwd)))) {
 
 806         /* 235: Authentication successful */
 
 807         if (PEAR::isError($error = $this->parseResponse(235))) {
 
 815      * Authenticates the user using the PLAIN method.
 
 817      * @param string $uid   The userid to authenticate as.
 
 818      * @param string $pwd   The password to authenticate with.
 
 819      * @param string $authz The optional authorization proxy identifier.
 
 821      * @return mixed Returns a PEAR_Error with an error message on any
 
 822      *               kind of failure, or true on success.
 
 825     protected function authPlain($uid, $pwd, $authz = '')
 
 827         if (PEAR::isError($error = $this->put('AUTH', 'PLAIN'))) {
 
 830         /* 334: Continue authentication request */
 
 831         if (PEAR::isError($error = $this->parseResponse(334))) {
 
 832             /* 503: Error: already authenticated */
 
 833             if ($this->code === 503) {
 
 839         $auth_str = base64_encode($authz . chr(0) . $uid . chr(0) . $pwd);
 
 841         if (PEAR::isError($error = $this->put($auth_str))) {
 
 845         /* 235: Authentication successful */
 
 846         if (PEAR::isError($error = $this->parseResponse(235))) {
 
 854      * Send the HELO command.
 
 856      * @param string $domain The domain name to say we are.
 
 858      * @return mixed Returns a PEAR_Error with an error message on any
 
 859      *               kind of failure, or true on success.
 
 862     public function helo($domain)
 
 864         if (PEAR::isError($error = $this->put('HELO', $domain))) {
 
 867         if (PEAR::isError($error = $this->parseResponse(250))) {
 
 875      * Return the list of SMTP service extensions advertised by the server.
 
 877      * @return array The list of SMTP service extensions.
 
 880     public function getServiceExtensions()
 
 886      * Send the MAIL FROM: command.
 
 888      * @param string $sender The sender (reverse path) to set.
 
 889      * @param string $params String containing additional MAIL parameters,
 
 890      *                       such as the NOTIFY flags defined by RFC 1891
 
 891      *                       or the VERP protocol.
 
 893      *                       If $params is an array, only the 'verp' option
 
 894      *                       is supported.  If 'verp' is true, the XVERP
 
 895      *                       parameter is appended to the MAIL command.
 
 896      *                       If the 'verp' value is a string, the full
 
 897      *                       XVERP=value parameter is appended.
 
 899      * @return mixed Returns a PEAR_Error with an error message on any
 
 900      *               kind of failure, or true on success.
 
 903     public function mailFrom($sender, $params = null)
 
 905         $args = "FROM:<$sender>";
 
 907         /* Support the deprecated array form of $params. */
 
 908         if (is_array($params) && isset($params['verp'])) {
 
 909             if ($params['verp'] === true) {
 
 911             } elseif (trim($params['verp'])) {
 
 912                 $args .= ' XVERP=' . $params['verp'];
 
 914         } elseif (is_string($params) && !empty($params)) {
 
 915             $args .= ' ' . $params;
 
 918         if (PEAR::isError($error = $this->put('MAIL', $args))) {
 
 921         if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) {
 
 929      * Send the RCPT TO: command.
 
 931      * @param string $recipient The recipient (forward path) to add.
 
 932      * @param string $params    String containing additional RCPT parameters,
 
 933      *                          such as the NOTIFY flags defined by RFC 1891.
 
 935      * @return mixed Returns a PEAR_Error with an error message on any
 
 936      *               kind of failure, or true on success.
 
 940     public function rcptTo($recipient, $params = null)
 
 942         $args = "TO:<$recipient>";
 
 943         if (is_string($params)) {
 
 944             $args .= ' ' . $params;
 
 947         if (PEAR::isError($error = $this->put('RCPT', $args))) {
 
 950         if (PEAR::isError($error = $this->parseResponse(array(250, 251), $this->pipelining))) {
 
 958      * Quote the data so that it meets SMTP standards.
 
 960      * This is provided as a separate public function to facilitate
 
 961      * easier overloading for the cases where it is desirable to
 
 962      * customize the quoting behavior.
 
 964      * @param string &$data The message text to quote. The string must be passed
 
 965      *                      by reference, and the text will be modified in place.
 
 969     public function quotedata(&$data)
 
 971         /* Because a single leading period (.) signifies an end to the
 
 972          * data, legitimate leading periods need to be "doubled" ('..'). */
 
 973         $data = preg_replace('/^\./m', '..', $data);
 
 975         /* Change Unix (\n) and Mac (\r) linefeeds into CRLF's (\r\n). */
 
 976         $data = preg_replace('/(?:\r\n|\n|\r(?!\n))/', "\r\n", $data);
 
 980      * Send the DATA command.
 
 982      * @param mixed  $data    The message data, either as a string or an open
 
 984      * @param string $headers The message headers.  If $headers is provided,
 
 985      *                        $data is assumed to contain only body data.
 
 987      * @return mixed Returns a PEAR_Error with an error message on any
 
 988      *               kind of failure, or true on success.
 
 991     public function data($data, $headers = null)
 
 993         /* Verify that $data is a supported type. */
 
 994         if (!is_string($data) && !is_resource($data)) {
 
 995             return PEAR::raiseError('Expected a string or file resource');
 
 998         /* Start by considering the size of the optional headers string.  We
 
 999          * also account for the addition 4 character "\r\n\r\n" separator
 
1001         $size = (is_null($headers)) ? 0 : strlen($headers) + 4;
 
1003         if (is_resource($data)) {
 
1004             $stat = fstat($data);
 
1005             if ($stat === false) {
 
1006                 return PEAR::raiseError('Failed to get file size');
 
1008             $size += $stat['size'];
 
1010             $size += strlen($data);
 
1013         /* RFC 1870, section 3, subsection 3 states "a value of zero indicates
 
1014          * that no fixed maximum message size is in force".  Furthermore, it
 
1015          * says that if "the parameter is omitted no information is conveyed
 
1016          * about the server's fixed maximum message size". */
 
1017         $limit = (isset($this->esmtp['SIZE'])) ? $this->esmtp['SIZE'] : 0;
 
1018         if ($limit > 0 && $size >= $limit) {
 
1019             $this->disconnect();
 
1020             return PEAR::raiseError('Message size exceeds server limit');
 
1023         /* Initiate the DATA command. */
 
1024         if (PEAR::isError($error = $this->put('DATA'))) {
 
1027         if (PEAR::isError($error = $this->parseResponse(354))) {
 
1031         /* If we have a separate headers string, send it first. */
 
1032         if (!is_null($headers)) {
 
1033             $this->quotedata($headers);
 
1034             if (PEAR::isError($result = $this->send($headers . "\r\n\r\n"))) {
 
1038             /* Subtract the headers size now that they've been sent. */
 
1039             $size -= strlen($headers) + 4;
 
1042         /* Now we can send the message body data. */
 
1043         if (is_resource($data)) {
 
1044             /* Stream the contents of the file resource out over our socket
 
1045              * connection, line by line.  Each line must be run through the
 
1046              * quoting routine. */
 
1047             while (strlen($line = fread($data, 8192)) > 0) {
 
1048                 /* If the last character is an newline, we need to grab the
 
1049                  * next character to check to see if it is a period. */
 
1050                 while (!feof($data)) {
 
1051                     $char = fread($data, 1);
 
1053                     if ($char != "\n") {
 
1057                 $this->quotedata($line);
 
1058                 if (PEAR::isError($result = $this->send($line))) {
 
1066              * Break up the data by sending one chunk (up to 512k) at a time.
 
1067              * This approach reduces our peak memory usage.
 
1069             for ($offset = 0; $offset < $size;) {
 
1070                 $end = $offset + 512000;
 
1073                  * Ensure we don't read beyond our data size or span multiple
 
1074                  * lines.  quotedata() can't properly handle character data
 
1075                  * that's split across two line break boundaries.
 
1077                 if ($end >= $size) {
 
1080                     for (; $end < $size; $end++) {
 
1081                         if ($data[$end] != "\n") {
 
1087                 /* Extract our chunk and run it through the quoting routine. */
 
1088                 $chunk = substr($data, $offset, $end - $offset);
 
1089                 $this->quotedata($chunk);
 
1091                 /* If we run into a problem along the way, abort. */
 
1092                 if (PEAR::isError($result = $this->send($chunk))) {
 
1096                 /* Advance the offset to the end of this chunk. */
 
1103         /* Don't add another CRLF sequence if it's already in the data */
 
1104         $terminator = (substr($last, -2) == "\r\n" ? '' : "\r\n") . ".\r\n";
 
1106         /* Finally, send the DATA terminator sequence. */
 
1107         if (PEAR::isError($result = $this->send($terminator))) {
 
1111         /* Verify that the data was successfully received by the server. */
 
1112         if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) {
 
1120      * Send the SEND FROM: command.
 
1122      * @param string $path The reverse path to send.
 
1124      * @return mixed Returns a PEAR_Error with an error message on any
 
1125      *               kind of failure, or true on success.
 
1128     public function sendFrom($path)
 
1130         if (PEAR::isError($error = $this->put('SEND', "FROM:<$path>"))) {
 
1133         if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) {
 
1141      * Send the SOML FROM: command.
 
1143      * @param string $path The reverse path to send.
 
1145      * @return mixed Returns a PEAR_Error with an error message on any
 
1146      *               kind of failure, or true on success.
 
1149     public function somlFrom($path)
 
1151         if (PEAR::isError($error = $this->put('SOML', "FROM:<$path>"))) {
 
1154         if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) {
 
1162      * Send the SAML FROM: command.
 
1164      * @param string $path The reverse path to send.
 
1166      * @return mixed Returns a PEAR_Error with an error message on any
 
1167      *               kind of failure, or true on success.
 
1170     public function samlFrom($path)
 
1172         if (PEAR::isError($error = $this->put('SAML', "FROM:<$path>"))) {
 
1175         if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) {
 
1183      * Send the RSET command.
 
1185      * @return mixed Returns a PEAR_Error with an error message on any
 
1186      *               kind of failure, or true on success.
 
1189     public function rset()
 
1191         if (PEAR::isError($error = $this->put('RSET'))) {
 
1194         if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) {
 
1202      * Send the VRFY command.
 
1204      * @param string $string The string to verify
 
1206      * @return mixed Returns a PEAR_Error with an error message on any
 
1207      *               kind of failure, or true on success.
 
1210     public function vrfy($string)
 
1212         /* Note: 251 is also a valid response code */
 
1213         if (PEAR::isError($error = $this->put('VRFY', $string))) {
 
1216         if (PEAR::isError($error = $this->parseResponse(array(250, 252)))) {
 
1224      * Send the NOOP command.
 
1226      * @return mixed Returns a PEAR_Error with an error message on any
 
1227      *               kind of failure, or true on success.
 
1230     public function noop()
 
1232         if (PEAR::isError($error = $this->put('NOOP'))) {
 
1235         if (PEAR::isError($error = $this->parseResponse(250))) {
 
1243      * Backwards-compatibility method.  identifySender()'s functionality is
 
1244      * now handled internally.
 
1246      * @return boolean This method always return true.
 
1250     public function identifySender()