Mail.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // $Id: Mail.php,v 1.6 2003/06/26 07:05:36 jon Exp $
  20. require_once 'PEAR.php';
  21. /**
  22. * PEAR's Mail:: interface. Defines the interface for implementing
  23. * mailers under the PEAR hierarchy, and provides supporting functions
  24. * useful in multiple mailer backends.
  25. *
  26. * @access public
  27. * @version $Revision: 1.6 $
  28. * @package Mail
  29. */
  30. class Mail
  31. {
  32. /**
  33. * Line terminator used for separating header lines.
  34. * @var string
  35. */
  36. var $sep = "\r\n";
  37. /**
  38. * Provides an interface for generating Mail:: objects of various
  39. * types
  40. *
  41. * @param string $driver The kind of Mail:: object to instantiate.
  42. * @param array $params The parameters to pass to the Mail:: object.
  43. * @return object Mail a instance of the driver class or if fails a PEAR Error
  44. * @access public
  45. */
  46. function factory($driver, $params = array())
  47. {
  48. $driver = strtolower($driver);
  49. @include_once 'Mail/' . $driver . '.php';
  50. $class = 'Mail_' . $driver;
  51. if (class_exists($class)) {
  52. return new $class($params);
  53. } else {
  54. return PEAR::raiseError('Unable to find class for driver ' . $driver);
  55. }
  56. }
  57. /**
  58. * Implements Mail::send() function using php's built-in mail()
  59. * command.
  60. *
  61. * @param mixed $recipients Either a comma-seperated list of recipients
  62. * (RFC822 compliant), or an array of recipients,
  63. * each RFC822 valid. This may contain recipients not
  64. * specified in the headers, for Bcc:, resending
  65. * messages, etc.
  66. *
  67. * @param array $headers The array of headers to send with the mail, in an
  68. * associative array, where the array key is the
  69. * header name (ie, 'Subject'), and the array value
  70. * is the header value (ie, 'test'). The header
  71. * produced from those values would be 'Subject:
  72. * test'.
  73. *
  74. * @param string $body The full text of the message body, including any
  75. * Mime parts, etc.
  76. *
  77. * @return mixed Returns true on success, or a PEAR_Error
  78. * containing a descriptive error message on
  79. * failure.
  80. * @access public
  81. * @deprecated use Mail_mail::send instead
  82. */
  83. function send($recipients, $headers, $body)
  84. {
  85. // if we're passed an array of recipients, implode it.
  86. if (is_array($recipients)) {
  87. $recipients = implode(', ', $recipients);
  88. }
  89. // get the Subject out of the headers array so that we can
  90. // pass it as a seperate argument to mail().
  91. $subject = '';
  92. if (isset($headers['Subject'])) {
  93. $subject = $headers['Subject'];
  94. unset($headers['Subject']);
  95. }
  96. // flatten the headers out.
  97. list(,$text_headers) = Mail::prepareHeaders($headers);
  98. return mail($recipients, $subject, $body, $text_headers);
  99. }
  100. /**
  101. * Take an array of mail headers and return a string containing
  102. * text usable in sending a message.
  103. *
  104. * @param array $headers The array of headers to prepare, in an associative
  105. * array, where the array key is the header name (ie,
  106. * 'Subject'), and the array value is the header
  107. * value (ie, 'test'). The header produced from those
  108. * values would be 'Subject: test'.
  109. *
  110. * @return mixed Returns false if it encounters a bad address,
  111. * otherwise returns an array containing two
  112. * elements: Any From: address found in the headers,
  113. * and the plain text version of the headers.
  114. * @access private
  115. */
  116. function prepareHeaders($headers)
  117. {
  118. $lines = array();
  119. $from = null;
  120. foreach ($headers as $key => $value) {
  121. if ($key === 'From') {
  122. include_once 'Mail/RFC822.php';
  123. $addresses = Mail_RFC822::parseAddressList($value, 'localhost',
  124. false);
  125. $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
  126. // Reject envelope From: addresses with spaces.
  127. if (strstr($from, ' ')) {
  128. return false;
  129. }
  130. $lines[] = $key . ': ' . $value;
  131. } elseif ($key === 'Received') {
  132. // Put Received: headers at the top. Spam detectors often
  133. // flag messages with Received: headers after the Subject:
  134. // as spam.
  135. array_unshift($lines, $key . ': ' . $value);
  136. } else {
  137. $lines[] = $key . ': ' . $value;
  138. }
  139. }
  140. return array($from, join($this->sep, $lines) . $this->sep);
  141. }
  142. /**
  143. * Take a set of recipients and parse them, returning an array of
  144. * bare addresses (forward paths) that can be passed to sendmail
  145. * or an smtp server with the rcpt to: command.
  146. *
  147. * @param mixed Either a comma-seperated list of recipients
  148. * (RFC822 compliant), or an array of recipients,
  149. * each RFC822 valid.
  150. *
  151. * @return array An array of forward paths (bare addresses).
  152. * @access private
  153. */
  154. function parseRecipients($recipients)
  155. {
  156. include_once 'Mail/RFC822.php';
  157. // if we're passed an array, assume addresses are valid and
  158. // implode them before parsing.
  159. if (is_array($recipients)) {
  160. $recipients = implode(', ', $recipients);
  161. }
  162. // Parse recipients, leaving out all personal info. This is
  163. // for smtp recipients, etc. All relevant personal information
  164. // should already be in the headers.
  165. $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
  166. $recipients = array();
  167. if (is_array($addresses)) {
  168. foreach ($addresses as $ob) {
  169. $recipients[] = $ob->mailbox . '@' . $ob->host;
  170. }
  171. }
  172. return $recipients;
  173. }
  174. }
  175. ?>