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.13 $
  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.         if (defined(PHP_EOL)) {
  70.             $this->sep = PHP_EOL;
  71.         else {
  72.             $this->sep = (strpos(PHP_OS'WIN'=== false"\n" "\r\n";
  73.         }
  74.     }
  75.  
  76.     /**
  77.      * Implements Mail::send() function using the sendmail
  78.      * command-line binary.
  79.      *
  80.      * @param mixed $recipients Either a comma-seperated list of recipients
  81.      *               (RFC822 compliant), or an array of recipients,
  82.      *               each RFC822 valid. This may contain recipients not
  83.      *               specified in the headers, for Bcc:, resending
  84.      *               messages, etc.
  85.      *
  86.      * @param array $headers The array of headers to send with the mail, in an
  87.      *               associative array, where the array key is the
  88.      *               header name (ie, 'Subject'), and the array value
  89.      *               is the header value (ie, 'test'). The header
  90.      *               produced from those values would be 'Subject:
  91.      *               test'.
  92.      *
  93.      * @param string $body The full text of the message body, including any
  94.      *                Mime parts, etc.
  95.      *
  96.      * @return mixed Returns true on success, or a PEAR_Error
  97.      *                containing a descriptive error message on
  98.      *                failure.
  99.      * @access public
  100.      */
  101.     function send($recipients$headers$body)
  102.     {
  103.         $recipients $this->parseRecipients($recipients);
  104.         if (PEAR::isError($recipients)) {
  105.             return $recipients;
  106.         }
  107.         $recipients = escapeShellCmd(implode(' '$recipients));
  108.  
  109.         $headerElements $this->prepareHeaders($headers);
  110.         if (PEAR::isError($headerElements)) {
  111.             return $headerElements;
  112.         }
  113.         list($from$text_headers$headerElements;
  114.  
  115.         if (!isset($from)) {
  116.             return PEAR::raiseError('No from address given.');
  117.         elseif (strpos($from' '!== false ||
  118.                   strpos($from';'!== false ||
  119.                   strpos($from'&'!== false ||
  120.                   strpos($from'`'!== false{
  121.             return PEAR::raiseError('From address specified with dangerous characters.');
  122.         }
  123.  
  124.         $from = escapeShellCmd($from);
  125.         $mail @popen($this->sendmail_path . (!empty($this->sendmail_args' ' $this->sendmail_args : ''. " -f$from -- $recipients"'w');
  126.         if (!$mail{
  127.             return PEAR::raiseError('Failed to open sendmail [' $this->sendmail_path . '] for execution.');
  128.         }
  129.  
  130.         fputs($mail$text_headers);
  131.         fputs($mail$this->sep);  // newline to end the headers section
  132.         fputs($mail$body);
  133.         $result pclose($mail);
  134.         if (version_compare(phpversion()'4.2.3'== -1{
  135.             // With older php versions, we need to shift the pclose
  136.             // result to get the exit code.
  137.             $result $result >> 8 0xFF;
  138.         }
  139.  
  140.         if ($result != 0{
  141.             return PEAR::raiseError('sendmail returned error code ' $result,
  142.                                     $result);
  143.         }
  144.  
  145.         return true;
  146.     }
  147.  
  148. }

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