PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Mail/Mail/sendmail.php

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