SOAP--Transport--SMTP
[ class tree: SOAP--Transport--SMTP ] [ index: SOAP--Transport--SMTP ] [ all elements ]

Source for file SMTP.php

Documentation is available at SMTP.php

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

Documentation generated on Mon, 11 Mar 2019 13:59:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.