title: Fetching Gmail Using PHP: imap_open(): Couldn't open stream
body: <p><strong>Preface:</strong></p>
  
  <p>I've read several StackOverflow threads and have tried virtually all of them. </p>
  
  <ol>
  <li>I’m trying to set up IMAP via PHP to fetch some emails using <a href="https://github.com/Clivern/Imap" rel="nofollow noreferrer">Clivern/Imap</a></li>
  <li>I've also tried David Walsh's post: <a href="https://davidwalsh.name/gmail-php-imap" rel="nofollow noreferrer">Retrieve Your Gmail Emails Using PHP and IMAP</a></li>
  </ol>
  
  <p><strong>Things I have done:</strong></p>
  
  <ol>
  <li>Tried this using both a GSuite and Gmail account; IMAP is enabled on both.</li>
  <li>Allowed <a href="https://support.google.com/accounts/answer/6010255" rel="nofollow noreferrer">'Less Secure Apps' to connect</a> (even though I am running under a valid SSL). <a href="https://groups.google.com/forum/#!topic/comp.lang.php/JB5hm8cKi_Q" rel="nofollow noreferrer">OpenSSL is installed</a> <em>(OpenSSL 1.0.2o 27 Mar 2018)</em></li>
  <li>I've completed <a href="https://accounts.google.com/b/0/displayunlockcaptcha" rel="nofollow noreferrer">Display Unlock Captcha</a></li>
  <li>I've tried testing in multiple local/hosted environments</li>
  </ol>
  
  <p><strong>The Issue:</strong></p>
  
  <ol>
  <li>The connection works when I use GSuite; it fails when I use Gmail (I have checked the email/password multiple times and compared IMAP settings side-by-side...they are all OK)</li>
  <li>The error I get when attempting to check Gmail via the Clivern Library is: <code>Warning --&gt; imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX</code></li>
  <li>The error I get when attempting to check Gmail via the PHP functions is: <code>Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure)</code></li>
  </ol>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>// Using Library
  use Clivern\Imap\Core\Connection;
  use Clivern\Imap\MailBox;
  
  $imap_email = 'my_email@gmail.com';
  $imap_password = 'my_password';
  
  $connection = new Connection(
      "imap.gmail.com",
      "993",
      $imap_email,
      $imap_password,
      "/ssl", // I've also tried /ssl/novalidate-cert
      "INBOX"
  );
  
  $connection-&gt;connect();
  $mailbox = new MailBox($connection);
  $folders = $mailbox-&gt;getFolders();
  echo var_dump($folders);
  $connection-&gt;disconnect();
  
  // Using PHP functions
  $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
  $username = $imap_email;
  $password = $imap_password;
  
  /* try to connect */
  $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
  </code></pre>
  
  <p><strong>My Question:</strong></p>
  
  <p>What else could be causing Gmail to fail to authenticate?</p>
  
  <p>...I'm particularly confused why GSuite would work and Gmail wouldn't.</p>