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

Source for file smtp.php

Documentation is available at smtp.php

  1. <?php
  2. //
  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. // +----------------------------------------------------------------------+
  19.  
  20. /**
  21.  * SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class.
  22.  * @access public
  23.  * @package Mail
  24.  * @version $Revision: 1.23 $
  25.  */
  26. class Mail_smtp extends Mail {
  27.  
  28.     /**
  29.      * SMTP connection object.
  30.      *
  31.      * @var object 
  32.      * @access private
  33.      */
  34.     var $_smtp = null;
  35.  
  36.     /**
  37.      * The SMTP host to connect to.
  38.      * @var string 
  39.      */
  40.     var $host = 'localhost';
  41.  
  42.     /**
  43.      * The port the SMTP server is on.
  44.      * @var integer 
  45.      */
  46.     var $port = 25;
  47.  
  48.     /**
  49.      * Should SMTP authentication be used?
  50.      *
  51.      * This value may be set to true, false or the name of a specific
  52.      * authentication method.
  53.      *
  54.      * If the value is set to true, the Net_SMTP package will attempt to use
  55.      * the best authentication method advertised by the remote SMTP server.
  56.      *
  57.      * @var mixed 
  58.      */
  59.     var $auth = false;
  60.  
  61.     /**
  62.      * The username to use if the SMTP server requires authentication.
  63.      * @var string 
  64.      */
  65.     var $username = '';
  66.  
  67.     /**
  68.      * The password to use if the SMTP server requires authentication.
  69.      * @var string 
  70.      */
  71.     var $password = '';
  72.  
  73.     /**
  74.      * Hostname or domain that will be sent to the remote SMTP server in the
  75.      * HELO / EHLO message.
  76.      *
  77.      * @var string 
  78.      */
  79.     var $localhost = 'localhost';
  80.  
  81.     /**
  82.      * SMTP connection timeout value.  NULL indicates no timeout.
  83.      *
  84.      * @var integer 
  85.      */
  86.     var $timeout = null;
  87.  
  88.     /**
  89.      * Whether to use VERP or not. If not a boolean, the string value
  90.      * will be used as the VERP separators.
  91.      *
  92.      * @var mixed boolean or string
  93.      */
  94.     var $verp = false;
  95.  
  96.     /**
  97.      * Turn on Net_SMTP debugging?
  98.      *
  99.      * @var boolean $debug 
  100.      */
  101.     var $debug = false;
  102.  
  103.     /**
  104.      * Indicates whether or not the SMTP connection should persist over
  105.      * multiple calls to the send() method.
  106.      *
  107.      * @var boolean 
  108.      */
  109.     var $persist = false;
  110.  
  111.     /**
  112.      * Constructor.
  113.      *
  114.      * Instantiates a new Mail_smtp:: object based on the parameters
  115.      * passed in. It looks for the following parameters:
  116.      *     host        The server to connect to. Defaults to localhost.
  117.      *     port        The port to connect to. Defaults to 25.
  118.      *     auth        SMTP authentication.  Defaults to none.
  119.      *     username    The username to use for SMTP auth. No default.
  120.      *     password    The password to use for SMTP auth. No default.
  121.      *     localhost   The local hostname / domain. Defaults to localhost.
  122.      *     timeout     The SMTP connection timeout. Defaults to none.
  123.      *     verp        Whether to use VERP or not. Defaults to false.
  124.      *     debug       Activate SMTP debug mode? Defaults to false.
  125.      *     persist     Should the SMTP connection persist?
  126.      *
  127.      * If a parameter is present in the $params array, it replaces the
  128.      * default.
  129.      *
  130.      * @param array Hash containing any parameters different from the
  131.      *               defaults.
  132.      * @access public
  133.      */
  134.     function Mail_smtp($params)
  135.     {
  136.         if (isset($params['host'])) $this->host = $params['host'];
  137.         if (isset($params['port'])) $this->port = $params['port'];
  138.         if (isset($params['auth'])) $this->auth = $params['auth'];
  139.         if (isset($params['username'])) $this->username = $params['username'];
  140.         if (isset($params['password'])) $this->password = $params['password'];
  141.         if (isset($params['localhost'])) $this->localhost = $params['localhost'];
  142.         if (isset($params['timeout'])) $this->timeout = $params['timeout'];
  143.         if (isset($params['verp'])) $this->verp = $params['verp'];
  144.         if (isset($params['debug'])) $this->debug = (boolean)$params['debug'];
  145.         if (isset($params['persist'])) $this->persist = (boolean)$params['persist'];
  146.  
  147.         register_shutdown_function(array(&$this'_Mail_smtp'));
  148.     }
  149.  
  150.     /**
  151.      * Destructor implementation to ensure that we disconnect from any
  152.      * potentially-alive persistent SMTP connections.
  153.      */
  154.     function _Mail_smtp()
  155.     {
  156.         if (is_object($this->_smtp)) {
  157.             $this->_smtp->disconnect();
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * Implements Mail::send() function using SMTP.
  163.      *
  164.      * @param mixed $recipients Either a comma-seperated list of recipients
  165.      *               (RFC822 compliant), or an array of recipients,
  166.      *               each RFC822 valid. This may contain recipients not
  167.      *               specified in the headers, for Bcc:, resending
  168.      *               messages, etc.
  169.      *
  170.      * @param array $headers The array of headers to send with the mail, in an
  171.      *               associative array, where the array key is the
  172.      *               header name (e.g., 'Subject'), and the array value
  173.      *               is the header value (e.g., 'test'). The header
  174.      *               produced from those values would be 'Subject:
  175.      *               test'.
  176.      *
  177.      * @param string $body The full text of the message body, including any
  178.      *                Mime parts, etc.
  179.      *
  180.      * @return mixed Returns true on success, or a PEAR_Error
  181.      *                containing a descriptive error message on
  182.      *                failure.
  183.      * @access public
  184.      */
  185.     function send($recipients$headers$body)
  186.     {
  187.         include_once 'Net/SMTP.php';
  188.  
  189.         /* If we don't already have an SMTP object, create one. */
  190.         if (is_object($this->_smtp=== false{
  191.             $this->_smtp =new Net_SMTP($this->host$this->port,
  192.                                          $this->localhost);
  193.  
  194.             /* If we still don't have an SMTP object at this point, fail. */
  195.             if (is_object($this->_smtp=== false{
  196.                 return PEAR::raiseError('Failed to create a Net_SMTP object');
  197.             }
  198.  
  199.             /* Configure the SMTP connection. */
  200.             if ($this->debug{
  201.                 $this->_smtp->setDebug(true);
  202.             }
  203.  
  204.             /* Attempt to connect to the configured SMTP server. */
  205.             if (PEAR::isError($res $this->_smtp->connect($this->timeout))) {
  206.                 $error $this->_error('Failed to connect to ' .
  207.                                        $this->host . ':' $this->port,
  208.                                        $res);
  209.                 return PEAR::raiseError($error);
  210.             }
  211.  
  212.             /* Attempt to authenticate if authentication has been enabled. */
  213.             if ($this->auth{
  214.                 $method is_string($this->auth$this->auth : '';
  215.  
  216.                 if (PEAR::isError($res $this->_smtp->auth($this->username,
  217.                                                             $this->password,
  218.                                                             $method))) {
  219.                     $error $this->_error("$method authentication failure",
  220.                                            $res);
  221.                     return PEAR::raiseError($error);
  222.                 }
  223.             }
  224.         }
  225.  
  226.         $headerElements $this->prepareHeaders($headers);
  227.         if (PEAR::isError($headerElements)) {
  228.             return $headerElements;
  229.         }
  230.         list($from$textHeaders$headerElements;
  231.  
  232.         /* Since few MTAs are going to allow this header to be forged
  233.          * unless it's in the MAIL FROM: exchange, we'll use
  234.          * Return-Path instead of From: if it's set. */
  235.         if (!empty($headers['Return-Path'])) {
  236.             $from $headers['Return-Path'];
  237.         }
  238.  
  239.         if (!isset($from)) {
  240.             return PEAR::raiseError('No From: address has been provided');
  241.         }
  242.  
  243.         $args['verp'$this->verp;
  244.         if (PEAR::isError($res $this->_smtp->mailFrom($from$args))) {
  245.             $error $this->_error("Failed to set sender: $from"$res);
  246.             return PEAR::raiseError($error);
  247.         }
  248.  
  249.         $recipients $this->parseRecipients($recipients);
  250.         if (PEAR::isError($recipients)) {
  251.             return $recipients;
  252.         }
  253.  
  254.         foreach ($recipients as $recipient{
  255.             if (PEAR::isError($res $this->_smtp->rcptTo($recipient))) {
  256.                 $error $this->_error("Failed to add recipient: $recipient",
  257.                                        $res);
  258.                 return PEAR::raiseError($error);
  259.             }
  260.         }
  261.  
  262.         /* Send the message's headers and the body as SMTP data. */
  263.         if (PEAR::isError($res $this->_smtp->data("$textHeaders\r\n$body"))) {
  264.             $error $this->_error('Failed to send data'$res);
  265.             return PEAR::raiseError($error);
  266.         }
  267.  
  268.         /* If persistent connections are disabled, destroy our SMTP object. */
  269.         if ($this->persist === false{
  270.             $this->_smtp->disconnect();
  271.             $this->_smtp = null;
  272.         }
  273.  
  274.         return true;
  275.     }
  276.  
  277.     /**
  278.      * Build a standardized string describing the current SMTP error.
  279.      *
  280.      * @param string $text  Custom string describing the error context.
  281.      * @param object $error Reference to the current PEAR_Error object.
  282.      *
  283.      * @return string       A string describing the current SMTP error.
  284.      *
  285.      * @since  1.1.7
  286.      * @access private
  287.      */
  288.     function _error($text&$error)
  289.     {
  290.         /* Split the SMTP response into a code and a response string. */
  291.         list($code$response$this->_smtp->getResponse();
  292.  
  293.         /* Build our standardized error string. */
  294.         $msg $text;
  295.         $msg .= ' [SMTP: ' $error->getMessage();
  296.         $msg .= " (code: $code, response: $response)]";
  297.  
  298.         return $msg;
  299.     }
  300. }

Documentation generated on Mon, 11 Mar 2019 14:17:01 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.