Net_SMTP
[ class tree: Net_SMTP ] [ index: Net_SMTP ] [ all elements ]

Source for file SMTP.php

Documentation is available at SMTP.php

  1. <?php
  2. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 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/2_02.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. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: SMTP.php,v 1.64 2008/12/20 23:03:49 jon Exp $
  22.  
  23. require_once 'PEAR.php';
  24. require_once 'Net/Socket.php';
  25.  
  26. /**
  27.  * Provides an implementation of the SMTP protocol using PEAR's
  28.  * Net_Socket:: class.
  29.  *
  30.  * @package Net_SMTP
  31.  * @author  Chuck Hagenbuch <chuck@horde.org>
  32.  * @author  Jon Parise <jon@php.net>
  33.  * @author  Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
  34.  *
  35.  * @example basic.php   A basic implementation of the Net_SMTP package.
  36.  */
  37. class Net_SMTP
  38. {
  39.     /**
  40.      * The server to connect to.
  41.      * @var string 
  42.      * @access public
  43.      */
  44.     var $host = 'localhost';
  45.  
  46.     /**
  47.      * The port to connect to.
  48.      * @var int 
  49.      * @access public
  50.      */
  51.     var $port = 25;
  52.  
  53.     /**
  54.      * The value to give when sending EHLO or HELO.
  55.      * @var string 
  56.      * @access public
  57.      */
  58.     var $localhost = 'localhost';
  59.  
  60.     /**
  61.      * List of supported authentication methods, in preferential order.
  62.      * @var array 
  63.      * @access public
  64.      */
  65.     var $auth_methods = array('DIGEST-MD5''CRAM-MD5''LOGIN''PLAIN');
  66.  
  67.     /**
  68.      * Use SMTP command pipelining (specified in RFC 2920) if the SMTP
  69.      * server supports it.
  70.      *
  71.      * When pipeling is enabled, rcptTo(), mailFrom(), sendFrom(),
  72.      * somlFrom() and samlFrom() do not wait for a response from the
  73.      * SMTP server but return immediately.
  74.      *
  75.      * @var bool 
  76.      * @access public
  77.      */
  78.     var $pipelining = false;
  79.  
  80.     /**
  81.      * Number of pipelined commands.
  82.      * @var int 
  83.      * @access private
  84.      */
  85.     var $_pipelined_commands = 0;
  86.  
  87.     /**
  88.      * Should debugging output be enabled?
  89.      * @var boolean 
  90.      * @access private
  91.      */
  92.     var $_debug = false;
  93.  
  94.     /**
  95.      * The socket resource being used to connect to the SMTP server.
  96.      * @var resource 
  97.      * @access private
  98.      */
  99.     var $_socket = null;
  100.  
  101.     /**
  102.      * The most recent server response code.
  103.      * @var int 
  104.      * @access private
  105.      */
  106.     var $_code = -1;
  107.  
  108.     /**
  109.      * The most recent server response arguments.
  110.      * @var array 
  111.      * @access private
  112.      */
  113.     var $_arguments = array();
  114.  
  115.     /**
  116.      * Stores detected features of the SMTP server.
  117.      * @var array 
  118.      * @access private
  119.      */
  120.     var $_esmtp = array();
  121.  
  122.     /**
  123.      * Instantiates a new Net_SMTP object, overriding any defaults
  124.      * with parameters that are passed in.
  125.      *
  126.      * If you have SSL support in PHP, you can connect to a server
  127.      * over SSL using an 'ssl://' prefix:
  128.      *
  129.      *   // 465 is a common smtps port.
  130.      *   $smtp = new Net_SMTP('ssl://mail.host.com', 465);
  131.      *   $smtp->connect();
  132.      *
  133.      * @param string  $host       The server to connect to.
  134.      * @param integer $port       The port to connect to.
  135.      * @param string  $localhost  The value to give when sending EHLO or HELO.
  136.      * @param boolean $pipeling   Use SMTP command pipelining
  137.      *
  138.      * @access  public
  139.      * @since   1.0
  140.      */
  141.     function Net_SMTP($host = null$port = null$localhost = null$pipelining = false)
  142.     {
  143.         if (isset($host)) {
  144.             $this->host = $host;
  145.         }
  146.         if (isset($port)) {
  147.             $this->port = $port;
  148.         }
  149.         if (isset($localhost)) {
  150.             $this->localhost = $localhost;
  151.         }
  152.         $this->pipelining = $pipelining;
  153.  
  154.         $this->_socket = new Net_Socket();
  155.  
  156.         /* Include the Auth_SASL package.  If the package is not
  157.          * available, we disable the authentication methods that
  158.          * depend upon it. */
  159.         if ((@include_once 'Auth/SASL.php'=== false{
  160.             $pos array_search('DIGEST-MD5'$this->auth_methods);
  161.             unset($this->auth_methods[$pos]);
  162.             $pos array_search('CRAM-MD5'$this->auth_methods);
  163.             unset($this->auth_methods[$pos]);
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Set the value of the debugging flag.
  169.      *
  170.      * @param   boolean $debug      New value for the debugging flag.
  171.      *
  172.      * @access  public
  173.      * @since   1.1.0
  174.      */
  175.     function setDebug($debug)
  176.     {
  177.         $this->_debug $debug;
  178.     }
  179.  
  180.     /**
  181.      * Send the given string of data to the server.
  182.      *
  183.      * @param   string  $data       The string of data to send.
  184.      *
  185.      * @return  mixed   True on success or a PEAR_Error object on failure.
  186.      *
  187.      * @access  private
  188.      * @since   1.1.0
  189.      */
  190.     function _send($data)
  191.     {
  192.         if ($this->_debug{
  193.             echo "DEBUG: Send: $data\n";
  194.         }
  195.  
  196.         if (PEAR::isError($error $this->_socket->write($data))) {
  197.             return PEAR::raiseError('Failed to write to socket: ' .
  198.                                     $error->getMessage());
  199.         }
  200.  
  201.         return true;
  202.     }
  203.  
  204.     /**
  205.      * Send a command to the server with an optional string of
  206.      * arguments.  A carriage return / linefeed (CRLF) sequence will
  207.      * be appended to each command string before it is sent to the
  208.      * SMTP server - an error will be thrown if the command string
  209.      * already contains any newline characters. Use _send() for
  210.      * commands that must contain newlines.
  211.      *
  212.      * @param   string  $command    The SMTP command to send to the server.
  213.      * @param   string  $args       A string of optional arguments to append
  214.      *                               to the command.
  215.      *
  216.      * @return  mixed   The result of the _send() call.
  217.      *
  218.      * @access  private
  219.      * @since   1.1.0
  220.      */
  221.     function _put($command$args '')
  222.     {
  223.         if (!empty($args)) {
  224.             $command .= ' ' $args;
  225.         }
  226.  
  227.         if (strcspn($command"\r\n"!== strlen($command)) {
  228.             return PEAR::raiseError('Commands cannot contain newlines');
  229.         }
  230.  
  231.         return $this->_send($command "\r\n");
  232.     }
  233.  
  234.     /**
  235.      * Read a reply from the SMTP server.  The reply consists of a response
  236.      * code and a response message.
  237.      *
  238.      * @param   mixed   $valid      The set of valid response codes.  These
  239.      *                               may be specified as an array of integer
  240.      *                               values or as a single integer value.
  241.      * @param   bool    $later      Do not parse the response now, but wait
  242.      *                               until the last command in the pipelined
  243.      *                               command group
  244.      *
  245.      * @return  mixed   True if the server returned a valid response code or
  246.      *                   a PEAR_Error object is an error condition is reached.
  247.      *
  248.      * @access  private
  249.      * @since   1.1.0
  250.      *
  251.      * @see     getResponse
  252.      */
  253.     function _parseResponse($valid$later = false)
  254.     {
  255.         $this->_code = -1;
  256.         $this->_arguments = array();
  257.  
  258.         if ($later{
  259.             $this->_pipelined_commands++;
  260.             return true;
  261.         }
  262.  
  263.         for ($i = 0; $i <= $this->_pipelined_commands$i++{
  264.             while ($line $this->_socket->readLine()) {
  265.                 if ($this->_debug{
  266.                     echo "DEBUG: Recv: $line\n";
  267.                 }
  268.  
  269.                 /* If we receive an empty line, the connection has been closed. */
  270.                 if (empty($line)) {
  271.                     $this->disconnect();
  272.                     return PEAR::raiseError('Connection was unexpectedly closed');
  273.                 }
  274.  
  275.                 /* Read the code and store the rest in the arguments array. */
  276.                 $code substr($line03);
  277.                 $this->_arguments[trim(substr($line4));
  278.  
  279.                 /* Check the syntax of the response code. */
  280.                 if (is_numeric($code)) {
  281.                     $this->_code = (int)$code;
  282.                 else {
  283.                     $this->_code = -1;
  284.                     break;
  285.                 }
  286.  
  287.                 /* If this is not a multiline response, we're done. */
  288.                 if (substr($line31!= '-'{
  289.                     break;
  290.                 }
  291.             }
  292.         }
  293.  
  294.         $this->_pipelined_commands = 0;
  295.  
  296.         /* Compare the server's response code with the valid code/codes. */
  297.         if (is_int($valid&& ($this->_code === $valid)) {
  298.             return true;
  299.         elseif (is_array($valid&& in_array($this->_code$validtrue)) {
  300.             return true;
  301.         }
  302.  
  303.         return PEAR::raiseError('Invalid response code received from server',
  304.                                 $this->_code);
  305.     }
  306.  
  307.     /**
  308.      * Return a 2-tuple containing the last response from the SMTP server.
  309.      *
  310.      * @return  array   A two-element array: the first element contains the
  311.      *                   response code as an integer and the second element
  312.      *                   contains the response's arguments as a string.
  313.      *
  314.      * @access  public
  315.      * @since   1.1.0
  316.      */
  317.     function getResponse()
  318.     {
  319.         return array($this->_codejoin("\n"$this->_arguments));
  320.     }
  321.  
  322.     /**
  323.      * Attempt to connect to the SMTP server.
  324.      *
  325.      * @param   int     $timeout    The timeout value (in seconds) for the
  326.      *                               socket connection.
  327.      * @param   bool    $persistent Should a persistent socket connection
  328.      *                               be used?
  329.      *
  330.      * @return mixed Returns a PEAR_Error with an error message on any
  331.      *                kind of failure, or true on success.
  332.      * @access public
  333.      * @since  1.0
  334.      */
  335.     function connect($timeout = null$persistent = false)
  336.     {
  337.         $result $this->_socket->connect($this->host$this->port,
  338.                                           $persistent$timeout);
  339.         if (PEAR::isError($result)) {
  340.             return PEAR::raiseError('Failed to connect socket: ' .
  341.                                     $result->getMessage());
  342.         }
  343.  
  344.         if (PEAR::isError($error $this->_parseResponse(220))) {
  345.             return $error;
  346.         }
  347.         if (PEAR::isError($error $this->_negotiate())) {
  348.             return $error;
  349.         }
  350.  
  351.         return true;
  352.     }
  353.  
  354.     /**
  355.      * Attempt to disconnect from the SMTP server.
  356.      *
  357.      * @return mixed Returns a PEAR_Error with an error message on any
  358.      *                kind of failure, or true on success.
  359.      * @access public
  360.      * @since  1.0
  361.      */
  362.     function disconnect()
  363.     {
  364.         if (PEAR::isError($error $this->_put('QUIT'))) {
  365.             return $error;
  366.         }
  367.         if (PEAR::isError($error $this->_parseResponse(221))) {
  368.             return $error;
  369.         }
  370.         if (PEAR::isError($error $this->_socket->disconnect())) {
  371.             return PEAR::raiseError('Failed to disconnect socket: ' .
  372.                                     $error->getMessage());
  373.         }
  374.  
  375.         return true;
  376.     }
  377.  
  378.     /**
  379.      * Attempt to send the EHLO command and obtain a list of ESMTP
  380.      * extensions available, and failing that just send HELO.
  381.      *
  382.      * @return mixed Returns a PEAR_Error with an error message on any
  383.      *                kind of failure, or true on success.
  384.      *
  385.      * @access private
  386.      * @since  1.1.0
  387.      */
  388.     function _negotiate()
  389.     {
  390.         if (PEAR::isError($error $this->_put('EHLO'$this->localhost))) {
  391.             return $error;
  392.         }
  393.  
  394.         if (PEAR::isError($this->_parseResponse(250))) {
  395.             /* If we receive a 503 response, we're already authenticated. */
  396.             if ($this->_code === 503{
  397.                 return true;
  398.             }
  399.  
  400.             /* If the EHLO failed, try the simpler HELO command. */
  401.             if (PEAR::isError($error $this->_put('HELO'$this->localhost))) {
  402.                 return $error;
  403.             }
  404.             if (PEAR::isError($this->_parseResponse(250))) {
  405.                 return PEAR::raiseError('HELO was not accepted: '$this->_code);
  406.             }
  407.  
  408.             return true;
  409.         }
  410.  
  411.         foreach ($this->_arguments as $argument{
  412.             $verb strtok($argument' ');
  413.             $arguments substr($argumentstrlen($verb+ 1,
  414.                                 strlen($argumentstrlen($verb- 1);
  415.             $this->_esmtp[$verb$arguments;
  416.         }
  417.  
  418.         if (!isset($this->_esmtp['PIPELINING'])) {
  419.             $this->pipelining = false;
  420.         }
  421.  
  422.         return true;
  423.     }
  424.  
  425.     /**
  426.      * Returns the name of the best authentication method that the server
  427.      * has advertised.
  428.      *
  429.      * @return mixed    Returns a string containing the name of the best
  430.      *                   supported authentication method or a PEAR_Error object
  431.      *                   if a failure condition is encountered.
  432.      * @access private
  433.      * @since  1.1.0
  434.      */
  435.     function _getBestAuthMethod()
  436.     {
  437.         $available_methods explode(' '$this->_esmtp['AUTH']);
  438.  
  439.         foreach ($this->auth_methods as $method{
  440.             if (in_array($method$available_methods)) {
  441.                 return $method;
  442.             }
  443.         }
  444.  
  445.         return PEAR::raiseError('No supported authentication methods');
  446.     }
  447.  
  448.     /**
  449.      * Attempt to do SMTP authentication.
  450.      *
  451.      * @param string The userid to authenticate as.
  452.      * @param string The password to authenticate with.
  453.      * @param string The requested authentication method.  If none is
  454.      *                specified, the best supported method will be used.
  455.      *
  456.      * @return mixed Returns a PEAR_Error with an error message on any
  457.      *                kind of failure, or true on success.
  458.      * @access public
  459.      * @since  1.0
  460.      */
  461.     function auth($uid$pwd $method '')
  462.     {
  463.         if (version_compare(PHP_VERSION'5.1.0''>='&& isset($this->_esmtp['STARTTLS'])) {
  464.             if (PEAR::isError($result $this->_put('STARTTLS'))) {
  465.                 return $result;
  466.             }
  467.             if (PEAR::isError($result $this->_parseResponse(220))) {
  468.                 return $result;
  469.             }
  470.             if (PEAR::isError($result $this->_socket->enableCrypto(trueSTREAM_CRYPTO_METHOD_TLS_CLIENT))) {
  471.                 return $result;
  472.             elseif ($result !== true{
  473.                 return PEAR::raiseError('STARTTLS failed');
  474.             }
  475.  
  476.             /* Send EHLO again to recieve the AUTH string from the
  477.              * SMTP server. */
  478.             $this->_negotiate();
  479.         }
  480.  
  481.         if (empty($this->_esmtp['AUTH'])) {
  482.             return PEAR::raiseError('SMTP server does not support authentication');
  483.         }
  484.  
  485.         /* If no method has been specified, get the name of the best
  486.          * supported method advertised by the SMTP server. */
  487.         if (empty($method)) {
  488.             if (PEAR::isError($method $this->_getBestAuthMethod())) {
  489.                 /* Return the PEAR_Error object from _getBestAuthMethod(). */
  490.                 return $method;
  491.             }
  492.         else {
  493.             $method strtoupper($method);
  494.             if (!in_array($method$this->auth_methods)) {
  495.                 return PEAR::raiseError("$method is not a supported authentication method");
  496.             }
  497.         }
  498.  
  499.         switch ($method{
  500.         case 'DIGEST-MD5':
  501.             $result $this->_authDigest_MD5($uid$pwd);
  502.             break;
  503.  
  504.         case 'CRAM-MD5':
  505.             $result $this->_authCRAM_MD5($uid$pwd);
  506.             break;
  507.  
  508.         case 'LOGIN':
  509.             $result $this->_authLogin($uid$pwd);
  510.             break;
  511.  
  512.         case 'PLAIN':
  513.             $result $this->_authPlain($uid$pwd);
  514.             break;
  515.  
  516.         default:
  517.             $result = PEAR::raiseError("$method is not a supported authentication method");
  518.             break;
  519.         }
  520.  
  521.         /* If an error was encountered, return the PEAR_Error object. */
  522.         if (PEAR::isError($result)) {
  523.             return $result;
  524.         }
  525.  
  526.         return true;
  527.     }
  528.  
  529.     /**
  530.      * Authenticates the user using the DIGEST-MD5 method.
  531.      *
  532.      * @param string The userid to authenticate as.
  533.      * @param string The password to authenticate with.
  534.      *
  535.      * @return mixed Returns a PEAR_Error with an error message on any
  536.      *                kind of failure, or true on success.
  537.      * @access private
  538.      * @since  1.1.0
  539.      */
  540.     function _authDigest_MD5($uid$pwd)
  541.     {
  542.         if (PEAR::isError($error $this->_put('AUTH''DIGEST-MD5'))) {
  543.             return $error;
  544.         }
  545.         /* 334: Continue authentication request */
  546.         if (PEAR::isError($error $this->_parseResponse(334))) {
  547.             /* 503: Error: already authenticated */
  548.             if ($this->_code === 503{
  549.                 return true;
  550.             }
  551.             return $error;
  552.         }
  553.  
  554.         $challenge base64_decode($this->_arguments[0]);
  555.         $digest &Auth_SASL::factory('digestmd5');
  556.         $auth_str base64_encode($digest->getResponse($uid$pwd$challenge,
  557.                                                        $this->host"smtp"));
  558.  
  559.         if (PEAR::isError($error $this->_put($auth_str))) {
  560.             return $error;
  561.         }
  562.         /* 334: Continue authentication request */
  563.         if (PEAR::isError($error $this->_parseResponse(334))) {
  564.             return $error;
  565.         }
  566.  
  567.         /* We don't use the protocol's third step because SMTP doesn't
  568.          * allow subsequent authentication, so we just silently ignore
  569.          * it. */
  570.         if (PEAR::isError($error $this->_put(''))) {
  571.             return $error;
  572.         }
  573.         /* 235: Authentication successful */
  574.         if (PEAR::isError($error $this->_parseResponse(235))) {
  575.             return $error;
  576.         }
  577.     }
  578.  
  579.     /**
  580.      * Authenticates the user using the CRAM-MD5 method.
  581.      *
  582.      * @param string The userid to authenticate as.
  583.      * @param string The password to authenticate with.
  584.      *
  585.      * @return mixed Returns a PEAR_Error with an error message on any
  586.      *                kind of failure, or true on success.
  587.      * @access private
  588.      * @since  1.1.0
  589.      */
  590.     function _authCRAM_MD5($uid$pwd)
  591.     {
  592.         if (PEAR::isError($error $this->_put('AUTH''CRAM-MD5'))) {
  593.             return $error;
  594.         }
  595.         /* 334: Continue authentication request */
  596.         if (PEAR::isError($error $this->_parseResponse(334))) {
  597.             /* 503: Error: already authenticated */
  598.             if ($this->_code === 503{
  599.                 return true;
  600.             }
  601.             return $error;
  602.         }
  603.  
  604.         $challenge base64_decode($this->_arguments[0]);
  605.         $cram &Auth_SASL::factory('crammd5');
  606.         $auth_str base64_encode($cram->getResponse($uid$pwd$challenge));
  607.  
  608.         if (PEAR::isError($error $this->_put($auth_str))) {
  609.             return $error;
  610.         }
  611.  
  612.         /* 235: Authentication successful */
  613.         if (PEAR::isError($error $this->_parseResponse(235))) {
  614.             return $error;
  615.         }
  616.     }
  617.  
  618.     /**
  619.      * Authenticates the user using the LOGIN method.
  620.      *
  621.      * @param string The userid to authenticate as.
  622.      * @param string The password to authenticate with.
  623.      *
  624.      * @return mixed Returns a PEAR_Error with an error message on any
  625.      *                kind of failure, or true on success.
  626.      * @access private
  627.      * @since  1.1.0
  628.      */
  629.     function _authLogin($uid$pwd)
  630.     {
  631.         if (PEAR::isError($error $this->_put('AUTH''LOGIN'))) {
  632.             return $error;
  633.         }
  634.         /* 334: Continue authentication request */
  635.         if (PEAR::isError($error $this->_parseResponse(334))) {
  636.             /* 503: Error: already authenticated */
  637.             if ($this->_code === 503{
  638.                 return true;
  639.             }
  640.             return $error;
  641.         }
  642.  
  643.         if (PEAR::isError($error $this->_put(base64_encode($uid)))) {
  644.             return $error;
  645.         }
  646.         /* 334: Continue authentication request */
  647.         if (PEAR::isError($error $this->_parseResponse(334))) {
  648.             return $error;
  649.         }
  650.  
  651.         if (PEAR::isError($error $this->_put(base64_encode($pwd)))) {
  652.             return $error;
  653.         }
  654.  
  655.         /* 235: Authentication successful */
  656.         if (PEAR::isError($error $this->_parseResponse(235))) {
  657.             return $error;
  658.         }
  659.  
  660.         return true;
  661.     }
  662.  
  663.     /**
  664.      * Authenticates the user using the PLAIN method.
  665.      *
  666.      * @param string The userid to authenticate as.
  667.      * @param string The password to authenticate with.
  668.      *
  669.      * @return mixed Returns a PEAR_Error with an error message on any
  670.      *                kind of failure, or true on success.
  671.      * @access private
  672.      * @since  1.1.0
  673.      */
  674.     function _authPlain($uid$pwd)
  675.     {
  676.         if (PEAR::isError($error $this->_put('AUTH''PLAIN'))) {
  677.             return $error;
  678.         }
  679.         /* 334: Continue authentication request */
  680.         if (PEAR::isError($error $this->_parseResponse(334))) {
  681.             /* 503: Error: already authenticated */
  682.             if ($this->_code === 503{
  683.                 return true;
  684.             }
  685.             return $error;
  686.         }
  687.  
  688.         $auth_str base64_encode(chr(0$uid chr(0$pwd);
  689.  
  690.         if (PEAR::isError($error $this->_put($auth_str))) {
  691.             return $error;
  692.         }
  693.  
  694.         /* 235: Authentication successful */
  695.         if (PEAR::isError($error $this->_parseResponse(235))) {
  696.             return $error;
  697.         }
  698.  
  699.         return true;
  700.     }
  701.  
  702.     /**
  703.      * Send the HELO command.
  704.      *
  705.      * @param string The domain name to say we are.
  706.      *
  707.      * @return mixed Returns a PEAR_Error with an error message on any
  708.      *                kind of failure, or true on success.
  709.      * @access public
  710.      * @since  1.0
  711.      */
  712.     function helo($domain)
  713.     {
  714.         if (PEAR::isError($error $this->_put('HELO'$domain))) {
  715.             return $error;
  716.         }
  717.         if (PEAR::isError($error $this->_parseResponse(250))) {
  718.             return $error;
  719.         }
  720.  
  721.         return true;
  722.     }
  723.  
  724.     /**
  725.      * Return the list of SMTP service extensions advertised by the server.
  726.      *
  727.      * @return array The list of SMTP service extensions.
  728.      * @access public
  729.      * @since 1.3
  730.      */
  731.     function getServiceExtensions()
  732.     {
  733.         return $this->_esmtp;
  734.     }
  735.  
  736.     /**
  737.      * Send the MAIL FROM: command.
  738.      *
  739.      * @param string $sender    The sender (reverse path) to set.
  740.      * @param string $params    String containing additional MAIL parameters,
  741.      *                           such as the NOTIFY flags defined by RFC 1891
  742.      *                           or the VERP protocol.
  743.      *
  744.      *                           If $params is an array, only the 'verp' option
  745.      *                           is supported.  If 'verp' is true, the XVERP
  746.      *                           parameter is appended to the MAIL command.  If
  747.      *                           the 'verp' value is a string, the full
  748.      *                           XVERP=value parameter is appended.
  749.      *
  750.      * @return mixed Returns a PEAR_Error with an error message on any
  751.      *                kind of failure, or true on success.
  752.      * @access public
  753.      * @since  1.0
  754.      */
  755.     function mailFrom($sender$params = null)
  756.     {
  757.         $args = "FROM:<$sender>";
  758.  
  759.         /* Support the deprecated array form of $params. */
  760.         if (is_array($params&& isset($params['verp'])) {
  761.             /* XVERP */
  762.             if ($params['verp'=== true{
  763.                 $args .= ' XVERP';
  764.  
  765.             /* XVERP=something */
  766.             elseif (trim($params['verp'])) {
  767.                 $args .= ' XVERP=' $params['verp'];
  768.             }
  769.         elseif (is_string($params)) {
  770.             $args .= ' ' $params;
  771.         }
  772.  
  773.         if (PEAR::isError($error $this->_put('MAIL'$args))) {
  774.             return $error;
  775.         }
  776.         if (PEAR::isError($error $this->_parseResponse(250$this->pipelining))) {
  777.             return $error;
  778.         }
  779.  
  780.         return true;
  781.     }
  782.  
  783.     /**
  784.      * Send the RCPT TO: command.
  785.      *
  786.      * @param string $recipient The recipient (forward path) to add.
  787.      * @param string $params    String containing additional RCPT parameters,
  788.      *                           such as the NOTIFY flags defined by RFC 1891.
  789.      *
  790.      * @return mixed Returns a PEAR_Error with an error message on any
  791.      *                kind of failure, or true on success.
  792.      *
  793.      * @access public
  794.      * @since  1.0
  795.      */
  796.     function rcptTo($recipient$params = null)
  797.     {
  798.         $args = "TO:<$recipient>";
  799.         if (is_string($params)) {
  800.             $args .= ' ' $params;
  801.         }
  802.  
  803.         if (PEAR::isError($error $this->_put('RCPT'$args))) {
  804.             return $error;
  805.         }
  806.         if (PEAR::isError($error $this->_parseResponse(array(250251)$this->pipelining))) {
  807.             return $error;
  808.         }
  809.  
  810.         return true;
  811.     }
  812.  
  813.     /**
  814.      * Quote the data so that it meets SMTP standards.
  815.      *
  816.      * This is provided as a separate public function to facilitate
  817.      * easier overloading for the cases where it is desirable to
  818.      * customize the quoting behavior.
  819.      *
  820.      * @param string $data  The message text to quote. The string must be passed
  821.      *                       by reference, and the text will be modified in place.
  822.      *
  823.      * @access public
  824.      * @since  1.2
  825.      */
  826.     function quotedata(&$data)
  827.     {
  828.         /* Change Unix (\n) and Mac (\r) linefeeds into
  829.          * Internet-standard CRLF (\r\n) linefeeds. */
  830.         $data preg_replace(array('/(?<!\r)\n/','/\r(?!\n)/')"\r\n"$data);
  831.  
  832.         /* Because a single leading period (.) signifies an end to the
  833.          * data, legitimate leading periods need to be "doubled"
  834.          * (e.g. '..'). */
  835.         $data str_replace("\n.""\n.."$data);
  836.     }
  837.  
  838.     /**
  839.      * Send the DATA command.
  840.      *
  841.      * @param string $data  The message body to send.
  842.      *
  843.      * @return mixed Returns a PEAR_Error with an error message on any
  844.      *                kind of failure, or true on success.
  845.      * @access public
  846.      * @since  1.0
  847.      */
  848.     function data($data)
  849.     {
  850.         /* RFC 1870, section 3, subsection 3 states "a value of zero
  851.          * indicates that no fixed maximum message size is in force".
  852.          * Furthermore, it says that if "the parameter is omitted no
  853.          * information is conveyed about the server's fixed maximum
  854.          * message size". */
  855.         if (isset($this->_esmtp['SIZE']&& ($this->_esmtp['SIZE'> 0)) {
  856.             if (strlen($data>= $this->_esmtp['SIZE']{
  857.                 $this->disconnect();
  858.                 return PEAR::raiseError('Message size excedes the server limit');
  859.             }
  860.         }
  861.  
  862.         /* Quote the data based on the SMTP standards. */
  863.         $this->quotedata($data);
  864.  
  865.         if (PEAR::isError($error $this->_put('DATA'))) {
  866.             return $error;
  867.         }
  868.         if (PEAR::isError($error $this->_parseResponse(354))) {
  869.             return $error;
  870.         }
  871.  
  872.         if (PEAR::isError($result $this->_send($data "\r\n.\r\n"))) {
  873.             return $result;
  874.         }
  875.         if (PEAR::isError($error $this->_parseResponse(250$this->pipelining))) {
  876.             return $error;
  877.         }
  878.  
  879.         return true;
  880.     }
  881.  
  882.     /**
  883.      * Send the SEND FROM: command.
  884.      *
  885.      * @param string The reverse path to send.
  886.      *
  887.      * @return mixed Returns a PEAR_Error with an error message on any
  888.      *                kind of failure, or true on success.
  889.      * @access public
  890.      * @since  1.2.6
  891.      */
  892.     function sendFrom($path)
  893.     {
  894.         if (PEAR::isError($error $this->_put('SEND'"FROM:<$path>"))) {
  895.             return $error;
  896.         }
  897.         if (PEAR::isError($error $this->_parseResponse(250$this->pipelining))) {
  898.             return $error;
  899.         }
  900.  
  901.         return true;
  902.     }
  903.  
  904.     /**
  905.      * Backwards-compatibility wrapper for sendFrom().
  906.      *
  907.      * @param string The reverse path to send.
  908.      *
  909.      * @return mixed Returns a PEAR_Error with an error message on any
  910.      *                kind of failure, or true on success.
  911.      *
  912.      * @access      public
  913.      * @since       1.0
  914.      * @deprecated  1.2.6
  915.      */
  916.     function send_from($path)
  917.     {
  918.         return sendFrom($path);
  919.     }
  920.  
  921.     /**
  922.      * Send the SOML FROM: command.
  923.      *
  924.      * @param string The reverse path to send.
  925.      *
  926.      * @return mixed Returns a PEAR_Error with an error message on any
  927.      *                kind of failure, or true on success.
  928.      * @access public
  929.      * @since  1.2.6
  930.      */
  931.     function somlFrom($path)
  932.     {
  933.         if (PEAR::isError($error $this->_put('SOML'"FROM:<$path>"))) {
  934.             return $error;
  935.         }
  936.         if (PEAR::isError($error $this->_parseResponse(250$this->pipelining))) {
  937.             return $error;
  938.         }
  939.  
  940.         return true;
  941.     }
  942.  
  943.     /**
  944.      * Backwards-compatibility wrapper for somlFrom().
  945.      *
  946.      * @param string The reverse path to send.
  947.      *
  948.      * @return mixed Returns a PEAR_Error with an error message on any
  949.      *                kind of failure, or true on success.
  950.      *
  951.      * @access      public
  952.      * @since       1.0
  953.      * @deprecated  1.2.6
  954.      */
  955.     function soml_from($path)
  956.     {
  957.         return somlFrom($path);
  958.     }
  959.  
  960.     /**
  961.      * Send the SAML FROM: command.
  962.      *
  963.      * @param string The reverse path to send.
  964.      *
  965.      * @return mixed Returns a PEAR_Error with an error message on any
  966.      *                kind of failure, or true on success.
  967.      * @access public
  968.      * @since  1.2.6
  969.      */
  970.     function samlFrom($path)
  971.     {
  972.         if (PEAR::isError($error $this->_put('SAML'"FROM:<$path>"))) {
  973.             return $error;
  974.         }
  975.         if (PEAR::isError($error $this->_parseResponse(250$this->pipelining))) {
  976.             return $error;
  977.         }
  978.  
  979.         return true;
  980.     }
  981.  
  982.     /**
  983.      * Backwards-compatibility wrapper for samlFrom().
  984.      *
  985.      * @param string The reverse path to send.
  986.      *
  987.      * @return mixed Returns a PEAR_Error with an error message on any
  988.      *                kind of failure, or true on success.
  989.      *
  990.      * @access      public
  991.      * @since       1.0
  992.      * @deprecated  1.2.6
  993.      */
  994.     function saml_from($path)
  995.     {
  996.         return samlFrom($path);
  997.     }
  998.  
  999.     /**
  1000.      * Send the RSET command.
  1001.      *
  1002.      * @return mixed Returns a PEAR_Error with an error message on any
  1003.      *                kind of failure, or true on success.
  1004.      * @access public
  1005.      * @since  1.0
  1006.      */
  1007.     function rset()
  1008.     {
  1009.         if (PEAR::isError($error $this->_put('RSET'))) {
  1010.             return $error;
  1011.         }
  1012.         if (PEAR::isError($error $this->_parseResponse(250$this->pipelining))) {
  1013.             return $error;
  1014.         }
  1015.  
  1016.         return true;
  1017.     }
  1018.  
  1019.     /**
  1020.      * Send the VRFY command.
  1021.      *
  1022.      * @param string The string to verify
  1023.      *
  1024.      * @return mixed Returns a PEAR_Error with an error message on any
  1025.      *                kind of failure, or true on success.
  1026.      * @access public
  1027.      * @since  1.0
  1028.      */
  1029.     function vrfy($string)
  1030.     {
  1031.         /* Note: 251 is also a valid response code */
  1032.         if (PEAR::isError($error $this->_put('VRFY'$string))) {
  1033.             return $error;
  1034.         }
  1035.         if (PEAR::isError($error $this->_parseResponse(array(250252)))) {
  1036.             return $error;
  1037.         }
  1038.  
  1039.         return true;
  1040.     }
  1041.  
  1042.     /**
  1043.      * Send the NOOP command.
  1044.      *
  1045.      * @return mixed Returns a PEAR_Error with an error message on any
  1046.      *                kind of failure, or true on success.
  1047.      * @access public
  1048.      * @since  1.0
  1049.      */
  1050.     function noop()
  1051.     {
  1052.         if (PEAR::isError($error $this->_put('NOOP'))) {
  1053.             return $error;
  1054.         }
  1055.         if (PEAR::isError($error $this->_parseResponse(250))) {
  1056.             return $error;
  1057.         }
  1058.  
  1059.         return true;
  1060.     }
  1061.  
  1062.     /**
  1063.      * Backwards-compatibility method.  identifySender()'s functionality is
  1064.      * now handled internally.
  1065.      *
  1066.      * @return  boolean     This method always return true.
  1067.      *
  1068.      * @access  public
  1069.      * @since   1.0
  1070.      */
  1071.     function identifySender()
  1072.     {
  1073.         return true;
  1074.     }
  1075.  
  1076. }

Documentation generated on Mon, 11 Mar 2019 15:27:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.