Connecting to Gmail through IMAP with PHP - SSL context failed

Clint Miller picture Clint Miller · Aug 14, 2009 · Viewed 59.2k times · Source

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?

Answer

Jordan S. Jones picture Jordan S. Jones · Aug 17, 2009

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&pli=1