You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Mail.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.11 2005/06/26 23:37:18 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.11 $
  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. $mailer = &new $class($params);
  53. } else {
  54. $mailer = PEAR::raiseError('Unable to find class for driver ' . $driver);
  55. }
  56. return $mailer;
  57. }
  58. /**
  59. * Implements Mail::send() function using php's built-in mail()
  60. * command.
  61. *
  62. * @param mixed $recipients Either a comma-seperated list of recipients
  63. * (RFC822 compliant), or an array of recipients,
  64. * each RFC822 valid. This may contain recipients not
  65. * specified in the headers, for Bcc:, resending
  66. * messages, etc.
  67. *
  68. * @param array $headers The array of headers to send with the mail, in an
  69. * associative array, where the array key is the
  70. * header name (ie, 'Subject'), and the array value
  71. * is the header value (ie, 'test'). The header
  72. * produced from those values would be 'Subject:
  73. * test'.
  74. *
  75. * @param string $body The full text of the message body, including any
  76. * Mime parts, etc.
  77. *
  78. * @return mixed Returns true on success, or a PEAR_Error
  79. * containing a descriptive error message on
  80. * failure.
  81. * @access public
  82. * @deprecated use Mail_mail::send instead
  83. */
  84. function send($recipients, $headers, $body)
  85. {
  86. // if we're passed an array of recipients, implode it.
  87. if (is_array($recipients)) {
  88. $recipients = implode(', ', $recipients);
  89. }
  90. // get the Subject out of the headers array so that we can
  91. // pass it as a seperate argument to mail().
  92. $subject = '';
  93. if (isset($headers['Subject'])) {
  94. $subject = $headers['Subject'];
  95. unset($headers['Subject']);
  96. }
  97. // flatten the headers out.
  98. list(,$text_headers) = Mail::prepareHeaders($headers);
  99. return mail($recipients, $subject, $body, $text_headers);
  100. }
  101. /**
  102. * Take an array of mail headers and return a string containing
  103. * text usable in sending a message.
  104. *
  105. * @param array $headers The array of headers to prepare, in an associative
  106. * array, where the array key is the header name (ie,
  107. * 'Subject'), and the array value is the header
  108. * value (ie, 'test'). The header produced from those
  109. * values would be 'Subject: test'.
  110. *
  111. * @return mixed Returns false if it encounters a bad address,
  112. * otherwise returns an array containing two
  113. * elements: Any From: address found in the headers,
  114. * and the plain text version of the headers.
  115. * @access private
  116. */
  117. function prepareHeaders($headers)
  118. {
  119. $lines = array();
  120. $from = null;
  121. foreach ($headers as $key => $value) {
  122. if (strcasecmp($key, 'From') === 0) {
  123. include_once 'Mail/RFC822.php';
  124. $parser = &new Mail_RFC822();
  125. $addresses = $parser->parseAddressList($value, 'localhost', false);
  126. if (PEAR::isError($addresses)) {
  127. return $addresses;
  128. }
  129. $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
  130. // Reject envelope From: addresses with spaces.
  131. if (strstr($from, ' ')) {
  132. return false;
  133. }
  134. $lines[] = $key . ': ' . $value;
  135. } elseif (strcasecmp($key, 'Received') === 0) {
  136. $received = array();
  137. if (is_array($value)) {
  138. foreach ($value as $line) {
  139. $received[] = $key . ': ' . $line;
  140. }
  141. }
  142. else {
  143. $received[] = $key . ': ' . $value;
  144. }
  145. // Put Received: headers at the top. Spam detectors often
  146. // flag messages with Received: headers after the Subject:
  147. // as spam.
  148. $lines = array_merge($received, $lines);
  149. } else {
  150. // If $value is an array (i.e., a list of addresses), convert
  151. // it to a comma-delimited string of its elements (addresses).
  152. if (is_array($value)) {
  153. $value = implode(', ', $value);
  154. }
  155. $lines[] = $key . ': ' . $value;
  156. }
  157. }
  158. return array($from, join($this->sep, $lines) . $this->sep);
  159. }
  160. /**
  161. * Take a set of recipients and parse them, returning an array of
  162. * bare addresses (forward paths) that can be passed to sendmail
  163. * or an smtp server with the rcpt to: command.
  164. *
  165. * @param mixed Either a comma-seperated list of recipients
  166. * (RFC822 compliant), or an array of recipients,
  167. * each RFC822 valid.
  168. *
  169. * @return array An array of forward paths (bare addresses).
  170. * @access private
  171. */
  172. function parseRecipients($recipients)
  173. {
  174. include_once 'Mail/RFC822.php';
  175. // if we're passed an array, assume addresses are valid and
  176. // implode them before parsing.
  177. if (is_array($recipients)) {
  178. $recipients = implode(', ', $recipients);
  179. }
  180. // Parse recipients, leaving out all personal info. This is
  181. // for smtp recipients, etc. All relevant personal information
  182. // should already be in the headers.
  183. $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
  184. $recipients = array();
  185. if (is_array($addresses)) {
  186. foreach ($addresses as $ob) {
  187. $recipients[] = $ob->mailbox . '@' . $ob->host;
  188. }
  189. }
  190. return $recipients;
  191. }
  192. }