Search code examples
phpsslimapgmail-imap

PHP is certificate error when using imap open imap.gmail.com:993/imap/ssl


I am trying to use PHP imap to connect to a gmail box. My connection line is:

$connection = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', 'username', 'password')

It then gives me an error saying: "PHP Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /sbc/phpscripts/lar/gm2.php on line 12 Cannot connect to Gmail: Certificate failure for imap.gmail.com: self signed certificate: /OU=No SNI provided; please fix your client./CN=invalid2.invalidPHP Notice: Unknown: Certificate failure for imap.gmail.com: self signed certificate: /OU=No SNI provided; please fix your client./CN=invalid2.invalid (errflg=2) in Unknown on line 0"

I can get it to connect using the novalidate-cert flag and enabling the allow less secure apps in gmail, but that is not secure and every solution that I find says to do that. Gmail says "allow less secure apps" will not be allowed after 5/30/22, so this is not an option and I suspect this is a issue that is going to cause a lot of people a problem in the near future.

I am running on rhel using php 8.

Can someone please explain how to get this set up so that I can connect securely to gmail? Do I need to install a certificate on my linux server? If so, how do I do that?


Solution

  • $connection = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', 'username', 'password')

    Add the novalidate-cert parameter and it should work