Workaround for PHP IMAP functions? Trying to work

2020-03-07 06:17发布

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

1条回答
forever°为你锁心
2楼-- · 2020-03-07 06:30

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";
?>
查看更多
登录 后发表回答