sendmail.php 6.4 KB

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