The issue here is the "locally with MAMP." the imap_open() command is relying on the imap php library (http://www.php.net/manual/en/ref.imap.php). Which can either be compiled with or without SSL support. MAMP, and MAMP PRO do not come with an ssl imap, so, the only way to get around this and make IMAP work with SSL (which is what is required for google apps/gmail) is to compile the IMAP extension for yourself.
This (https://www.activecollab.com/forums/topic/5844/) is where I got the following information. Hopefully this is helpful:
Snow Leopard Server has PHP 5.3 built-in (hooray!), but without the IMAP module (drat!). There are several guides out there that have partial solutions to the problem, but most ask you to build your own Apache and PHP apart from the OSX native. I wanted to use as much native OSX as possible. One blog in particular was the easiest solution (with a few minor corrections in the blog comments). Here are all the instructions, in order, with all the corrections inline:
1 Open a Terminal session from OSX
2 Download and unpack the latest IMAP source code (2007f as of this writing):
curl -o imap.tar.gz ftp://ftp.cac.washington.edu/imap/imap-2007f.tar.gz
tar -zxvf imap.tar.gz
3 Build IMAP libraries and copy to correct locations:
make osx EXTRACFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
sudo mkdir /usr/local/include (optional)
sudo cp c-client/*.h /usr/local/include/
sudo mkdir /usr/local/lib (optional)
sudo cp c-client/*.c /usr/local/lib/
sudo cp c-client/c-client.a /usr/local/lib/libc-client.a
4 Download and unpack the source code of whatever version of PHP you are using. I was using 5.3.14
curl -o php.tar.gz http://us.php.net/distributions/php-5.3.14.tar.gz
tar -zxvf php.tar.gz
5 We only need the source for the IMAP extension, so there's no need to compile all of PHP.
--The following info is system dependent. I was on snow leopard so this worked for me. You might have to look this up somewhere else.
CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
LDFLAGS="-arch i386 -arch x86_64 -bind_at_load"
./configure --with-imap=/usr/local/imap-2007 --with-kerberos --with-imap-ssl=/usr/
6 If you received errors (specifically, a complaint about OpenSSL's evp.h), you may need to adjust the path at the end of "with-imap-ssl" to /usr/include or /usr/include/openssl. The above worked for me, using Snow Leopard 10.6.8.
7 Now make the module:
8 If you didn't receive an error, go to the next step. If you received an error about pcre.h (notably on MAMP instead of Snow Leopard Server), you need to copy some files from the downloaded PHP source
sudo cp ../pcre/pcrelib/pcre.h /usr/include/php/ext/pcre/
9 And now we copy it into our extensions directory. Your directory might be named differently, depending on what version of php you have configured.
sudo cp modules/imap.so /Applications/MAMP/bin/php/php5.3.14/lib/php/extensions/no-debug-non-zts-20090626/
10 You may have to add the following to your php.ini file, but I have a hunch that extension=imap.so is already there:
11 Restart apache