sendmail.php 5.4 KB

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