views:

2559

answers:

4

I'm trying to connect to Gmail through IMAP with PHP running in Apache. This is on an Ubuntu 9.04 system. I've got some sort of PHP configuration issue that is keeping this from working. First, here's what I did to setup IMAP for PHP:

sudo apt-get install libc-client2007b libc-client2007b-dev
sudo apt-get install php5-imap
sudo /etc/init.d/apache2 start

When I run phpinfo(), I get the following imap values:

IMAP c-Client Version: 2004
SSL Support: enabled
Kerberos Support: enabled

Here's my sample code:

<?php
$connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$user = 'my gmail address';
$password = 'my gmail password';

$connection = imap_open($connect_to, $user, $password)
  or die("Can't connect to '$connect_to': " . imap_last_error());

imap_close($connection);
?>

When I execute this code, I get the following output:

Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in /var/www/clint/gmail/gmail.php on line 10
Can't connect to '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX': TLS/SSL failure for imap.gmail.com: SSL context failed

Note that I can telnet to imap.gmail.com:993 from this computer. I can also hookup Evolution (mail reader) to Gmail through IMAP and fetch mail without problems. So, I don't think this is a firewall issue. I'm pretty sure I've got something in PHP not setup correctly.

Any ideas?

A: 

Check your setup with phpinfo() and ensure you see --with-imap-ssl listed.

ayman
I'm not building PHP myself. I've installed it with apt-get. All the blogs I've read indicate that if I do "apt-get install php5-imap" then it will work. Maybe I have to give up on that approach and compile PHP myself. I'll try that tonight.
Clint Miller
checking phpinfo() will tell you if SSL is enabled. It sounds like it may no longer be included by default.
TheJacobTaylor
You should have openssl in phpinfo (I believe that is what IMAP is using):opensslOpenSSL support enabledOpenSSL Version OpenSSL 0.9.7i 14 Oct 2005
TheJacobTaylor
A: 

Run your code from the command line and see if php spits our any other errors:

php -f gmail.php

On my Ubuntu I did:

sudo apt-get install php5-imap

And the system installed: libc-client2007b mlock libc-client2007b mlock php5-imap

Then how about uninstalling php5 and re-installing cleanly.

Derick
+1  A: 

One more additional thing you need enabled in PHP, is the OpenSSL extension. It appears that the IMAP Client library (with SSL) depends on this.

It doesn't matter if Apache has the OpenSSL module enabled as this is processed/handled before the request is handed off to PHP.

The following discussion thread may help shed some light:

http://groups.google.com/group/comp.lang.php/browse_thread/thread/241e619bc70a8bf4/bd3ae0c6a82409bc?lnk=raot&amp;pli=1

Jordan S. Jones
A: 

This has worked for me after a lonmg effort:

$ServerName = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox"; [][]

Supriya