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 5                                                        |
  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.  * @package Mail
  22.  * @version $Revision$
  23.  */
  24. class Mail2_sendmail extends Mail2 {
  25.  
  26.     /**
  27.      * The location of the sendmail or sendmail wrapper binary on the
  28.      * filesystem.
  29.      * @var string 
  30.      */
  31.     var $sendmail_path = '/usr/sbin/sendmail';
  32.  
  33.     /**
  34.      * Any extra command-line parameters to pass to the sendmail or
  35.      * sendmail wrapper binary.
  36.      * @var string 
  37.      */
  38.     var $sendmail_args = '-i';
  39.  
  40.     /**
  41.      * Constructor.
  42.      *
  43.      * Instantiates a new Mail_sendmail:: object based on the parameters
  44.      * passed in. It looks for the following parameters:
  45.      *     sendmail_path    The location of the sendmail binary on the
  46.      *                      filesystem. Defaults to '/usr/sbin/sendmail'.
  47.      *
  48.      *     sendmail_args    Any extra parameters to pass to the sendmail
  49.      *                      or sendmail wrapper binary.
  50.      *
  51.      * If a parameter is present in the $params array, it replaces the
  52.      * default.
  53.      *
  54.      * @param array $params Hash containing any parameters different from the
  55.      *               defaults.
  56.      */
  57.     public function __construct($params)
  58.     {
  59.         if (isset($params['sendmail_path'])) {
  60.             $this->sendmail_path = $params['sendmail_path'];
  61.         }
  62.         if (isset($params['sendmail_args'])) {
  63.             $this->sendmail_args = $params['sendmail_args'];
  64.         }
  65.  
  66.         /*
  67.          * Because we need to pass message headers to the sendmail program on
  68.          * the commandline, we can't guarantee the use of the standard "\r\n"
  69.          * separator.  Instead, we use the system's native line separator.
  70.          */
  71.         if (defined('PHP_EOL')) {
  72.             $this->sep = PHP_EOL;
  73.         else {
  74.             $this->sep = (strpos(PHP_OS'WIN'=== false"\n" "\r\n";
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Implements Mail::send() function using the sendmail
  80.      * command-line binary.
  81.      *
  82.      * @param mixed $recipients Either a comma-seperated list of recipients
  83.      *               (RFC822 compliant), or an array of recipients,
  84.      *               each RFC822 valid. This may contain recipients not
  85.      *               specified in the headers, for Bcc:, resending
  86.      *               messages, etc.
  87.      *
  88.      * @param array $headers The array of headers to send with the mail, in an
  89.      *               associative array, where the array key is the
  90.      *               header name (ie, 'Subject'), and the array value
  91.      *               is the header value (ie, 'test'). The header
  92.      *               produced from those values would be 'Subject:
  93.      *               test'.
  94.      *
  95.      * @param string $body The full text of the message body, including any
  96.      *                Mime parts, etc.
  97.      *
  98.      * @return mixed Returns true on success, or a PEAR_Error
  99.      *                containing a descriptive error message on
  100.      *                failure.
  101.      */
  102.     public function send($recipients$headers$body)
  103.     {
  104.         if (!is_array($headers)) {
  105.             throw new InvalidArgumentException('$headers must be an array');
  106.         }
  107.  
  108.         $result $this->_sanitizeHeaders($headers);
  109.         if (is_a($result'PEAR_Error')) {
  110.             return $result;
  111.         }
  112.  
  113.         $recipients $this->parseRecipients($recipients);
  114.         if (is_a($recipients'PEAR_Error')) {
  115.             return $recipients;
  116.         }
  117.         $recipients implode(' 'array_map('escapeshellarg'$recipients));
  118.  
  119.         $headerElements $this->prepareHeaders($headers);
  120.         if (is_a($headerElements'PEAR_Error')) {
  121.             return $headerElements;
  122.         }
  123.         list($from$text_headers$headerElements;
  124.  
  125.         /* Since few MTAs are going to allow this header to be forged
  126.          * unless it's in the MAIL FROM: exchange, we'll use
  127.          * Return-Path instead of From: if it's set. */
  128.         if (!empty($headers['Return-Path'])) {
  129.             $from $headers['Return-Path'];
  130.         }
  131.  
  132.         if (!isset($from)) {
  133.             throw new InvalidArgumentException('No from address given.');
  134.         elseif (strpos($from' '!== false ||
  135.                   strpos($from';'!== false ||
  136.                   strpos($from'&'!== false ||
  137.                   strpos($from'`'!== false{
  138.             throw new InvalidArgumentException('From address specified with dangerous characters.');
  139.         }
  140.  
  141.         $from escapeshellarg($from)// Security bug #16200
  142.  
  143.         $mail @popen($this->sendmail_path . (!empty($this->sendmail_args' ' $this->sendmail_args : ''. " -f$from -- $recipients"'w');
  144.         if (!$mail{
  145.             throw new Mail2_Exception('Failed to open sendmail [' $this->sendmail_path . '] for execution.');
  146.         }
  147.  
  148.         // Write the headers following by two newlines: one to end the headers
  149.         // section and a second to separate the headers block from the body.
  150.         fputs($mail$text_headers $this->sep . $this->sep);
  151.  
  152.         fputs($mail$body);
  153.         $result pclose($mail);
  154.         if (version_compare(phpversion()'4.2.3'== -1{
  155.             // With older php versions, we need to shift the pclose
  156.             // result to get the exit code.
  157.             $result $result >> 8 0xFF;
  158.         }
  159.  
  160.         if ($result != 0{
  161.             throw new Mail2_Exception('sendmail returned error code ' $result,
  162.                                     $result);
  163.         }
  164.  
  165.         return true;
  166.     }
  167.  
  168. }

Documentation generated on Mon, 11 Mar 2019 16:04:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.