home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Vyzkuste / phptriad / phptriad2-2-1.exe / php / pear / Mail.php < prev    next >
Encoding:
PHP Script  |  2001-08-09  |  7.3 KB  |  186 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 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. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Mail.php,v 1.14 2001/08/09 08:06:52 cox Exp $
  20.  
  21. require_once 'PEAR.php';
  22.  
  23. /**
  24.  * PEAR's Mail:: interface. Defines the interface for implementing
  25.  * mailers under the PEAR hierarchy, and provides supporting functions
  26.  * useful in multiple mailer backends.
  27.  *
  28.  * @access public
  29.  * @version $Revision: 1.14 $
  30.  * @package Mail
  31.  */
  32. class Mail extends PEAR
  33. {
  34.     /**
  35.      * Provides an interface for generating Mail:: objects of various
  36.      * types
  37.      *
  38.      * @param string $driver The kind of Mail:: object to instantiate.
  39.      * @param array  $params The parameters to pass to the Mail:: object.
  40.      * @return object Mail a instance of the driver class or if fails a PEAR Error
  41.      * @access public
  42.      */
  43.     function factory($driver, $params = array())
  44.     {
  45.         $driver = strtolower($driver);
  46.         @include_once 'Mail/' . $driver . '.php';
  47.         $class = 'Mail_' . $driver;
  48.         if (class_exists($class)) {
  49.             return new $class($params);
  50.         } else {
  51.             return $this->raiseError('Unable to find class for driver ' . $driver);
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Implements Mail::send() function using php's built-in mail()
  57.      * command.
  58.      *
  59.      * @param mixed $recipients Either a comma-seperated list of recipients
  60.      *              (RFC822 compliant), or an array of recipients,
  61.      *              each RFC822 valid. This may contain recipients not
  62.      *              specified in the headers, for Bcc:, resending
  63.      *              messages, etc.
  64.      *
  65.      * @param array $headers The array of headers to send with the mail, in an
  66.      *              associative array, where the array key is the
  67.      *              header name (ie, 'Subject'), and the array value
  68.      *              is the header value (ie, 'test'). The header
  69.      *              produced from those values would be 'Subject:
  70.      *              test'.
  71.      *
  72.      * @param string $body The full text of the message body, including any
  73.      *               Mime parts, etc.
  74.      *
  75.      * @return mixed Returns true on success, or a PEAR_Error
  76.      *               containing a descriptive error message on
  77.      *               failure.
  78.      * @access public
  79.      * @deprecated use Mail_mail::send instead
  80.      */
  81.     function send($recipients, $headers, $body)
  82.     {
  83.         // if we're passed an array of recipients, implode it.
  84.         if (is_array($recipients)) {
  85.             $recipients = implode(', ', $recipients);
  86.         }
  87.  
  88.         // get the Subject out of the headers array so that we can
  89.         // pass it as a seperate argument to mail().
  90.         $subject = '';
  91.         if (isset($headers['Subject'])) {
  92.             $subject = $headers['Subject'];
  93.             unset($headers['Subject']);
  94.         }
  95.  
  96.         // flatten the headers out.
  97.         list(,$text_headers) = Mail::prepareHeaders($headers);
  98.  
  99.         return mail($recipients, $subject, $body, $text_headers);
  100.  
  101.     }
  102.  
  103.     /**
  104.      * Take an array of mail headers and return a string containing
  105.      * text usable in sending a message.
  106.      *
  107.      * @param array $headers The array of headers to prepare, in an associative
  108.      *              array, where the array key is the header name (ie,
  109.      *              'Subject'), and the array value is the header
  110.      *              value (ie, 'test'). The header produced from those
  111.      *              values would be 'Subject: test'.
  112.      *
  113.      * @return mixed Returns false if it encounters a bad address,
  114.      *               otherwise returns an array containing two
  115.      *               elements: Any From: address found in the headers,
  116.      *               and the plain text version of the headers.
  117.      * @access private
  118.      */
  119.     function prepareHeaders($headers)
  120.     {
  121.         // Look out for the From: value to use along the way.
  122.         $text_headers = '';  // text representation of headers
  123.         $from = null;
  124.  
  125.         foreach ($headers as $key => $val) {
  126.             if ($key == 'From') {
  127.                 include_once 'Mail/RFC822.php';
  128.  
  129.                 $from_arr = Mail_RFC822::parseAddressList($val, 'localhost', false);
  130.                 $from = $from_arr[0]->mailbox . '@' . $from_arr[0]->host;
  131.                 if (strstr($from, ' ')) {
  132.                     // Reject outright envelope From addresses with spaces.
  133.                     return false;
  134.                 }
  135.                 $text_headers .= $key . ': ' . $val . "\n";
  136.             } else if ($key == 'Received') {
  137.                 // put Received: headers at the top, since Receieved:
  138.                 // after Subject: in the header order is somtimes used
  139.                 // as a spam trap.
  140.                 $text_headers = $key . ': ' . $val . "\n" . $text_headers;
  141.             } else {
  142.                 $text_headers .= $key . ': ' . $val . "\n";
  143.             }
  144.         }
  145.  
  146.         return array($from, $text_headers);
  147.     }
  148.  
  149.     /**
  150.      * Take a set of recipients and parse them, returning an array of
  151.      * bare addresses (forward paths) that can be passed to sendmail
  152.      * or an smtp server with the rcpt to: command.
  153.      *
  154.      * @param mixed Either a comma-seperated list of recipients
  155.      *              (RFC822 compliant), or an array of recipients,
  156.      *              each RFC822 valid.
  157.      *
  158.      * @return array An array of forward paths (bare addresses).
  159.      * @access private
  160.      */
  161.     function parseRecipients($recipients)
  162.     {
  163.         include_once 'Mail/RFC822.php';
  164.  
  165.         // if we're passed an array, assume addresses are valid and
  166.         // implode them before parsing.
  167.         if (is_array($recipients)) {
  168.             $recipients = implode(', ', $recipients);
  169.         }
  170.  
  171.         // Parse recipients, leaving out all personal info. This is
  172.         // for smtp recipients, etc. All relevant personal information
  173.         // should already be in the headers.
  174.         $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
  175.         $recipients = array();
  176.         if (is_array($addresses)) {
  177.             foreach ($addresses as $ob) {
  178.                 $recipients[] = $ob->mailbox . '@' . $ob->host;
  179.             }
  180.         }
  181.  
  182.         return $recipients;
  183.     }
  184.  
  185. }
  186. ?>