title: PHPMailer do not work properly sometimes on gmail port 465 or 587 on localhost
body: <p>I am using the latest version of <strong>PHPMailer 6.0.2</strong> on <strong>LOCALHOST</strong> with Gmail SMTP Ports [ <strong>SSL on 465</strong> and <strong>TLS 587</strong> ]. It works well but not stable or works properly, very strange while OpenSSL Extension is also active in PHP Config /PHP.ini file.</p>
  
  <p>It returns the error " <strong>SMTP Error: Could not connect to SMTP host</strong> ", sometimes on 465 or 587 port. </p>
  
  <p>As right now it's on 587. </p>
  
  <p><strong>Here the exact error on port 587 ;</strong></p>
  
  <pre><code>2017-12-05 13:00:26 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( 'ssl' =&gt; array ( 'verify_peer' =&gt; false, 'verify_peer_name' =&gt; false, 'allow_self_signed' =&gt; true, ),)
  2017-12-05 13:00:26 Connection: opened
  2017-12-05 13:00:26 SMTP INBOUND: "220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp"
  2017-12-05 13:00:26 SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp
  2017-12-05 13:00:26 CLIENT -&gt; SERVER: EHLO localhost
  2017-12-05 13:00:27 SMTP INBOUND: "250-smtp.gmail.com at your service, [110.36.136.72]"
  2017-12-05 13:00:27 SMTP INBOUND: "250-SIZE 35882577"
  2017-12-05 13:00:27 SMTP INBOUND: "250-8BITMIME"
  2017-12-05 13:00:27 SMTP INBOUND: "250-STARTTLS"
  2017-12-05 13:00:27 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
  2017-12-05 13:00:27 SMTP INBOUND: "250-PIPELINING"
  2017-12-05 13:00:27 SMTP INBOUND: "250-CHUNKING"
  2017-12-05 13:00:27 SMTP INBOUND: "250 SMTPUTF8"
  2017-12-05 13:00:27 SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [110.36.136.72]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
  2017-12-05 13:00:27 CLIENT -&gt; SERVER: STARTTLS
  2017-12-05 13:00:27 SMTP INBOUND: ""
  2017-12-05 13:00:27 SERVER -&gt; CLIENT: 
  2017-12-05 13:00:27 SMTP ERROR: STARTTLS command failed: 
  SMTP Error: Could not connect to SMTP host.
  2017-12-05 13:00:27 SMTP NOTICE: EOF caught while checking if connected
  2017-12-05 13:00:27 Connection: closed
  SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting     
  Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  </code></pre>
  
  <p><strong>HERE IS THE RELEVANT CODE:</strong> </p>
  
  <pre><code>use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\OAuth;
  use League\OAuth2\Client\Provider\Google;
  
  require '../../vendor/autoload.php';
  $mail = new PHPMailer;
  $mail-&gt;isSMTP();
  $mail-&gt;SMTPDebug = 4;
  
  $mail-&gt;Host = 'smtp.gmail.com';
  $mail-&gt;Port = 587;
  
  $mail-&gt;SMTPSecure = 'tls';
  $mail-&gt;SMTPAuth = true;
  $mail-&gt;SMTPAutoTLS = false;
  
  $mail-&gt;SMTPOptions = array(
      'ssl' =&gt; array(
          'verify_peer' =&gt; false,
          'verify_peer_name' =&gt; false,
          'allow_self_signed' =&gt; true
      )
  );
  
  $mail-&gt;Username = $from_address;
  $mail-&gt;Password = $from_password;
  $mail-&gt;SetLanguage("tr", "phpmailer/language");
  $mail-&gt;CharSet = "utf-8";
  $mail-&gt;Encoding = "base64";
  $mail-&gt;SetFrom($from_address, $from_name);
  
  foreach ($to_email_list as $to) {
      $mail-&gt;AddAddress($to);
  }
  
  $mail-&gt;AddReplyTo($from_address, $from_name);
  $mail-&gt;Subject = $email_subject;
  
  //Creating Email Body
  $message = "&lt;html&gt;\n";
  $message .= "&lt;body&gt;\n";
  $message .= '&lt;p&gt;Greetings,&lt;/p&gt;';
  $message .= '&lt;p&gt;' . $email_message . '&lt;/p&gt;';
  $message .= "&lt;/body&gt;\n";
  $message .= "&lt;/html&gt;\n";
  $mail-&gt;isHTML(true);
  $mail-&gt;MsgHTML($message);
  
  if(!$mail-&gt;Send()) {
      echo "On Port: " . $from_smtp_port . " &lt;/br&gt; Mailer Error: " . $mail-&gt;ErrorInfo;
  } else {
      echo "Message sent!  on Port " . $from_smtp_port . "&lt;/br&gt;";
  
     foreach($to_email_list as $list){
          echo $list . "&lt;/br&gt;";
      }
  
  }
  </code></pre>