PHP IMAP - Connection Failed

August 10, 2018 722 views
PHP Email Ubuntu 18.04

I have successfully setup an email server for myself using this DO tutorial for Postfix, Dovecot, MySQL, and RoundCube. I skipped spamassasin. Everything works fine, I can send and receive emails, and manage them through roundcube or my iphone.

But now I have spent 8 hours on this PHP problem, here's the simplest I can reproduce it...

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$mbox = imap_open("{localhost:143/imap}", "john@example.com", "johnspassword", OP_HALFOPEN);
?>

Gives the errors

Warning: imap_open(): Couldn't open stream {localhost:143/imap} in /var/www/example.com/public_html/test.php on line 6
Notice: Unknown: Connection failed to localhost,143: Connection refused (errflg=2) in Unknown on line 0

That is the error that shows for the php output. I can't seem to find a log file with the same information. All I want is to connect to my emails.

1 comment
  • port 993 has the same issue. I found the log file, nothing helpful there either. Up to about 10 hours of work on this now... Any guidance would be GREATLY appreciated.

1 Answer

Hello friend!

This is definitely an interesting one because from the way it sounds on one end, IMAP services are up and running. From the other end, sounds like they may not be. There's some clear disconnect there, so perhaps the configuration is more complex than average.

I would install telnet on the server and run this:
telnet localhost 143

It's just a quick way to see what you get when connecting to the port to/from localhost. A firewall entry could be blocking the internal connection (odd but possible). I can't think of what else it could be, to be honest, that would cause external connections to succeed and internal ones to fail in relation to dovecot.

Kind Regards,
Jarland

  • It's a simple setup, the domain/server's only purpose in life is my email. I am leaving Yahoo because of the new Oath policy that says they can now read everyone's emails.

    I realized I had imap port 143 off. But 993 isn't working either when ssl=on.

    service imap-login {
      inet_listener imap {
        #port = 143
        port = 0
      }
      inet_listener imaps {
        #port = 993
        #ssl = yes
      }
    

    now works like this

    service imap-login {
      inet_listener imap {
        #port = 143
      }
      inet_listener imaps {
        #port = 993
        ssl = no
      }
    

    Telnet hangs for 60 seconds but gives no details to what happened.

    root@example:~# telnet example.com 993
    Trying 127.0.1.1...
    Connected to example.com.
    Escape character is '^]'.
    Connection closed by foreign host.
    root@example:~#
    
    

    From the mail.log file:

    Aug 13 17:24:57 example dovecot: imap-login: Disconnected (no auth attempts in 60 secs): user=<>, rip=127.0.0.1, lip=127.0.1.1, TLS handshaking: SSL_accept() syscall failed: Success, session=<yMNZX1RzXNp/AAAB>
    

    No auth attempts?! Is Dovecot not listening? Where is that setup?

Have another answer? Share your knowledge.