title: SMTP connect failed - PHPMailer Error on Localhost
body: <p>I was using PHPMailer latest version and I was able to send emails using it. But now I don't know what had happened? Now I'm not able to send mail using it. It gives me following error -</p>
  
  <pre><code>Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  </code></pre>
  
  <p>I have also checked openssl extension.</p>
  
  <p>Here is my code -</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form method="post" action="mail.php"&gt;
    &lt;input type="text" placeholder="Enter your email to subscribe" name="emailid" /&gt;
    &lt;input type="submit" value="Submit" /&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The contents of <code>mail.php</code> file</p>
  
  <pre><code>&lt;?php
  $emailSubscr = $_POST['emailid'];
  $sub = 'New Subscriber';
  $message = $emailSubscr.' has subscribed for newsletter';
  
  require 'phpmailer/PHPMailerAutoload.php';
  
  $mail = new PHPMailer;
  
  //$mail-&gt;SMTPDebug = 3;
  
  $mail-&gt;isSMTP();
  $mail-&gt;Host = 'smtp.gmail.com';     // SMTP Host Name
  $mail-&gt;SMTPAuth = true;                               // Enable SMTP authentication
  $mail-&gt;Username = 'myusername@gmail.com';                 // SMTP username
  $mail-&gt;Password = 'mypassword';                           // SMTP password
  $mail-&gt;SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
  $mail-&gt;Port = 587;                                    // TCP port to connect to
  
  $mail-&gt;From = $emailSubscr;
  $mail-&gt;FromName = 'Subscriber';
  $mail-&gt;addAddress('me@example.com', 'Rohit Kumar');     // Add a recipient
  $mail-&gt;addReplyTo($emailSubscr, 'Subscriber');
  
  $mail-&gt;Subject = $sub;
  $mail-&gt;Body    = $message;
  
  if(!$mail-&gt;send()) {
      echo 'Some error occured, please try again later.';
      echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;
  } else {
      echo 'Thanks for subscribing our newsletter on your email id.';
  }
  ?&gt;
  </code></pre>