Webbots, Spiders, and Screen Scrapers - Michael Schrenk [66]
Parsing the actual message is more involved, as shown in Listing 15-8.
$content_type = return_between($raw_message, "Content-Type: ", "\n", EXCL);
$boundary = get_attribute($content_type, "boundary");
$raw_msg = return_between($message, "--".$boundary, "--".$boundary, EXCL );
$msg_separator = $raw_msg, chr(13).chr(10).chr(13).chr(10);
$clean_msg = return_between($raw_msg, $msg_separator, $msg_separator, EXCL );
Listing 15-8: Parsing the actual message from a raw POP3 response
When parsing the message, you must first identify the Content-Type, which holds the boundaries describing where the message is found. The Content-Type is further parsed with the get_attribute() function, to obtain the actual boundary value.[51] Finally, the text defined within the boundaries may contain additional information that tells the client how to display the content of the message. This information, if it exists, is removed by parsing only what's within the message separator, a combination of carriage returns and line feeds.
Other Useful POP3 Commands
The DELE and QUIT (followed by the mail id) commands mark a message for deletion. Listing 15-9 shows demonstrations of both the DELE and QUIT commands.
DELE 8
+OK
QUIT
+OK
Listing 15-9: Using the POP3 DELE and QUIT commands
When you use DELE, the deleted message is only marked for deletion and not actually deleted. The deletion doesn't occur until you execute a QUIT command and your server session ends.
Note
If you've accidentally marked a message with the DELE function and wish to retain it when you quit, enter RSET followed by the message number. The message will not be marked for deletion when you issue the QUIT command (retention is the default condition).
* * *
[49] See Chapter 16 to learn how to send email with webbots and spiders.
[50] Telnet clients are standard on all Windows, Mac OS X, Linux, and Unix distributions.
[51] The actual boundary, which defines the message, is prefixed with -- characters to distinguish the actual boundary from where it is defined.
Executing POP3 Commands with a Webbot
POP3 commands can be performed with PHP's opensocket(), fputs(), and fgets() functions. The LIB_pop3 library is available for you to download from this book's website. This library contains functions for connecting to the mail server, authenticating your account on the server, finding out what mail is available for the account, requesting messages from the server, and deleting messages.
The scripts in Listings 15-10 through 15-13 show how to use the LIB_pop3 library. The larger script is split up and annotated here for clarity, but it is available in its entirety on this book's website.
Note
Before you use the script in Listing 15-10, replace the values for SERVER, USER, and PASS with your email account information.
include("LIB_pop3.php"); // Include POP3 command library
define("SERVER", "your.mailserver.net"); // Your POP3 mailserver
define("USER", "your@emailsccount.com "); // Your POP3 email address
define("PASS", "your_password"); // Your POP3 password
Listing 15-10: Including the LIB_pop3 library and initializing credentials
In Listing 15-11, the script makes the connection to the server and, after a successful login attempt, obtains a connection array containing the "handle" that is required for all subsequent communication with the server.
# Connect to POP3 server
$connection_array = POP3_connect(SERVER, USER, PASS);
$POP3_connection = $connection_array['handle'];
if($POP3_connection)
{
// Create an array, which is the result of a POP3 LIST command
$list_array = POP3_list($POP3_connection);
Listing 15-11: Connecting to the server and making an array of available messages
The script in Listing 15-12 uses the $list_array obtained in the previous step to create requests for each email message. It displays each message along with its ID and size and then deletes the message,