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 PEAR
  22.  * Net_SMTP:: class.
  23.  * @access public
  24.  * @package Mail
  25.  * @version $Revision: 1.20 $
  26.  */
  27. class Mail_smtp extends Mail {
  28.  
  29.     /**
  30.      * The SMTP host to connect to.
  31.      * @var string 
  32.      */
  33.     var $host = 'localhost';
  34.  
  35.     /**
  36.      * The port the SMTP server is on.
  37.      * @var integer 
  38.      */
  39.     var $port = 25;
  40.  
  41.     /**
  42.      * Should SMTP authentication be used?
  43.      *
  44.      * This value may be set to true, false or the name of a specific
  45.      * authentication method.
  46.      *
  47.      * If the value is set to true, the Net_SMTP package will attempt to use
  48.      * the best authentication method advertised by the remote SMTP server.
  49.      *
  50.      * @var mixed 
  51.      */
  52.     var $auth = false;
  53.  
  54.     /**
  55.      * The username to use if the SMTP server requires authentication.
  56.      * @var string 
  57.      */
  58.     var $username = '';
  59.  
  60.     /**
  61.      * The password to use if the SMTP server requires authentication.
  62.      * @var string 
  63.      */
  64.     var $password = '';
  65.  
  66.     /**
  67.      * Hostname or domain that will be sent to the remote SMTP server in the
  68.      * HELO / EHLO message.
  69.      *
  70.      * @var string 
  71.      */
  72.     var $localhost = 'localhost';
  73.  
  74.     /**
  75.      * SMTP connection timeout value.  NULL indicates no timeout.
  76.      *
  77.      * @var integer 
  78.      */
  79.     var $timeout = null;
  80.  
  81.     /**
  82.      * Whether to use VERP or not. If not a boolean, the string value
  83.      * will be used as the VERP separators.
  84.      *
  85.      * @var mixed boolean or string
  86.      */
  87.     var $verp = false;
  88.  
  89.     /**
  90.      * Turn on Net_SMTP debugging?
  91.      *
  92.      * @var boolean $debug 
  93.      */
  94.     var $debug = false;
  95.  
  96.     /**
  97.      * Constructor.
  98.      *
  99.      * Instantiates a new Mail_smtp:: object based on the parameters
  100.      * passed in. It looks for the following parameters:
  101.      *     host        The server to connect to. Defaults to localhost.
  102.      *     port        The port to connect to. Defaults to 25.
  103.      *     auth        SMTP authentication.  Defaults to none.
  104.      *     username    The username to use for SMTP auth. No default.
  105.      *     password    The password to use for SMTP auth. No default.
  106.      *     localhost   The local hostname / domain. Defaults to localhost.
  107.      *     timeout     The SMTP connection timeout. Defaults to none.
  108.      *     verp        Whether to use VERP or not. Defaults to false.
  109.      *     debug       Activate SMTP debug mode? Defaults to false.
  110.      *
  111.      * If a parameter is present in the $params array, it replaces the
  112.      * default.
  113.      *
  114.      * @param array Hash containing any parameters different from the
  115.      *               defaults.
  116.      * @access public
  117.      */
  118.     function Mail_smtp($params)
  119.     {
  120.         if (isset($params['host'])) $this->host = $params['host'];
  121.         if (isset($params['port'])) $this->port = $params['port'];
  122.         if (isset($params['auth'])) $this->auth = $params['auth'];
  123.         if (isset($params['username'])) $this->username = $params['username'];
  124.         if (isset($params['password'])) $this->password = $params['password'];
  125.         if (isset($params['localhost'])) $this->localhost = $params['localhost'];
  126.         if (isset($params['timeout'])) $this->timeout = $params['timeout'];
  127.         if (isset($params['verp'])) $this->verp = $params['verp'];
  128.         if (isset($params['debug'])) $this->debug = (boolean)$params['debug'];
  129.     }
  130.  
  131.     /**
  132.      * Implements Mail::send() function using SMTP.
  133.      *
  134.      * @param mixed $recipients Either a comma-seperated list of recipients
  135.      *               (RFC822 compliant), or an array of recipients,
  136.      *               each RFC822 valid. This may contain recipients not
  137.      *               specified in the headers, for Bcc:, resending
  138.      *               messages, etc.
  139.      *
  140.      * @param array $headers The array of headers to send with the mail, in an
  141.      *               associative array, where the array key is the
  142.      *               header name (e.g., 'Subject'), and the array value
  143.      *               is the header value (e.g., 'test'). The header
  144.      *               produced from those values would be 'Subject:
  145.      *               test'.
  146.      *
  147.      * @param string $body The full text of the message body, including any
  148.      *                Mime parts, etc.
  149.      *
  150.      * @return mixed Returns true on success, or a PEAR_Error
  151.      *                containing a descriptive error message on
  152.      *                failure.
  153.      * @access public
  154.      */
  155.     function send($recipients$headers$body)
  156.     {
  157.         include_once 'Net/SMTP.php';
  158.  
  159.         if (!($smtp &new Net_SMTP($this->host$this->port$this->localhost))) {
  160.             return PEAR::raiseError('unable to instantiate Net_SMTP object');
  161.         }
  162.  
  163.         if ($this->debug{
  164.             $smtp->setDebug(true);
  165.         }
  166.  
  167.         if (PEAR::isError($smtp->connect($this->timeout))) {
  168.             return PEAR::raiseError('unable to connect to smtp server ' .
  169.                                     $this->host . ':' $this->port);
  170.         }
  171.  
  172.         if ($this->auth{
  173.             $method is_string($this->auth$this->auth : '';
  174.  
  175.             if (PEAR::isError($smtp->auth($this->username$this->password,
  176.                               $method))) {
  177.                 return PEAR::raiseError('unable to authenticate to smtp server');
  178.             }
  179.         }
  180.  
  181.         $headerElements $this->prepareHeaders($headers);
  182.         if (PEAR::isError($headerElements)) {
  183.             return $headerElements;
  184.         }
  185.         list($from$text_headers$headerElements;
  186.  
  187.         /* Since few MTAs are going to allow this header to be forged
  188.          * unless it's in the MAIL FROM: exchange, we'll use
  189.          * Return-Path instead of From: if it's set. */
  190.         if (!empty($headers['Return-Path'])) {
  191.             $from $headers['Return-Path'];
  192.         }
  193.  
  194.         if (!isset($from)) {
  195.             return PEAR::raiseError('No from address given');
  196.         }
  197.  
  198.         $args['verp'$this->verp;
  199.         if (PEAR::isError($smtp->mailFrom($from$args))) {
  200.             return PEAR::raiseError('unable to set sender to [' $from ']');
  201.         }
  202.  
  203.         $recipients $this->parseRecipients($recipients);
  204.         if (PEAR::isError($recipients)) {
  205.             return $recipients;
  206.         }
  207.  
  208.         foreach ($recipients as $recipient{
  209.             if (PEAR::isError($res $smtp->rcptTo($recipient))) {
  210.                 return PEAR::raiseError('unable to add recipient [' .
  211.                                         $recipient ']: ' $res->getMessage());
  212.             }
  213.         }
  214.  
  215.         if (PEAR::isError($smtp->data($text_headers "\r\n" $body))) {
  216.             return PEAR::raiseError('unable to send data');
  217.         }
  218.  
  219.         $smtp->disconnect();
  220.         return true;
  221.     }
  222.  
  223. }

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