1

I have some php code that I'm trying to use to connect to gmail using imap. Here's the code:

$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$tmp_username = 'username';
$tmp_password = 'password';
$inbox = imap_open($hostname, $username, $password) or die(imap_last_error());

And I get this error output everytime i try to connect:

Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in /var/www/PHP/EmailScript.php on line 14 Login aborted

I dont understand what could be wrong!! I've heard of people having SSL errors but this doesnt seem to be one of those. Please please please help me!!!!!

Edit: When trying to connect to imap.gmail.com through telnet-ssl i get the following output:

Trying 74.125.155.109... Connected to gmail-imap.l.google.com. Escape character is '^]'.

And Nothign else happens

  • try stackoverflow.com? – Chris_K Aug 22 '09 at 00:29
  • for some reason not able to reach the gmail server using telnet imap.gmail.com 993 through the server machine. Can someone help please? –  Aug 22 '09 at 00:43
  • 1
    you need to specify `-z ssl` when using telnet-ssl otherwise you will get what you got. – sybreon Aug 22 '09 at 12:58
  • @sybreon ok that worked! i used this command: (telnet-ssl -z ssl imap.gmail.com 993) and it connected successfully to gmail. Now, why can't I do it with my php?!?!?! –  Aug 22 '09 at 21:11

3 Answers3

2

Use this command to start up the session....

openssl s_client -connect imap.gmail.com:993

if I do that I get this...

* OK Gimap ready for requests from <ip_address> 16if41262875qci.66

casey
  • 218
  • 1
  • 8
1

If you telnet directly to 993, it should not work correctly because telnet is not on SSL. Use something like telnet-ssl or stunnel instead to ensure that you are connecting to 993 over SSL. Once that is done, then try to see if you get the welcome message and then try a few IMAP commands to see if they work.

sybreon
  • 7,405
  • 1
  • 21
  • 20
  • Well this is the output I get "Trying 74.125.155.109... Connected to gmail-imap.l.google.com. Escape character is '^]'." Nothing else though –  Aug 22 '09 at 02:43
  • That's a pretty normal output for an initial telnet connection. Chances are it connected. – Mark Henderson Aug 22 '09 at 03:50
  • If you telnet successfully to gmail, you will receive "* OK Gimap ready for requests from..." – sybreon Aug 22 '09 at 12:55
  • @Alexander: Your telnet session is not running over SSL. You will not get anything that way. – sybreon Aug 22 '09 at 12:56
0

First ensure you can connect to the IMAP server from the machine:

telnet imap.gmail.com 993

If the connection works than check if SSL is installed with php-imap, using phpinfo() you must see something like this

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

Other ideas in a similar thread in StackOverflow

hdanniel
  • 4,293
  • 23
  • 25
  • Hmmm, i am not able to connect to imap.gmail.com 993 from the machine, very weird! Why do you think this is? I do have SSL installed with php imap though. –  Aug 22 '09 at 00:43
  • Check if other machines can connect. I think your firewall is not allowing your machine or your server to connect through port 993. – hdanniel Aug 22 '09 at 00:54
  • Ok, now i am able to connect to it through telnet, however still getting errors! –  Aug 22 '09 at 01:26
  • Are you sure the other variables are OK? First you use $tmp_username and $tmp_password and with imap_open you use $username and $password. Try to connect without using variables to check if everything is fine and don't forget that username must contain the domain. – hdanniel Aug 22 '09 at 01:38