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 / sendmail.php < prev    next >
Encoding:
PHP Script  |  2001-08-10  |  5.1 KB  |  127 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. require_once 'Mail.php';
  20.  
  21. /**
  22.  * Sendmail implementation of the PEAR Mail:: interface.
  23.  * @access public
  24.  * @package Mail
  25.  * @version $Revision: 1.8 $
  26.  */
  27. class Mail_sendmail extends Mail {
  28.     
  29.     /**
  30.      * The location of the sendmail or sendmail wrapper binary on the
  31.      * filesystem.
  32.      * @var string
  33.      */
  34.     var $sendmail_path = '/usr/sbin/sendmail';
  35.     
  36.     /**
  37.      * Any extra command-line parameters to pass to the sendmail or
  38.      * sendmail wrapper binary.
  39.      * @var string
  40.      */
  41.     var $sendmail_args = '';
  42.     
  43.     /**
  44.      * Constructor.
  45.      * 
  46.      * Instantiates a new Mail_sendmail:: object based on the parameters
  47.      * passed in. It looks for the following parameters:
  48.      *     sendmail_path    The location of the sendmail binary on the
  49.      *                      filesystem. Defaults to '/usr/sbin/sendmail'.
  50.      *
  51.      *     sendmail_args    Any extra parameters to pass to the sendmail
  52.      *                      or sendmail wrapper binary.
  53.      *
  54.      * If a parameter is present in the $params array, it replaces the
  55.      * default.
  56.      *
  57.      * @param array $params Hash containing any parameters different from the
  58.      *              defaults.
  59.      * @access public
  60.      */    
  61.     function Mail_sendmail($params)
  62.     {
  63.         if (isset($params['sendmail_path'])) $this->sendmail_path = $params['sendmail_path'];
  64.         if (isset($params['sendmail_args'])) $this->sendmail_args = $params['sendmail_args'];
  65.     }
  66.     
  67.     /**
  68.      * Implements Mail::send() function using the sendmail
  69.      * command-line binary.
  70.      * 
  71.      * @param mixed $recipients Either a comma-seperated list of recipients
  72.      *              (RFC822 compliant), or an array of recipients,
  73.      *              each RFC822 valid. This may contain recipients not
  74.      *              specified in the headers, for Bcc:, resending
  75.      *              messages, etc.
  76.      *
  77.      * @param array $headers The array of headers to send with the mail, in an
  78.      *              associative array, where the array key is the
  79.      *              header name (ie, 'Subject'), and the array value
  80.      *              is the header value (ie, 'test'). The header
  81.      *              produced from those values would be 'Subject:
  82.      *              test'.
  83.      *
  84.      * @param string $body The full text of the message body, including any
  85.      *               Mime parts, etc.
  86.      *
  87.      * @return mixed Returns true on success, or a PEAR_Error
  88.      *               containing a descriptive error message on
  89.      *               failure.
  90.      * @access public
  91.      */    
  92.     function send($recipients, $headers, $body)
  93.     {
  94.         $recipients = escapeShellCmd(implode(' ', $this->parseRecipients($recipients)));
  95.         
  96.         list($from, $text_headers) = $this->prepareHeaders($headers);
  97.         if (!isset($from)) {
  98.             return new PEAR_Error('No from address given.');
  99.         } elseif (strstr($from, ' ') ||
  100.                   strstr($from, ';') ||
  101.                   strstr($from, '&') ||
  102.                   strstr($from, '`')) {
  103.             return new PEAR_Error('From address specified with dangerous characters.');
  104.         }
  105.         
  106.         $result = 0;
  107.         if (@is_executable($this->sendmail_path)) {
  108.             $from = escapeShellCmd($from);
  109.             $mail = popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f$from -- $recipients", 'w');
  110.             fputs($mail, $text_headers);
  111.             fputs($mail, "\n");  // newline to end the headers section
  112.             fputs($mail, $body);
  113.             $result = pclose($mail) >> 8 & 0xFF; // need to shift the pclose result to get the exit code
  114.         } else {
  115.             return new PEAR_Error('sendmail [' . $this->sendmail_path . '] not executable');
  116.         }
  117.         
  118.         if ($result != 0) {
  119.             return new PEAR_Error('sendmail returned error code ' . $result);
  120.         }
  121.         
  122.         return true;
  123.     }
  124.     
  125. }
  126. ?>
  127.