smtp.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // | Authors: Chuck Hagenbuch <chuck@horde.org> |
  17. // | Jon Parise <jon@php.net> |
  18. // +----------------------------------------------------------------------+
  19. require_once 'Mail.php';
  20. /**
  21. * SMTP implementation of the PEAR Mail:: interface. Requires the PEAR
  22. * Net_SMTP:: class.
  23. * @access public
  24. * @package Mail
  25. * @version $Revision: 1.12 $
  26. */
  27. class Mail_smtp extends Mail {
  28. /**
  29. * The SMTP host to connect to.
  30. * @var string
  31. */
  32. var $host = 'localhost';
  33. /**
  34. * The port the SMTP server is on.
  35. * @var integer
  36. */
  37. var $port = 25;
  38. /**
  39. * Should SMTP authentication be used?
  40. *
  41. * This value may be set to true, false or the name of a specific
  42. * authentication method.
  43. *
  44. * If the value is set to true, the Net_SMTP package will attempt to use
  45. * the best authentication method advertised by the remote SMTP server.
  46. *
  47. * @var mixed
  48. */
  49. var $auth = false;
  50. /**
  51. * The username to use if the SMTP server requires authentication.
  52. * @var string
  53. */
  54. var $username = '';
  55. /**
  56. * The password to use if the SMTP server requires authentication.
  57. * @var string
  58. */
  59. var $password = '';
  60. /**
  61. * Hostname or domain that will be sent to the remote SMTP server in the
  62. * HELO / EHLO message.
  63. *
  64. * @var string
  65. */
  66. var $localhost = 'localhost';
  67. /**
  68. * Constructor.
  69. *
  70. * Instantiates a new Mail_smtp:: object based on the parameters
  71. * passed in. It looks for the following parameters:
  72. * host The server to connect to. Defaults to localhost.
  73. * port The port to connect to. Defaults to 25.
  74. * auth SMTP authentication. Defaults to none.
  75. * username The username to use for SMTP auth. No default.
  76. * password The password to use for SMTP auth. No default.
  77. * localhost The local hostname / domain. Defaults to localhost.
  78. *
  79. * If a parameter is present in the $params array, it replaces the
  80. * default.
  81. *
  82. * @param array Hash containing any parameters different from the
  83. * defaults.
  84. * @access public
  85. */
  86. function Mail_smtp($params)
  87. {
  88. if (isset($params['host'])) $this->host = $params['host'];
  89. if (isset($params['port'])) $this->port = $params['port'];
  90. if (isset($params['auth'])) $this->auth = $params['auth'];
  91. if (isset($params['username'])) $this->username = $params['username'];
  92. if (isset($params['password'])) $this->password = $params['password'];
  93. if (isset($params['localhost'])) $this->localhost = $params['localhost'];
  94. }
  95. /**
  96. * Implements Mail::send() function using SMTP.
  97. *
  98. * @param mixed $recipients Either a comma-seperated list of recipients
  99. * (RFC822 compliant), or an array of recipients,
  100. * each RFC822 valid. This may contain recipients not
  101. * specified in the headers, for Bcc:, resending
  102. * messages, etc.
  103. *
  104. * @param array $headers The array of headers to send with the mail, in an
  105. * associative array, where the array key is the
  106. * header name (e.g., 'Subject'), and the array value
  107. * is the header value (e.g., 'test'). The header
  108. * produced from those values would be 'Subject:
  109. * test'.
  110. *
  111. * @param string $body The full text of the message body, including any
  112. * Mime parts, etc.
  113. *
  114. * @return mixed Returns true on success, or a PEAR_Error
  115. * containing a descriptive error message on
  116. * failure.
  117. * @access public
  118. */
  119. function send($recipients, $headers, $body)
  120. {
  121. include_once 'Net/SMTP.php';
  122. if (!($smtp = new Net_SMTP($this->host, $this->port, $this->localhost))) {
  123. return new PEAR_Error('unable to instantiate Net_SMTP object');
  124. }
  125. if (PEAR::isError($smtp->connect())) {
  126. return new PEAR_Error('unable to connect to smtp server ' .
  127. $this->host . ':' . $this->port);
  128. }
  129. if ($this->auth) {
  130. $method = is_string($this->auth) ? $this->auth : '';
  131. if (PEAR::isError($smtp->auth($this->username, $this->password,
  132. $method))) {
  133. return new PEAR_Error('unable to authenticate to smtp server');
  134. }
  135. }
  136. list($from, $text_headers) = $this->prepareHeaders($headers);
  137. /*
  138. * Since few MTAs are going to allow this header to be forged unless
  139. * it's in the MAIL FROM: exchange, we'll use Return-Path instead of
  140. * From: if it's set.
  141. */
  142. if (!empty($headers['Return-Path'])) {
  143. $from = $headers['Return-Path'];
  144. }
  145. if (!isset($from)) {
  146. return new PEAR_Error('No from address given');
  147. }
  148. if (PEAR::isError($smtp->mailFrom($from))) {
  149. return new PEAR_Error('unable to set sender to [' . $from . ']');
  150. }
  151. $recipients = $this->parseRecipients($recipients);
  152. foreach($recipients as $recipient) {
  153. if (PEAR::isError($res = $smtp->rcptTo($recipient))) {
  154. return new PEAR_Error('unable to add recipient [' .
  155. $recipient . ']: ' . $res->getMessage());
  156. }
  157. }
  158. if (PEAR::isError($smtp->data($text_headers . "\r\n" . $body))) {
  159. return new PEAR_Error('unable to send data');
  160. }
  161. $smtp->disconnect();
  162. return true;
  163. }
  164. }
  165. ?>