Mail
[ class tree: Mail ] [ index: Mail ] [ all elements ]

Source for file sendmail.php

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

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