Connect to Gmail IMAP PHP "Could not open stream"

There are many people who have similar problems, but no one answers their questions. I have IMAP enabled in PHP using all the correct information. I do not see where I am wrong.

Here is my code:

$hostname = '{imap.gmail.com:995/imap/ssl/novalidate-cert}'; $username = ' emailaddress@gmail.com '; $password = 'password'; $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); print_r(imap_errors()); 

Do not return any errors except:

Warning: imap_open () [function.imap-open]: Could not open stream {imap.gmail.com:995/imap/ssl/novalidate-cert} in / home / a 8066360 / public_html / test / imap.php in line 6

Unable to connect to Gmail: Unable to connect to gmail-imap.l.google.com, 995: Connection timeout

I noticed that if I changed single quotes to `

shell_exec () is disabled for security reasons ...

Please, help!!!

+10
source share
6 answers

You need port 993 , SSL IMAP port.

Port 995 is the POP3 SSL port.

+12
source

I think that access to IMAP Gmail can only be obtained on port 993.

$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}";

+6
source

I had the same error and found another solution. I added debugging information to the host:

 "{imap.gmail.com:993/debug/imap/ssl/novalidate-cert}INBOX"; 

When I read the php error log I found

 Unknown: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) (errflg=1) in Unknown on line 0 

Open the link and follow the instructions. Search

Your application may not support the latest security standards. Try changing a few settings to allow less secure applications to access your account.

Click the link and enable less secure access to applications.

Then it works for me.

+2
source

You can set up two-step authentication, and then assign the APP password for use in your requests (just replace your password with the one specified for the application, your regular password does not change.).

This will help your script run from any host without blocking Google (due to a change in login location).

0
source

You can try the following code, notls argument and connecting the server as follows if SSL is not used.

 $hostname = '{imap.YOUR_DOMAIN.com:143/imap/notls}INBOX'; $username = 'YOUR_USERNAME'; $password = 'YOUR_PASSWORD'; $inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error()); 

If it does not throw an error, it means that you are successfully connected to the server. Hope this helps.

0
source

You must enable this option from your Gmail account and connect to the Gmail server from another device or from the Internet. https://myaccount.google.com/lesssecureapps

0
source

All Articles