mail.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * internal PHP-mail() implementation of the PEAR Mail:: interface.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2010 Chuck Hagenbuch
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * o Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * o Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * o The names of the authors may not be used to endorse or promote
  22. * products derived from this software without specific prior written
  23. * permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Mail
  38. * @package Mail
  39. * @author Chuck Hagenbuch <chuck@horde.org>
  40. * @copyright 2010 Chuck Hagenbuch
  41. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  42. * @version CVS: $Id: mail.php 294747 2010-02-08 08:18:33Z clockwerx $
  43. * @link http://pear.php.net/package/Mail/
  44. */
  45. /**
  46. * internal PHP-mail() implementation of the PEAR Mail:: interface.
  47. * @package Mail
  48. * @version $Revision: 294747 $
  49. */
  50. class Mail_mail extends Mail {
  51. /**
  52. * Any arguments to pass to the mail() function.
  53. * @var string
  54. */
  55. var $_params = '';
  56. /**
  57. * Constructor.
  58. *
  59. * Instantiates a new Mail_mail:: object based on the parameters
  60. * passed in.
  61. *
  62. * @param array $params Extra arguments for the mail() function.
  63. */
  64. function Mail_mail($params = null)
  65. {
  66. // The other mail implementations accept parameters as arrays.
  67. // In the interest of being consistent, explode an array into
  68. // a string of parameter arguments.
  69. if (is_array($params)) {
  70. $this->_params = join(' ', $params);
  71. } else {
  72. $this->_params = $params;
  73. }
  74. /* Because the mail() function may pass headers as command
  75. * line arguments, we can't guarantee the use of the standard
  76. * "\r\n" separator. Instead, we use the system's native line
  77. * separator. */
  78. if (defined('PHP_EOL')) {
  79. $this->sep = PHP_EOL;
  80. } else {
  81. $this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
  82. }
  83. }
  84. /**
  85. * Implements Mail_mail::send() function using php's built-in mail()
  86. * command.
  87. *
  88. * @param mixed $recipients Either a comma-seperated list of recipients
  89. * (RFC822 compliant), or an array of recipients,
  90. * each RFC822 valid. This may contain recipients not
  91. * specified in the headers, for Bcc:, resending
  92. * messages, etc.
  93. *
  94. * @param array $headers The array of headers to send with the mail, in an
  95. * associative array, where the array key is the
  96. * header name (ie, 'Subject'), and the array value
  97. * is the header value (ie, 'test'). The header
  98. * produced from those values would be 'Subject:
  99. * test'.
  100. *
  101. * @param string $body The full text of the message body, including any
  102. * Mime parts, etc.
  103. *
  104. * @return mixed Returns true on success, or a PEAR_Error
  105. * containing a descriptive error message on
  106. * failure.
  107. *
  108. * @access public
  109. */
  110. function send($recipients, $headers, $body)
  111. {
  112. if (!is_array($headers)) {
  113. return PEAR::raiseError('$headers must be an array');
  114. }
  115. $result = $this->_sanitizeHeaders($headers);
  116. if (is_a($result, 'PEAR_Error')) {
  117. return $result;
  118. }
  119. // If we're passed an array of recipients, implode it.
  120. if (is_array($recipients)) {
  121. $recipients = implode(', ', $recipients);
  122. }
  123. // Get the Subject out of the headers array so that we can
  124. // pass it as a seperate argument to mail().
  125. $subject = '';
  126. if (isset($headers['Subject'])) {
  127. $subject = $headers['Subject'];
  128. unset($headers['Subject']);
  129. }
  130. // Also remove the To: header. The mail() function will add its own
  131. // To: header based on the contents of $recipients.
  132. unset($headers['To']);
  133. // Flatten the headers out.
  134. $headerElements = $this->prepareHeaders($headers);
  135. if (is_a($headerElements, 'PEAR_Error')) {
  136. return $headerElements;
  137. }
  138. list(, $text_headers) = $headerElements;
  139. // We only use mail()'s optional fifth parameter if the additional
  140. // parameters have been provided and we're not running in safe mode.
  141. if (empty($this->_params) || ini_get('safe_mode')) {
  142. $result = mail($recipients, $subject, $body, $text_headers);
  143. } else {
  144. $result = mail($recipients, $subject, $body, $text_headers,
  145. $this->_params);
  146. }
  147. // If the mail() function returned failure, we need to create a
  148. // PEAR_Error object and return it instead of the boolean result.
  149. if ($result === false) {
  150. $result = PEAR::raiseError('mail() returned failure');
  151. }
  152. return $result;
  153. }
  154. }