How-To

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.

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.

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

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

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.

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

Image

Christopher R Marshall

@codegoalie

Enjoys programming web applications; especially in Go and Ruby. Also enjoys playing ice hockey as a goalie and playing the guitar.

Categories