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