title: How to resolve 'SMTP Error: Could not connect to SMTP host.' in phpmailer?
body: <p>I'm trying to send email using my gmail account but i am getting an error</p>
  
  <blockquote>
    <p>"SMTP Error: Could not connect to SMTP host".</p>
  </blockquote>
  
  <p>I have tried port 587,465 25 but still its not working.</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['submit']))
  {
  
      $message=
          'Full Name: '.$_POST['fullname'].'&lt;br /&gt;
          Subject:    '.$_POST['subject'].'&lt;br /&gt;
          Phone:  '.$_POST['phone'].'&lt;br /&gt;
          Email:  '.$_POST['emailid'].'&lt;br /&gt;
          Comments:   '.$_POST['comments'].'
         ';
  require "phpmailer/class.phpmailer.php"; //include phpmailer class
  
  // Instantiate Class
  $mail = new PHPMailer();
  
  // Set up SMTP
  $mail-&gt;IsSMTP();                // Sets up a SMTP connection
  $mail-&gt;SMTPAuth = true;         // Connection with the SMTP does require authorization
  $mail-&gt;SMTPSecure = "ssl";      // Connect using a TLS connection
  $mail-&gt;Host = "smtp.gmail.com";  //Gmail SMTP server address
  $mail-&gt;Port = 465;  //Gmail SMTP port
  $mail-&gt;SMTPSecure = "tls";
  
  $mail-&gt;Encoding = '7bit';
  
  // Authentication
  $mail-&gt;Username   = "rhlsngh302@gmail.com"; // Your full Gmail address
  $mail-&gt;Password   = "*********"; // Your Gmail password
  
  // Compose
  $mail-&gt;SetFrom($_POST['emailid'], $_POST['fullname']);
  $mail-&gt;AddReplyTo($_POST['emailid'], $_POST['fullname']);
  $mail-&gt;Subject = "New Contact Form Enquiry";      // Subject (which isn't required)
  $mail-&gt;MsgHTML($message);
  
  // Send To
  $mail-&gt;AddAddress("rhlsngh302@gmail.com", "RahulName"); // Where to send it - Recipient
  $result = $mail-&gt;Send();        // Send!
  $message = $result ? 'Successfully Sent!' : 'Sending Failed!';
  unset($mail);
  
  }
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
       &lt;title&gt;Contact Form&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;div style="margin: 100px auto 0;width: 300px;"&gt;
          &lt;h3&gt;Contact Form&lt;/h3&gt;
          &lt;form name="form1" id="form1" action="" method="post"&gt;
                  &lt;fieldset&gt;
                    &lt;input type="text" name="fullname" placeholder="Full Name"    /&gt;
                    &lt;br /&gt;
                    &lt;input type="text" name="subject" placeholder="Subject" /&gt;
                    &lt;br /&gt;
                    &lt;input type="text" name="phone" placeholder="Phone" /&gt;
                    &lt;br /&gt;
                    &lt;input type="text" name="emailid" placeholder="Email" /&gt;
                    &lt;br /&gt;
                    &lt;textarea rows="4" cols="20" name="comments" placeholder="Comments"&gt;&lt;/textarea&gt;
                    &lt;br /&gt;
                    &lt;input type="submit" name="submit" value="Send" /&gt;
                  &lt;/fieldset&gt;
          &lt;/form&gt;
          &lt;p&gt;&lt;?php if(!empty($message)) echo $message; ?&gt;&lt;/p&gt;
      &lt;/div&gt;              
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How can I solve this problem?</p>