PHP IMAP support in Mac OS X Lion without recompiling PHP

OK, so this was a lot harder than it should have been, and I had to use various tutorials to get it working, however I am pleased to say that I finally did get it all working without needing to completely recompile php. Here are the steps:

Extract the tar.gz file (double click it in finder or tar xvzf imap-2007f.tar.gz in terminal)

Then, modified from This Comment on the same xeonxai blog post, do the following to avoid the error:"configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information." when executing item 6 in this tutorial.cd ~/Downloads/imap-2007f
make osx EXTRACFLAGS="arch i386 -arch X86_64 -g -Os -pipe -no-cpp-precomp"
sudo cp c-client/*.h /usr/local/include/
sudo cp c-client/*.c /usr/local/lib/
sudo cp c-client/c-client.a /usr/local/lib/libc-client.a

Then download php source. Make sure you get the right source (you can check your php version by the command in the terminal: php -r "phpinfo();" I think OS X Lion standard install is php 5.3.6 which you can download at http://es.php.net/get/php-5.3.6.tar.bz2/from/a/mirror

In Terminal:cd ~/Downloads/php-5.3.6/ext/imap
phpize
./configure --with-imap==/usr/local/imap-2007 --with-kerberos --with-imap-ssl
make
At the make command, things will fail. This is the message that I got…In file included from /Users/danspencer/Downloads/php-5.3.6/ext/imap/php_imap.c:44:
/usr/include/php/ext/pcre/php_pcre.h:29:18: error: pcre.h: No such file or directory
In file included from /Users/danspencer/Downloads/php-5.3.6/ext/imap/php_imap.c:44:
/usr/include/php/ext/pcre/php_pcre.h:37: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:44: error: expected specifier-qualifier-list before ‘pcre’
make: *** [php_imap.lo] Error 1
Basically, pcre is not installed, so we need to install this first. See next step!

Now you should be able to cd to the imap extension directory in php-5.3.6 source and compile the extension:cd ~/Downloads/php-5.3.6/ext/imap
phpize
./configure --with-imap==/usr/local/imap-2007 --with-kerberos --with-imap-ssl
make
sudo cp modules/imap.so /usr/lib/php/extensions/no-debug-non-zts-20090626/

Now edit php.ini (sudo vi /etc/php.ini) and add the imap extension in (I added mine around line 990, just under the commented out windows extensions. in vi, you will need to press i to insert text):;IMAP Extension
extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/imap.so
After editing, save the file (in vi press esc to exit insert mode, then type “:x”).

now restart apache, and we should be done! sudo apachectl graceful

To test to see if we are up and running, we can do a couple of things…

php -r "phpinfo();"
We should be able to find the following in the output (the modules are in alphabetical order):imap

You can also check to see if the imap functions exist:php -r "if(function_exists('imap_open')) { echo 'imap_open exists!'; } else { echo 'imap_open does not exist'; }"
You should get an output of “imap_open exists”