mail.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * internal PHP-mail() implementation of the PEAR Mail:: interface.
  4. *
  5. * PHP version 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$
  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$
  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. public function __construct($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. public function send($recipients, $headers, $body)
  109. {
  110. if (!is_array($headers)) {
  111. return PEAR::raiseError('$headers must be an array');
  112. }
  113. $result = $this->_sanitizeHeaders($headers);
  114. if (is_a($result, 'PEAR_Error')) {
  115. return $result;
  116. }
  117. // If we're passed an array of recipients, implode it.
  118. if (is_array($recipients)) {
  119. $recipients = implode(', ', $recipients);
  120. }
  121. // Get the Subject out of the headers array so that we can
  122. // pass it as a seperate argument to mail().
  123. $subject = '';
  124. if (isset($headers['Subject'])) {
  125. $subject = $headers['Subject'];
  126. unset($headers['Subject']);
  127. }
  128. // Also remove the To: header. The mail() function will add its own
  129. // To: header based on the contents of $recipients.
  130. unset($headers['To']);
  131. // Flatten the headers out.
  132. $headerElements = $this->prepareHeaders($headers);
  133. if (is_a($headerElements, 'PEAR_Error')) {
  134. return $headerElements;
  135. }
  136. list(, $text_headers) = $headerElements;
  137. // We only use mail()'s optional fifth parameter if the additional
  138. // parameters have been provided and we're not running in safe mode.
  139. if (empty($this->_params) || ini_get('safe_mode')) {
  140. $result = mail($recipients, $subject, $body, $text_headers);
  141. } else {
  142. $result = mail($recipients, $subject, $body, $text_headers,
  143. $this->_params);
  144. }
  145. // If the mail() function returned failure, we need to create a
  146. // PEAR_Error object and return it instead of the boolean result.
  147. if ($result === false) {
  148. $result = PEAR::raiseError('mail() returned failure');
  149. }
  150. return $result;
  151. }
  152. }