Writing SMTP-Based SOAP Messages in PHP
By Shane Caraveo, October 01, 2002
With the buzz around web services, most SOAP usage has focused around the HTTP protocol, but other protocols can be used as well. SMTP has advantages not found with HTTP, including SMTP's ability to store and forward messages, implement one-to-many broadcast messaging, and use attachments to embed extra data into a SOAP message.
October 2002/Writing SMTP-Based SOAP Messages in PHP
Listing 5: Retrieving messages via the POP3 protocol
<?php
/* include the email server class, which knows how to
parse a standard email as a SOAP message */
require_once 'SOAP/Server/Email.php';
/* create the SOAP server object */
$server = new SOAP_Server_Email('smtp.domain.com');
/* This is our image archival class. It is
extremely simplistic; it just stores the image
to a configured directory, then returns
an http url pointing to the image. */
class imageArchiver
{
/* configure variables */
var $basedir = 'c:\\website\\images\\';
var $baseurl = 'http://server.com/images/';
/* SOAP related variables */
var $method_namespace = 'http://server.com/imagearchive';
function archiveImage($name, $image)
{
$f = fopen($this->basedir.$name, 'w');
if ($f) {
fwrite($f, $image);
fclose($f);
return $this->baseurl.$name;
}
// we were unable to open the file, return an error
return new SOAP_Fault("Unable to save image $name to disk");
}
}
/* Now we register our image archive class to the SOAP server. */
/*The SOAP server can only execute functions that are registered with it. */
$server->addObjectMap(new imageArchiver());
/* include a class to access POP3 */
require_once 'Net/POP3.php';
/* connect to a POP3 server and read the messages */
$pop3 =& new Net_POP3();
if ($pop3->connect('localhost', 110)) {
if ($pop3->login('soap', 'password')) {
$listing = $pop3->getListing();
/* now loop through each message, and call the
SOAP server with that message */
foreach ($listing as $msg) {
$email = $pop3->getMsg($msg['msg_id']);
/* This is where we actually handle the SOAP
response. The SOAP::Server::Email class we
are using will send the SOAP response to
the sender via email. */
if ($email) {
$server->service($email);
$pop3->deleteMsg($msg['msg_id']);
}
}
}
$pop3->disconnect();
}
?>