title: SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed
body: <p>I am trying to send an email using Mailer but getting below error</p>
  
  <blockquote>
    <p>Connection: opening 2017-05-25 08:22:07   SMTP ERROR: Failed to connect
    to server: (0) SMTP connect() failed. Mailer Error: SMTP connect()
    failed.</p>
  </blockquote>
  
  <p>php_openssl extension &amp; IMAP both are enabled. I tried to find it on google but still no luck.</p>
  
  <p>Code:</p>
  
  <pre><code>function sendMail($subject='',$to='',$emailcontent='',$attach='')
  {
      global $_mailmsg;
      //echo $emailcontent;exit;
      $mail = new PHPMailer;
      $mail-&gt;SMTPDebug = 4;
      $mail-&gt;isSMTP();                                    
      $mail-&gt;Host = 'smtp.gmail.com';
      $mail-&gt;Port = '465';
      $mail-&gt;SMTPAuth = true;                               
      $mail-&gt;Username = 'xx@gmail.com';                            
      $mail-&gt;Password = 'xxxx';                           
      $mail-&gt;SMTPSecure = 'ssl';                                  
      $mail-&gt;From = 'xx@gmail.com';
      $mail-&gt;FromName = 'Test';
      $mail-&gt;addAddress($to);  // Add a recipient
      if(!empty($cc)){    $mail-&gt;addCC($cc); }
      if(!empty($bcc)){   $mail-&gt;addBCC($bcc);    }
  
      $mail-&gt;WordWrap = 50;                                 
      if($attach != ''){
          $mail-&gt;addAttachment($attach);    
      }
      //$mail-&gt;addAttachment('/tmp/image.jpg', 'new.jpg');   
      $mail-&gt;isHTML(true);                                  
      $mail-&gt;Subject = $subject;
      $mail-&gt;Body    = 'Test';
      try
      {
          if($mail-&gt;send()) {
              return 1;
             exit;
          }
          else
          {
              echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;
              return 0;
          }
      }
      catch(Exception $e)
      {
          return 0;   
      }
  }
  </code></pre>