imap_open : couldn't open stream to my mail server

52,575

Solution 1

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!

Solution 2

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

Solution 3

In my case, this did the trick:

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

Solution 4

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.

Share:
52,575

Related videos on Youtube

PASTAGA
Author by

PASTAGA

French PHP Web Developer Backend/Frontend

Updated on July 09, 2022

Comments

  • PASTAGA
    PASTAGA almost 2 years

    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..

    • Vivek Muthal
      Vivek Muthal about 10 years
      what error are you getting?
    • PASTAGA
      PASTAGA about 10 years
      Cannot connect: Array ( [0] => Connection failed to mail.domain.com,143: Connection timed out )
    • Vivek Muthal
      Vivek Muthal about 10 years
      this error occurs when server doesn't have IMAP Server running. If you are trying to connect gmail or any other mail server then you have to enable imap from settings. And if you have VPS then check you dovecot log.
    • Max
      Max about 10 years
      If you are using SSL, you likely need port 993. If you're using plaintext, it will be 143. If you're using STARTTLS, it will be 143. Can you telnet from you web host to mail.domain.com 143 and get a * OK prompt?
  • Max
    Max about 10 years
    Your comments above contradict that: you say you have a self-signed certificate, which would only be useful for SSL.
  • PASTAGA
    PASTAGA about 10 years
    Yes I'am sorry. I use SSL protocole but it uses self-signed certificates.
  • Max
    Max about 10 years
    The default IMAP secure port is 993, not 585.
  • Sunil Bhawsar
    Sunil Bhawsar almost 8 years
    Same problem when i try to download too big email it is showing.... imap_open(): Couldn't open stream {imap.gmail.com:123/imap/ssl}INBOX in,,,,,,,,,,,,,Cannot connect to Gmail: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: support.google.com/mail/acco..........what mistake am i doing please let me know