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

4
  • try stackoverflow.com?
    – Chris_K
    Aug 22, 2009 at 0: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?
    – Alexander
    Aug 22, 2009 at 0:43
  • 1
    you need to specify -z ssl when using telnet-ssl otherwise you will get what you got.
    – sybreon
    Aug 22, 2009 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?!?!?!
    – Alexander
    Aug 22, 2009 at 21:11

3 Answers 3

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

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.

4
  • 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
    – Alexander
    Aug 22, 2009 at 2:43
  • That's a pretty normal output for an initial telnet connection. Chances are it connected. Aug 22, 2009 at 3:50
  • If you telnet successfully to gmail, you will receive "* OK Gimap ready for requests from..."
    – sybreon
    Aug 22, 2009 at 12:55
  • @Alexander: Your telnet session is not running over SSL. You will not get anything that way.
    – sybreon
    Aug 22, 2009 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

4
  • 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.
    – Alexander
    Aug 22, 2009 at 0: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, 2009 at 0:54
  • Ok, now i am able to connect to it through telnet, however still getting errors!
    – Alexander
    Aug 22, 2009 at 1: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, 2009 at 1:38

You must log in to answer this question.