title: PHPMailer SMTP connection failed error
body: <p>In my code I am just trying to send an email to a recipient using the PhPmailer tool but every time I do i get errors. I've watched numerous amounts of video tutorials on this and looked at tons of websites but no fix. I've tried changing my port from "587 to 465", change the "SMTPDebug = 2" to see clear error message, I even tried troubleshooting from the github page but I am still having no luck. Here is my code</p>
  
  <pre><code>&lt;?php
  require 'PHPMailerAutoload.php';
  require 'credential.php';
  
  $mail = new PHPMailer;
  
  $mail-&gt;SMTPDebug = 4;                               // Enable verbose debug output
  
  $mail-&gt;isSMTP();                                      // Set mailer to use SMTP
  $mail-&gt;Host = 'smtp1.gmail.com;';  // Specify main and backup SMTP servers
  $mail-&gt;SMTPAuth = true;                               // Enable SMTP authentication
  $mail-&gt;Username = EMAIL;                 // SMTP username
  $mail-&gt;Password = PASS;                           // SMTP password
  $mail-&gt;SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
  $mail-&gt;Port = 587;                                    // TCP port to connect to
  
  $mail-&gt;setFrom(EMAIL, 'Testing this');
  $mail-&gt;addAddress('testemail123@outlook.com');     // Add a recipient              // Name is optional
  $mail-&gt;addReplyTo(EMAIL);
  $mail-&gt;isHTML(true);                                  // Set email format to HTML
  
  $mail-&gt;Subject = 'Here is the subject';
  $mail-&gt;Body    = 'This is the HTML message body &lt;b&gt;in bold!&lt;/b&gt;';
  $mail-&gt;AltBody = 'This is the body in plain text for non-HTML mail clients';
  
  if(!$mail-&gt;send()) {
      echo 'Message could not be sent.';
      echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;
  } else {
      echo 'Message has been sent';
  }
  ?&gt;
  </code></pre>
  
  <p>Errors Messages</p>
  
  <pre><code>2018-02-23 07:16:07 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
  2018-02-23 07:16:07 Connection: opened
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is ""
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "220 smtp.gmail.com ESMTP s125sm758477vkb.40 - gsmtp"
  2018-02-23 07:16:07 SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP s125sm758477vkb.40 - gsmtp
  2018-02-23 07:16:07 CLIENT -&gt; SERVER: EHLO localhost
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is ""
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250-smtp.gmail.com at your service, [x.x.x.x]"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250-SIZE 35882577"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250-8BITMIME"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250-STARTTLS"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250-ENHANCEDSTATUSCODES"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250-PIPELINING"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250-CHUNKING"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING"
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "250 SMTPUTF8"
  2018-02-23 07:16:07 SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
  2018-02-23 07:16:07 CLIENT -&gt; SERVER: STARTTLS
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is ""
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $str is "220 2.0.0 Ready to start TLS"
  2018-02-23 07:16:07 SERVER -&gt; CLIENT: 220 2.0.0 Ready to start TLS
  2018-02-23 07:16:07 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [C:\wamp64\www\mailer\phpmailer\class.smtp.php line 375]
  SMTP Error: Could not connect to SMTP host.
  2018-02-23 07:16:07 CLIENT -&gt; SERVER: QUIT
  2018-02-23 07:16:07 SMTP -&gt; get_lines(): $data is ""
  2018-02-23 07:16:07 
  2018-02-23 07:16:07 
  2018-02-23 07:16:07 
  2018-02-23 07:16:07 
  2018-02-23 07:16:07 
  2018-02-23 07:16:07 Connection: closed
  SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  </code></pre>