Getting and Parsing E-mail With PHP

Here’s my Problem: My website sends text files to a partner’s site via FTP. Our partner site sends us the results of their processing the file in an e-mail. This e-mail is a the direct output from their processing script. Or, the relevant details are buried in a bunch of garbled text.

My Solution: I first looked at message piping, but my hosting provider doesn’t provide an easy way to do this. Then I found this PHP class from PHP classes, which handles the interactions with the server. You do have to create an account with PHP classes to download the files. If you e-mail me directly, I could send them to you. It’s easy enough to get working quickly.

$pop3->hostname="localhost";    // POP 3 server host name 

$user="username";                    // Authentication user name 
$password="password";             // Authentication password  

$pop3->debug=1;                    // Output debug information
$pop3->html_debug=1;            // Debug information is in HTML

Edit the test_pop3.php file. All you need to do is change the sever name, user name and password to get rolling. After you see the full output and it’s getting the mail correctly, I recommend setting the debug and html_debug values to 0. This will cut out the unnecessary text of the interaction with the server and just show you the messages and what’s going on.

One hangup of this class, the body of the message is an array and the message was repeated twice in the array.

array(14) {
  [0]=> string(30) "--0016e6d99d7ed848e5047b02e012"
  [1]=> string(44) "Content-Type: text/plain; charset=ISO-8859-1"
  [2]=> string(0) ""
  [3]=> string(71) "This is a test. If this were an actual message, important text would be"
  [4]=> string(5) "here."
  [5]=> string(0) ""
  [6]=> string(30) "--0016e6d99d7ed848e5047b02e012"
  [7]=> string(43) "Content-Type: text/html; charset=ISO-8859-1"
  [8]=> string(0) ""
  [9]=> string(77) "This is a test. If this were an actual message, important text would be here."
  [10]=> string(4) ""
  [11]=> string(0) ""
  [12]=> string(32) "--0016e6d99d7ed848e5047b02e012--"
  [13]=> string(0) ""
}

Luckily, the part boundaries are included in the array which makes it fairly easy to split the array and just get the message once.

$oneBody = array();
$delimiter = $body[0];
$oneBody_i = 0;
for($body_i=3; $body_i<count($body); $body_i++) // starts on 3 to skip the top of the body stuff
{
  if($body[$body_i] == $delimiter)
      break;
  $oneBody[$oneBody_i] = $body[$body_i];
  $oneBody_i++;
}

I needed to run some regular expressions on the body and imploded it into a single string.

$bodyString = implode('', $oneBody);

Now I have the body of my e-mail message as a string variable ready for processing. Alternatively, you can change the first parameter in implode( to a line break if you need to display the message not as one long string.

Lastly, once you process your messages, you don’t need them anymore.

if(($error=$pop3->DeleteMessage($index))=="")
  echo "<PRE>Marked message $index for deletion.</PRE>\n";

The DeleteMessage( method will mark messages for deletion when you close the connection to your mail server. Please note that this method does not immediately delete the messages and messages can still be ‘un-deleted’ until the connection is closed. The ResetDeletedMessages( method is used to un-mark all messages for deletion.

Happy Parsing!!

—Chris




Like this post? Flattr me!

Comments