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",   "[email protected] ");  // 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, as shown here.

    # Request and display all messages in $list_array
    for($xx=0; $xx<count($list_array); $xx++)
        {
        // Parse the mail ID from the message size
        list($mail_id, $size) = explode(" ", $list_array[$xx]);

        // Request the message for the specific mail ID
        $message = POP3_retr($POP3_connection, $mail_id);

        // Display message and place mail ID, size, and message in an array
        echo "$mail_id, $size
";
        $mail_array[$xx]['ID']      = $mail_id;
        $mail_array[$xx]['SIZE']    = $size;
        $mail_array[$xx]['MESSAGE'] = $message;

        // Display message in <xmp></xmp> tags to disable HTML
        // (in case script is run in a browser)
        echo "<xmp>$message</xmp>";


        // Delete the message from the server
        POP3_delete($POP3_connection, $mail_id);
        }

Listing 15-12: Reading, displaying, and deleting each message found on the server

Finally, after each message is read and deleted from the server, the session is closed, as shown in Listing 15-13.

    // End the server session
    echo POP3_quit($POP3_connection);
    }
else
    {
    echo "Login error";
    }

Listing 15-13: Closing the connection to the server, or noting the login error if necessary

Subsequently, if the connection wasn't originally made to the server, the script returns an error message.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.222