SOAP
[ class tree: SOAP ] [ index: SOAP ] [ all elements ]

Source for file SMTP.php

Documentation is available at SMTP.php

  1. <?php
  2. /**
  3.  * This file contains the code for an SMTP transport layer.
  4.  *
  5.  * This code is still a rough and untested draft.
  6.  * TODO:
  7.  *  switch to pear mail stuff
  8.  *  smtp authentication
  9.  *  smtp ssl support
  10.  *  ability to define smtp options (encoding, from, etc.)
  11.  *
  12.  * PHP versions 4 and 5
  13.  *
  14.  * LICENSE: This source file is subject to version 2.02 of the PHP license,
  15.  * that is bundled with this package in the file LICENSE, and is available at
  16.  * through the world-wide-web at http://www.php.net/license/2_02.txt.  If you
  17.  * did not receive a copy of the PHP license and are unable to obtain it
  18.  * through the world-wide-web, please send a note to license@php.net so we can
  19.  * mail you a copy immediately.
  20.  *
  21.  * @category   Web Services
  22.  * @package    SOAP
  23.  * @author     Shane Caraveo <Shane@Caraveo.com>
  24.  * @author     Jan Schneider <jan@horde.org>
  25.  * @copyright  2003-2006 The PHP Group
  26.  * @license    http://www.php.net/license/2_02.txt  PHP License 2.02
  27.  * @link       http://pear.php.net/package/SOAP
  28.  */
  29.  
  30. require_once 'SOAP/Transport.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.  * @package SOAP
  43.  * @author Shane Caraveo <shane@php.net>
  44.  * @author Jan Schneider <jan@horde.org>
  45.  */
  46. {
  47.     var $credentials = '';
  48.     var $timeout = 4; // connect timeout
  49.     var $host = '127.0.0.1';
  50.     var $port = 25;
  51.     var $auth = null;
  52.  
  53.     /**
  54.      * SOAP_Transport_SMTP Constructor
  55.      *
  56.      * @param string $url  mailto: address.
  57.      *
  58.      * @access public
  59.      */
  60.     function SOAP_Transport_SMTP($url$encoding 'US-ASCII')
  61.     {
  62.         parent::SOAP_Base('SMTP');
  63.         $this->encoding = $encoding;
  64.         $this->urlparts = @parse_url($url);
  65.         $this->url = $url;
  66.     }
  67.  
  68.     /**
  69.      * Sends and receives SOAP data.
  70.      *
  71.      * @access public
  72.      *
  73.      * @param string  Outgoing SOAP data.
  74.      * @param array   Options.
  75.      *
  76.      * @return string|SOAP_Fault
  77.      */
  78.     function send($msg$options = array())
  79.     {
  80.         $this->fault = null;
  81.         $this->incoming_payload = '';
  82.         $this->outgoing_payload = $msg;
  83.         if (!$this->_validateUrl()) {
  84.             return $this->fault;
  85.         }
  86.         if (!$options || !isset($options['from'])) {
  87.             return $this->_raiseSoapFault('No From: address to send message with');
  88.         }
  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.  
  96.         $headers = array();
  97.         $headers['From'$options['from'];
  98.         $headers['X-Mailer'$this->_userAgent;
  99.         $headers['MIME-Version''1.0';
  100.         $headers['Message-ID'md5(time()) '.soap@' $this->host;
  101.         $headers['To'$this->urlparts['path'];
  102.         if (isset($options['soapaction'])) {
  103.             $headers['Soapaction'= "\"{$options['soapaction']}\"";
  104.         }
  105.  
  106.         if (isset($options['headers']))
  107.             $headers array_merge($headers$options['headers']);
  108.  
  109.         // If the content type is already set, we assume that MIME encoding is
  110.         // already done.
  111.         if (isset($headers['Content-Type'])) {
  112.             $out $msg;
  113.         else {
  114.             // Do a simple inline MIME encoding.
  115.             $headers['Content-Disposition''inline';
  116.             $headers['Content-Type'= "text/xml; charset=\"$this->encoding\"";
  117.             if (isset($options['transfer-encoding'])) {
  118.                 if (strcasecmp($options['transfer-encoding']'quoted-printable'== 0{
  119.                     $headers['Content-Transfer-Encoding'$options['transfer-encoding'];
  120.                     $out $msg;
  121.                 elseif (strcasecmp($options['transfer-encoding'],'base64'== 0{
  122.                     $headers['Content-Transfer-Encoding''base64';
  123.                     $out chunk_split(base64_encode($msg)76"\n");
  124.                 else {
  125.                     return $this->_raiseSoapFault("Invalid Transfer Encoding: {$options['transfer-encoding']}");
  126.                 }
  127.             else {
  128.                 // Default to base64.
  129.                 $headers['Content-Transfer-Encoding''base64';
  130.                 $out chunk_split(base64_encode($msg));
  131.             }
  132.         }
  133.  
  134.         $headers['Subject'= isset($options['subject']$options['subject''SOAP Message';
  135.  
  136.         foreach ($headers as $key => $value{
  137.             $header_text .= "$key$value\n";
  138.         }
  139.         $this->outgoing_payload = $header_text "\r\n" $this->outgoing_payload;
  140.  
  141.         $mailer_params = array(
  142.             'host' => $this->host,
  143.             'port' => $this->port,
  144.             'username' => $this->username,
  145.             'password' => $this->password,
  146.             'auth' => $this->auth
  147.         );
  148.         $mailer = new Mail_smtp($mailer_params);
  149.         $result $mailer->send($this->urlparts['path']$headers$out);
  150.         if (!PEAR::isError($result)) {
  151.             $val = new SOAP_Value('Message-ID''string'$headers['Message-ID']);
  152.         else {
  153.             $sval[= new SOAP_Value('faultcode''QName'SOAP_BASE::SOAPENVPrefix().':Client');
  154.             $sval[= new SOAP_Value('faultstring''string'"couldn't send SMTP message to {$this->urlparts['path']}");
  155.             $val = new SOAP_Value('Fault''Struct'$sval);
  156.         }
  157.  
  158.         $methodValue = new SOAP_Value('Response''Struct'array($val));
  159.  
  160.         $this->incoming_payload = $this->makeEnvelope($methodValue,
  161.                                                       $this->headers,
  162.                                                       $this->encoding);
  163.  
  164.         return $this->incoming_payload;
  165.     }
  166.  
  167.     /**
  168.      * Sets data for HTTP authentication, creates Authorization header.
  169.      *
  170.      * @param string $username  Username.
  171.      * @param string $password  Response data, minus HTTP headers.
  172.      *
  173.      * @access public
  174.      */
  175.     function setCredentials($username$password)
  176.     {
  177.         $this->username $username;
  178.         $this->password $password;
  179.     }
  180.  
  181.     /**
  182.      * Validates url data passed to constructor.
  183.      *
  184.      * @return boolean 
  185.      * @access private
  186.      */
  187.     function _validateUrl()
  188.     {
  189.         if (!is_array($this->urlparts)) {
  190.             $this->_raiseSoapFault("Unable to parse URL $this->url");
  191.             return false;
  192.         }
  193.         if (!isset($this->urlparts['scheme']||
  194.             strcasecmp($this->urlparts['scheme']'mailto'!= 0{
  195.                 $this->_raiseSoapFault("Unable to parse URL $this->url");
  196.                 return false;
  197.         }
  198.         if (!isset($this->urlparts['path'])) {
  199.             $this->_raiseSoapFault("Unable to parse URL $this->url");
  200.             return false;
  201.         }
  202.         return true;
  203.     }
  204.  
  205. }

Documentation generated on Mon, 04 Aug 2008 20:00:31 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.