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.18 $
  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.      * Whether to use VERP or not. If not a boolean, the string value
  76.      * will be used as the VERP separators.
  77.      *
  78.      * @var mixed boolean or string
  79.      */
  80.     var $verp = false;
  81.  
  82.     /**
  83.      * Turn on Net_SMTP debugging?
  84.      *
  85.      * @var boolean $debug 
  86.      */
  87.     var $debug = false;
  88.  
  89.     /**
  90.      * Constructor.
  91.      *
  92.      * Instantiates a new Mail_smtp:: object based on the parameters
  93.      * passed in. It looks for the following parameters:
  94.      *     host        The server to connect to. Defaults to localhost.
  95.      *     port        The port to connect to. Defaults to 25.
  96.      *     auth        SMTP authentication.  Defaults to none.
  97.      *     username    The username to use for SMTP auth. No default.
  98.      *     password    The password to use for SMTP auth. No default.
  99.      *     localhost   The local hostname / domain. Defaults to localhost.
  100.      *     verp        Whether to use VERP or not. Defaults to false.
  101.      *     debug       Activate SMTP debug mode? Defaults to false.
  102.      *
  103.      * If a parameter is present in the $params array, it replaces the
  104.      * default.
  105.      *
  106.      * @param array Hash containing any parameters different from the
  107.      *               defaults.
  108.      * @access public
  109.      */
  110.     function Mail_smtp($params)
  111.     {
  112.         if (isset($params['host'])) $this->host = $params['host'];
  113.         if (isset($params['port'])) $this->port = $params['port'];
  114.         if (isset($params['auth'])) $this->auth = $params['auth'];
  115.         if (isset($params['username'])) $this->username = $params['username'];
  116.         if (isset($params['password'])) $this->password = $params['password'];
  117.         if (isset($params['localhost'])) $this->localhost = $params['localhost'];
  118.         if (isset($params['verp'])) $this->verp = $params['verp'];
  119.         if (isset($params['debug'])) $this->debug = (boolean)$params['debug'];
  120.     }
  121.  
  122.     /**
  123.      * Implements Mail::send() function using SMTP.
  124.      *
  125.      * @param mixed $recipients Either a comma-seperated list of recipients
  126.      *               (RFC822 compliant), or an array of recipients,
  127.      *               each RFC822 valid. This may contain recipients not
  128.      *               specified in the headers, for Bcc:, resending
  129.      *               messages, etc.
  130.      *
  131.      * @param array $headers The array of headers to send with the mail, in an
  132.      *               associative array, where the array key is the
  133.      *               header name (e.g., 'Subject'), and the array value
  134.      *               is the header value (e.g., 'test'). The header
  135.      *               produced from those values would be 'Subject:
  136.      *               test'.
  137.      *
  138.      * @param string $body The full text of the message body, including any
  139.      *                Mime parts, etc.
  140.      *
  141.      * @return mixed Returns true on success, or a PEAR_Error
  142.      *                containing a descriptive error message on
  143.      *                failure.
  144.      * @access public
  145.      */
  146.     function send($recipients$headers$body)
  147.     {
  148.         include_once 'Net/SMTP.php';
  149.  
  150.         if (!($smtp &new Net_SMTP($this->host$this->port$this->localhost))) {
  151.             return PEAR::raiseError('unable to instantiate Net_SMTP object');
  152.         }
  153.  
  154.         if ($this->debug{
  155.             $smtp->setDebug(true);
  156.         }
  157.  
  158.         if (PEAR::isError($smtp->connect())) {
  159.             return PEAR::raiseError('unable to connect to smtp server ' .
  160.                                     $this->host . ':' $this->port);
  161.         }
  162.  
  163.         if ($this->auth{
  164.             $method is_string($this->auth$this->auth : '';
  165.  
  166.             if (PEAR::isError($smtp->auth($this->username$this->password,
  167.                               $method))) {
  168.                 return PEAR::raiseError('unable to authenticate to smtp server');
  169.             }
  170.         }
  171.  
  172.         $headerElements $this->prepareHeaders($headers);
  173.         if (PEAR::isError($headerElements)) {
  174.             return $headerElements;
  175.         }
  176.         list($from$text_headers$headerElements;
  177.  
  178.         /* Since few MTAs are going to allow this header to be forged
  179.          * unless it's in the MAIL FROM: exchange, we'll use
  180.          * Return-Path instead of From: if it's set. */
  181.         if (!empty($headers['Return-Path'])) {
  182.             $from $headers['Return-Path'];
  183.         }
  184.  
  185.         if (!isset($from)) {
  186.             return PEAR::raiseError('No from address given');
  187.         }
  188.  
  189.         $args['verp'$this->verp;
  190.         if (PEAR::isError($smtp->mailFrom($from$args))) {
  191.             return PEAR::raiseError('unable to set sender to [' $from ']');
  192.         }
  193.  
  194.         $recipients $this->parseRecipients($recipients);
  195.         if (PEAR::isError($recipients)) {
  196.             return $recipients;
  197.         }
  198.  
  199.         foreach ($recipients as $recipient{
  200.             if (PEAR::isError($res $smtp->rcptTo($recipient))) {
  201.                 return PEAR::raiseError('unable to add recipient [' .
  202.                                         $recipient ']: ' $res->getMessage());
  203.             }
  204.         }
  205.  
  206.         if (PEAR::isError($smtp->data($text_headers "\r\n" $body))) {
  207.             return PEAR::raiseError('unable to send data');
  208.         }
  209.  
  210.         $smtp->disconnect();
  211.         return true;
  212.     }
  213.  
  214. }

Documentation generated on Mon, 11 Mar 2019 13:55:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.