PHP and the IMAP idiosyncrasy
So recently i’ve started working as a senior software engineer on a PHP project that, amongst a load of other things, shows the content of an IMAP mailbox. As this is a generally easy technology to work with, you’d think it would be coded sooner than you can spell idiosyncrasy (which is still a long time, if you’re me).
Problems arose on the horizon when I started noticing that PHP’s imap functions would dump any E_NOTICE’s they stumbled upon directly to STDOUT, in this case, the table that I use to display the email messages. Another fun fact is that searching and sorting using nested and/or criteria doesn’t work in one go. My colleague created a workaround by searching for each nested criteria, storing the resulting msg_id’s and then performing some filtering magic on the results.This, however, is not wat you want on a high-traphic website, each query to a database, or an imap mailbox, is one too many.
So a solution must be found.
I was made painfully aware after a day of searching that alternative IMAP libraries/components/widgets/whatchamacallits are very rare and rarely of sufficient quality, this problem is increased because of the number libraries that are (PHP4-styled) OO wrappers around the existing imap_ functions.
The solution we arrived on was using PEAR’s Net_Imap module, it is a very usable module based on Net_Socket that uses PHP’s socket functions to access the imap server. It gives you a lot of control of the communications you do with the server while keeping it high-level enough so you don’t have to type each command yourself. Sorting and searching is made pretty easy and using UID instead of Message ID is supported as well.
It does however have a couple of downsides:
- Under the hood, it uses the PEAR::isError mechanism to check for errors and handle appropriatly, we needed it to throw Exceptions to make sure it fit within the existing architecture.
- It’s for PHP4
- It doesn’t support ‘Peeking’, which makes highlighting messages as unread a distinct displeasure
- It’s for PHP4
- The response parser that handles the raw response from the server has a bug that adds [int][int]} to the start of the body content, we noticed this when we wanted to display attachments and the base64_decode kept failing
- It’s for PHP4
- UID instead of MID isn’t supported in all functions
- It’s for PHP4
After a nice couple of days of rewriting I got it to work without E_STRICT or E_NOTICES but it really needs a good update. I’m working on it in my free time but it would be nice if a developer or two with some more spare time would look at it.
And for frak sakes… Port it to PHP5 already, it’s been a while sinds they released it. Oh and if you’re at it anyways, go poke the PHP dev’s to give their old C libaries and update as well, functions just outputting notices is quite a bad practice.