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 Example 14-10 through Example 14-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.
Before you use the script in Example 14-10, replace the values for SERVER
, USER
, and PASS
with your email account information.
Example 14-10. Including the LIB_pop3
library and initializing credentials
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
In Example 14-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.
Example 14-11. Connecting to the server and making an array of available messages
# 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);
The script in Example 14-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.
Example 14-12. Reading, displaying, and deleting each message found on the server
# 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\n"; $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); }
Finally, after each message is read and deleted from the server, the session is closed, as shown in Example 14-13.
Example 14-13. Closing the connection to the server, or noting the login error if necessary
// End the server session echo POP3_quit($POP3_connection); } else { echo "Login error"; }
Subsequently, if the connection wasn’t originally made to the server, the script returns an error message.