SMTP.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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: Shane Caraveo <Shane@Caraveo.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: SMTP.php,v 1.17 2003/04/13 21:38:58 shane Exp $
  20. //
  21. // Status: rough draft, untested
  22. //
  23. // TODO:
  24. // switch to pear mail stuff
  25. // smtp authentication
  26. // smtp ssl support
  27. // ability to define smtp options (encoding, from, etc.)
  28. //
  29. require_once 'SOAP/Base.php';
  30. require_once 'Mail/smtp.php';
  31. /**
  32. * SMTP Transport for SOAP
  33. *
  34. * implements SOAP-SMTP as defined at
  35. * http://www.pocketsoap.com/specs/smtpbinding/
  36. *
  37. * TODO: use PEAR smtp and Mime classes
  38. *
  39. * @access public
  40. * @version $Id: SMTP.php,v 1.17 2003/04/13 21:38:58 shane Exp $
  41. * @package SOAP::Transport::SMTP
  42. * @author Shane Caraveo <shane@php.net>
  43. */
  44. class SOAP_Transport_SMTP extends SOAP_Base
  45. {
  46. var $credentials = '';
  47. var $timeout = 4; // connect timeout
  48. var $urlparts = NULL;
  49. var $url = '';
  50. var $incoming_payload = '';
  51. var $_userAgent = SOAP_LIBRARY_NAME;
  52. var $encoding = SOAP_DEFAULT_ENCODING;
  53. var $host = '127.0.0.1';
  54. var $port = 25;
  55. var $auth = NULL;
  56. /**
  57. * SOAP_Transport_SMTP Constructor
  58. *
  59. * @param string $URL mailto:address
  60. *
  61. * @access public
  62. */
  63. function SOAP_Transport_SMTP($URL, $encoding='US-ASCII')
  64. {
  65. parent::SOAP_Base('SMTP');
  66. $this->encoding = $encoding;
  67. $this->urlparts = @parse_url($URL);
  68. $this->url = $URL;
  69. }
  70. /**
  71. * send and receive soap data
  72. *
  73. * @param string &$msg outgoing post data
  74. * @param string $action SOAP Action header data
  75. * @param int $timeout socket timeout, default 0 or off
  76. *
  77. * @return string &$response response data, minus http headers
  78. * @access public
  79. */
  80. function send(&$msg, /*array*/ $options = NULL)
  81. {
  82. $this->incoming_payload = '';
  83. $this->outgoing_payload = &$msg;
  84. if (!$this->_validateUrl()) {
  85. return $this->fault;
  86. }
  87. if (!$options || !array_key_exists('from',$options)) {
  88. return $this->_raiseSoapFault("No FROM address to send message with");
  89. }
  90. if (isset($options['host'])) $this->host = $options['host'];
  91. if (isset($options['port'])) $this->port = $options['port'];
  92. if (isset($options['auth'])) $this->auth = $options['auth'];
  93. if (isset($options['username'])) $this->username = $options['username'];
  94. if (isset($options['password'])) $this->password = $options['password'];
  95. $headers = array();
  96. $headers['From'] = $options['from'];
  97. $headers['X-Mailer'] = $this->_userAgent;
  98. $headers['MIME-Version'] = '1.0';
  99. $headers['Message-ID'] = md5(time()).'.soap@'.$this->host;
  100. $headers['To'] = $this->urlparts['path'];
  101. if (array_key_exists('soapaction', $options)) {
  102. $headers['Soapaction'] = "\"{$options['soapaction']}\"";
  103. }
  104. if (isset($options['headers']))
  105. $headers = array_merge($headers, $options['headers']);
  106. // if the content type is already set, we assume that Mime encoding
  107. // is already done
  108. if (isset($headers['Content-Type'])) {
  109. $out = $msg;
  110. } else {
  111. // do a simple inline Mime encoding
  112. $headers['Content-Disposition'] = 'inline';
  113. $headers['Content-Type'] = "text/xml; charset=\"$this->encoding\"";
  114. if (array_key_exists('transfer-encoding', $options)) {
  115. if (strcasecmp($options['transfer-encoding'],'quoted-printable')==0) {
  116. $headers['Content-Transfer-Encoding'] = $options['transfer-encoding'];
  117. $out = &$msg;
  118. } else if (strcasecmp($options['transfer-encoding'],'base64')==0) {
  119. $headers['Content-Transfer-Encoding'] = 'base64';
  120. $out = chunk_split(base64_encode($msg),76,"\n");
  121. } else {
  122. return $this->_raiseSoapFault("Invalid Transfer Encoding: {$options['transfer-encoding']}");
  123. }
  124. } else {
  125. // default to base64
  126. $headers['Content-Transfer-Encoding'] = 'base64';
  127. $out = chunk_split(base64_encode($msg));
  128. }
  129. }
  130. $headers['Subject'] = array_key_exists('subject', $options) ? $options['subject'] : 'SOAP Message';
  131. foreach ($headers as $key => $value) {
  132. $header_text .= "$key: $value\n";
  133. }
  134. $this->outgoing_payload = $header_text."\r\n".$this->outgoing_payload;
  135. # we want to return a proper XML message
  136. $mailer_params = array(
  137. 'host' => $this->host,
  138. 'port' => $this->port,
  139. 'username' => $this->username,
  140. 'password' => $this->password,
  141. 'auth' => $this->auth
  142. );
  143. $mailer =& new Mail_smtp($mailer_params);
  144. $result = $mailer->send($this->urlparts['path'], $headers, $out);
  145. #$result = mail($this->urlparts['path'], $headers['Subject'], $out, $header_text);
  146. if (!PEAR::isError($result)) {
  147. $val =& new SOAP_Value('Message-ID','string',$headers['Message-ID']);
  148. } else {
  149. $sval[] =& new SOAP_Value('faultcode','QName','SOAP-ENV:Client');
  150. $sval[] =& new SOAP_Value('faultstring','string',"couldn't send SMTP message to {$this->urlparts['path']}");
  151. $val =& new SOAP_Value('Fault','Struct',$sval);
  152. }
  153. $mqname =& new QName($method, $namespace);
  154. $methodValue =& new SOAP_Value('Response', 'Struct', array($val));
  155. $this->incoming_payload =& $this->_makeEnvelope($methodValue, $this->headers, $this->encoding);
  156. return $this->incoming_payload;
  157. }
  158. /**
  159. * set data for http authentication
  160. * creates Authorization header
  161. *
  162. * @param string $username username
  163. * @param string $password response data, minus http headers
  164. *
  165. * @return none
  166. * @access public
  167. */
  168. function setCredentials($username, $password)
  169. {
  170. $this->username = $username;
  171. $this->password = $password;
  172. }
  173. // private members
  174. /**
  175. * validate url data passed to constructor
  176. *
  177. * @return boolean
  178. * @access private
  179. */
  180. function _validateUrl()
  181. {
  182. if ( ! is_array($this->urlparts) ) {
  183. $this->_raiseSoapFault("Unable to parse URL $url");
  184. return FALSE;
  185. }
  186. if (!isset($this->urlparts['scheme']) ||
  187. strcasecmp($this->urlparts['scheme'], 'mailto') != 0) {
  188. $this->_raiseSoapFault("Unable to parse URL $url");
  189. return FALSE;
  190. }
  191. if (!isset($this->urlparts['path'])) {
  192. $this->_raiseSoapFault("Unable to parse URL $url");
  193. return FALSE;
  194. }
  195. return TRUE;
  196. }
  197. } // end SOAP_Transport_HTTP
  198. ?>