Home arrow PHP arrow Page 7 - Socket Programming With PHP

POP Goes The Weasel - PHP

You might not know this, but PHP comes with a very capable socketprogramming API. These socket functions now include almost everything youwould need for socket-based client-server communication over TCP/IP, andcan be easily deployed to build simple network applications. Find out more,inside.

TABLE OF CONTENTS:
  1. Socket Programming With PHP
  2. Putting It All Together
  3. Fortune's Fool
  4. Looping The Loop
  5. On Web-bed Feet
  6. Different Strokes
  7. POP Goes The Weasel
  8. Access Denied
  9. Game Over
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 204
February 05, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Let's try something a little more advanced. How about a TCP client that connects to a POP3 server to retrieve the total number of messages for a user's mailbox?

In order to build such a client, I need to first understand the sequence of commands passed to a POP3 server in order to obtain the message total, and then replicate this sequence in my PHP-based client. The best way to do this is by using a regular telnet client to interact with the server and understand the command sequence - so here goes:

$ telnet mail.host 110 Trying 192.168.0.1... Connected to 192.168.0.1. Escape character is '^]'. +OK POP3 mail.host v5.5 server ready USER john +OK User name accepted, password please PASS doe +OK Mailbox open, 72 messages STAT +OK 72 24595628 QUIT +OK Sayonara Connection closed by foreign host.
As you can see from the sample session above, the second element of the string returned by a STAT command

STAT +OK 72 24595628
holds the total number of messages (72, in this case). All that's needed, therefore, is a script that connects to the POP3 server (usually available on port 110), sends the sequence of commands above, retrieves the output of the STAT command, and extracts the message total from it.

Here's the script to accomplish this:

<? // mail server settings $host="192.168.0.99"; $port = 110; $user = "john"; $pass = "doe"; // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); // if a handle is not returned if (!$fp) { die("Error: could not open socket connection\n"); } else { // get the welcome message $welcome = fgets ($fp, 150); // check for success code if (substr($welcome, 0, 3) == "+OK") { // send username and read response fputs ($fp, "USER $user\n"); fgets($fp, 50); // send password and read response fputs ($fp, "PASS $pass\n"); $ack = fgets($fp, 50); // check for success code if (substr($ack, 0, 3) == "+OK") { // send status request and read response fputs ($fp, "STAT\n"); $status = fgets($fp, 50); if (substr($status, 0, 3) == "+OK") { // shut down connection fputs ($fp, "QUIT\n"); fclose ($fp); } // error getting status else { die ("Server said: $status"); } } // auth failure else { die ("Server said: $ack"); } } // bad welcome message else { die ("Bad connection string\n"); } // get status string // split by spaces $arr = explode(" ", $status); // the second element contains the total number of messages echo $arr[1] . " messages in mailbox"; } ?>
And here's the output:

$ /usr/local/bin/php -q popclient.php 72 messages in mailbox
How does this work? Very simple.

First, a connection is opened to the POP3 server using the fsockopen() function discussed previously; the arguments to this function (host, port et al) are obtained through PHP variables which are set at the top of the script.

<? // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); ?>
Once a socket connection has been established, the fgets() and fputs() functions are used to send POP3 commands to the server via this socket, and read the resulting output into PHP variables.

<? // send username and read response fputs ($fp, "USER $user\n"); fgets($fp, 50); // send password and read response fputs ($fp, "PASS $pass\n"); $ack = fgets($fp, 50); ?>
A POP3 server typically prefixes the result of every successful command with the string "+OK". This knowledge allows for simple error-checking within the script - note how the output of fgets() is checked at every stage, with subsequent commands executed only if the previous one was successful.

<? // send status request and read response fputs ($fp, "STAT\n"); $status = fgets($fp, 50); if (substr($status, 0, 3) == "+OK") { // shut down connection fputs ($fp, "QUIT\n"); fclose ($fp); } // error getting status else { die ("Server said: $status"); } ?>
Once the output of the STAT command has been received, the socket is closed, and the result string is split into its constituent parts with PHP's very cool explode() function. The message total is then extracted and printed.

<? // get status string // split by spaces $arr = explode(" ", $status); // the second element contains the total number of messages echo $arr[1] . " messages in mailbox"; ?>
Simple, huh?

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: