2

In the project I am working on right now, I am trying to add the functionality where I can change the status of a ticket from 'closed' to 'reopened' when the user sends an email to the support desk. I would also like to save their email reply to the database.

The problem I am running into is that I cannot get PHP's IMAP functions to work on my current Apache configuration. From looking at quite a few posts here at stackoverflow and other places, it seems that the problem is OpenSSL is not enabled in the standard configuration. So for example, when I run this code:

<h1>IMAP testing!</h1>
<?php
$connect = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$user    = "my email address @gmail.com";
$pass    = "my password";

$mailbox = imap_open($connect, $user, $pass);
?>

I get the error:

Can't open mailbox {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX: invalid remote specification.

Is there anything I can do, short of recompiling PHP, to mimic IMAP functionality on my local machine to be able to continue developing the functionality of working with email (email piping)?

Some notes on my current configuration, just in case it helps:

  • OS X 10.7.4
  • PHP v.5.3.1

UPDATE -

I am almost there (thanks to dAm2K)!

I installed Stunnel (using Mac Ports), got everything configured finally and ran the command:

sudo stunnel /opt/local/etc/stunnel/stunnel.conf -c -d 127.0.0.1:443 -r imap.gmail.com:993

(For whatever reason I had to add the path to the .conf file)

Now my code looks like this:

<?php
$connect = "{localhost:443}INBOX";
$user    = "my email address @gmail.com";
$pass    = "my password";

$mailbox = imap_open($connect, $user, $pass);
?>

Now when I load the page, it just hangs for 30 seconds or so and gives the warning:

Notice: Unknown: Connection failed to localhost,443: Operation timed out (errflg=2) in Unknown on line 0

What is interesting is that if I change $connect to:

$connect = "{localhost:443/ssl}INBOX";

or

$connect = "{localhost:443/novalidate-cert}INBOX";

I get the original error, which was:

Notice: Unknown: Can't open mailbox {localhost:443/novalidate-cert}INBOX: invalid remote specification (errflg=2) in Unknown on line 0

Any ideas? Just a guess but could it maybe be something to do with the setup of stunnel, like having a self-signed cert or something with the stunnel.conf file I am missing?

Thanks a lot.

Tim

BigHeadCreations
  • 1,583
  • 3
  • 16
  • 31

1 Answers1

1

You probably have a firewall that blocks outgoing TCP packets going to imap.gmail.com on port 993.

Ask your sysadmin to check for outgoing TCP on dport 993 (imaps). Also check if your DNS is resolving imap.gmail.com:

The command:

telnet imap.gmail.com 993

should give you a valid connection. If it doesn't succeed you found the problem.

You may want to install a IMAP server on your development machine so to continue the development offline... you can install "courier imap" package but it's not a very simple task...

If the connection succeded and the command:

openssl s_client -connect imap.gmail.com:993

give you a valid connection, the problem could be that your libc-client doesn't have SSL support compiled in. In this case you cannot use imaps with PHP, and you could use the "stunnel" command to forward clear traffic originating on your local machine going encrypted to gmail IMAP servers.

The command:

stunnel -c -d 127.0.0.1:443 -r imap.gmail.com:993

should do the trick. This way you can connect your PHP script to 127.0.0.1:443:

<?
  $connect = "{localhost:443}INBOX";
?>
dAm2K
  • 9,923
  • 5
  • 44
  • 47
  • I work from home, so I guess I am the sysadmin :). Sorry but I am not sure on how to check for outgoing TCP on dport 993 or to see if DNS is resolving to imap.gmail.com. Any tips on that? The command: telnet imap.gmail.com 993 returns: Connected to gmail-imap.l.google.com. – BigHeadCreations May 14 '12 at 12:47
  • OK, so the connection seems to work. Not a OSX expert but chek this command: openssl s_client -connect imap.gmail.com:993 – dAm2K May 14 '12 at 12:57
  • So, after entering the above command, it returns: CONNECTED(00000003). Next it does say: unable to get local issuer certificate. Not sure if that is a problem or not. It then goes on to show the server certificate, then shows the SSL handshake, some SSL-Session data. Last it says '* OK Gimap ready for requests from 114.33.41.22 u70if18010275yhm.106. read:errno=0.' – BigHeadCreations May 14 '12 at 13:09
  • OK. Read my edited answer. You can solve your problem using the stunnel command. I'm a linux user, I really don't know if stunnel is OK on osx... – dAm2K May 14 '12 at 13:28
  • This looks like what I am looking for. But when I type stunnel in terminal it returns 'command not found'. Looks like I have to install stunnel. I let you know what happens. BTW, thanks a lot dAm2K for your help! – BigHeadCreations May 14 '12 at 14:52
  • 1
    Check at gmail.com - google account is it IMAP OPENED in settings? Today, I create new account it was IMAP turned off. – Sergey May 15 '12 at 06:37
  • @Sergey - thanks for the tip but I already configured Gmail to work with IMAP/POP3. – BigHeadCreations May 16 '12 at 13:23
  • @dAm2K or whoever can help. Please see my edit in the original question above. Thanks. – BigHeadCreations May 16 '12 at 13:40
  • @BigHeadCreations, I think you have try another Imap account, like your internet provider's email box. And I suggest: recompile/reinstall PHP with ssl/imap-ssl. see also here http://stackoverflow.com/questions/6727276/php-imap-open-invalid-remote-specification-when-trying-connect-to-gmail – Sergey May 16 '12 at 13:47