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

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