Getting error like this imap_open(): Couldn't open stream

5.3k views Asked by At

I am trying to access my company mail id (the mail provider is Gmail) inbox using IMAP. I have applied almost all the possible solutions available here and also on google. But I am facing the same issue.

These are the some solutions which I tried,

  1. allow less secure app access Gmail
  2. enabled IMAP access from Gmail
  3. added novalidate-cert

Here's my code:

$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = '[email protected]';
$password = 'mypassword';

// try to connect 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . 
imap_last_error());

Error message

Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX 
in \path\to\file\index.php on line 12

I don't see where I'm going wrong. Please help...

2

There are 2 answers

1
Orgoth Dorngree On

Google has deactivated the regular login for third parties on 2022-05-31!

https://support.google.com/accounts/answer/6010255

It is now necessary to implement the Google PHP lib.

https://github.com/googleapis/google-api-php-client
https://github.com/googleapis/google-api-php-client/blob/main/docs/oauth-web.md

The option for less secure apps has also been removed by Google, which now also eliminates this possibility. https://myaccount.google.com/lesssecureapps

0
Zoidberg On

I ran into the same issue and solved it by generating an app-password for my application.

see: How can I read emails with Google Gmail?