Beyond Web Pages: Using PHP to Parse Email Messages

So here at HybridCluster this last week, I’ve been reviewing our operations management procedures and how we can take advantage of our own software to provide a better level of support to the organisations using it.

Our software continually monitors itself and the state of the machines on which it’s running. If something such as disk usage that might conceivably become an issue is detected to be reaching a threshold at which some human attention might be a good idea, our system will dispatch alert notices to the designated technical contact email.

But how to sort all these incoming emails and separate the routine from the high priority? Our customer-facing Launchpad is written in PHP, the server side scripting language that powers approximately 80% of dynamic websites whose implementation language can be known. Seeing as we already have a system that groups all the useful information about a cluster in one place, it made sense to me to improve our support tools in that system by taking advantage of how PHP can be used for all sorts of interesting tasks besides generating web pages – in this case, using PHP’s bundled IMAP extension to communicate with a mail server, retrieve alerts and notices about a cluster and parse that data, storing the messages that are most important in our support database and linking them directly to our records about the cluster concerned.

You too can take advantage of PHP’s mail handling capabilities for your own benefit – the use cases of such a PHP script are wide-ranging; we’re using this technique to monitor incoming alerts from a cluster, but you could use the same principles to help build an online customer support desk, an internal company calendar, a WordPress plugin to post your blog entries by sending an email to a particular address, etc.

Setting up a basic email parser in PHP is easy and with the method we’re going to be examining in this blog post, you won’t even have to deal with the minimal technical hassle of setting up your servers’ incoming mail transfer agent to pipe raw messages to a script, because we’re going to login via IMAP and retrieve our messages directly from a mailbox.

So for this technique, the first thing you should do is head on over to GitHub and grab yourself a copy of this very nifty PHP IMAP class – it’s going to do the core of our work, exposing to our main script a nice and simple interface to quickly grab our messages and peek at the contents.

Extract your downloaded archive and drop ImapMailbox.php in to your include path. Ensure your PHP installation includes a build of the IMAP extension – if it doesn’t, you can follow the instructions given in the PHP documentation. Once you’ve got these bits sorted, we’re good to write a quick example script to make a connection to our mailbox, pull a list of messages and access ready-parsed data that we can use to inspect the emails and treat them as we wish in accordance with their subject, from address, message body or whatever else.

define('ATTACHMENTS_DIR', dirname(__FILE__) . '/attachments');
define('IMAP_USERNAME', '');
define('IMAP_PASSWORD', 'somesecurepassword');
define('IMAP_SERVER', '');
define('IMAP_PORT', '993');
$mailbox = new ImapMailbox('{'.IMAP_SERVER.':'.IMAP_PORT.'/imap/ssl}INBOX', IMAP_USERNAME, IMAP_PASSWORD, ATTACHMENTS_DIR, 'utf-8');
$messages = $mailbox->searchMailbox('FROM'); // $messages is now an array containing a list of message ID numbers
foreach ($messages as $id) {
    $message = $mailbox->getMail($id);
    // $message is now an object containing the parsed message, allowing us to easily access the subject, header components, body and any attachments
    echo "This message has subject {$message->subject} and was sent from {$message->fromAddress} on {$message->date}";

The $message object in the script above has several publicly accessible properties – we’ve used subject, fromAddress and date, however you can also access fromName, replyTo, to, toString, cc, textPlain, textHtml and attachments.

Now this parsed data is available, you can modify the example script above to perform all sorts of useful tasks on each message retrieved. Parse the From address or subject line with a regular expression, send an email back to a customer assigning them a support ticket reference, insert the message body in to a database, etc.

Finally it’s worth mentioning that the IMAP extension can also be used to access POP3 and NNTP protocols – despite the name, you’re not actually limited to just IMAP!

Happy coding everyone! :)

Leave a Reply

Your email address will not be published. Required fields are marked *

— Back to top —