PHP IMAP - Connection Failed

Posted August 10, 2018 7.1k views
PHPEmailUbuntu 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…

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

$mbox = imap_open("{localhost:143/imap}", "", "johnspassword", OP_HALFOPEN);

Gives the errors

Warning: imap_open(): Couldn't open stream {localhost:143/imap} in /var/www/ 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.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
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,

  • 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 993
    Connected to
    Escape character is '^]'.
    Connection closed by foreign host.

    From the mail.log file:

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

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