Goku
Goku

Reputation: 2139

imap_open : couldn't open stream to my mail server

Notice: Unknown: Connection failed to mail.domain.com,143: Connection timed out (errflg=2) in Unknown on line 0

imap_open("{mail.domain.com:143/novalidate-cert}INBOX", 'login', 'password')

The port 143 is open, I'm not behind a firewall, my server uses self-signed certificates.

I really don't understand why I can not connect to my mail server

I searched everywhere but I found no answer..

Upvotes: 20

Views: 65984

Answers (6)

Salem
Salem

Reputation: 774

For yahoo email this is what work for me

imap_open( "{imap.mail.yahoo.com:993/imap/ssl/novalidate-cert}INBOX", "Email", "PASSWORD" );

Upvotes: -1

Shahzaib Chadhar
Shahzaib Chadhar

Reputation: 117

I had the same issue for myself. As you can see.

enter image description here

So, How I solved this:

Go to Your Google Account, You have to change the Security Settings.

Go to Security Tab.

Scroll down and you will find Signing in to Google

Turn OFF these two settings:

  1. Use your phone to sign in
  2. 2-Step Verification

And Turn Less secure app access ON.

That's all, It worked for me and hope will work for you.

Upvotes: 0

Goku
Goku

Reputation: 2139

Thank you for your answers. My mistake was not coming from the connection but rather a loop that crashed the server when I had too much email :

imap_open("{mail.domain.com:143/novalidate-cert}INBOX", 'login', 'password')

$mails = imap_search($stream, 'UNSEEN');

rsort($mails);
foreach ($mails as $mailId) {
  imap_fetch_overview($stream, $mailId, 0);
} //that was the mistake when email number is too big!

Upvotes: 12

MakG
MakG

Reputation: 1274

In my case, the imap extension was compiled without the --with-imap-ssl option. You need to pass it to configure or if you're using Docker:

docker-php-ext-configure imap --with-imap-ssl
docker-php-ext-install imap

You can verify if there is an SSL Support using this command:

php -i | grep imap -A 5

If there is no "SSL Support => enabled" string, you need to recompile the imap extension.

Lack of SSL Support was resulting in not very clear error messages:

Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
Can't open mailbox {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX: invalid remote specification

Upvotes: 1

Adam Fischer
Adam Fischer

Reputation: 1100

My guess is that you are behind ssl (default port 993)

Try

imap_open("{mail.domain.com:993/imap/ssl/novalidate-cert}INBOX", 'login', 'password') or die('Cannot connect: ' . print_r(imap_errors(), true))

Dont forget to open that port

Upvotes: 1

Carl
Carl

Reputation: 149

In my case, this did the trick:

imap_open("{mail.domain.com:110/pop3/notls}INBOX", 'login', 'password')

Upvotes: 2

Related Questions