title: Gmail SMTP Error With PHP Mailer
body: <p>I know this question has been asked a lot and I've read through most of them.</p>
  
  <p>I'm getting this error when trying to use my Gmail account to send email from PHP using PHP Mailer and WAMP Server.</p>
  
  <ol>
  <li>It was working till yesterday without any problems (since about 3 months).</li>
  <li>I have not changed any code in the mailer helper function.</li>
  <li>php-openssl.dll is enabled in my wamp settings.</li>
  <li>Tried with ssl &amp; 465, Doesn't connect.</li>
  <li>I have "Allow less secured apps" turned on in my google settings.</li>
  <li>I've tried using gethostbyname(). it doesn't work.</li>
  <li>It is working with my organisation's email server, which doesn't use any security.</li>
  </ol>
  
  <p>This is my phpmailer function code. All the settings are being retrieved from the database. As it is a multi user application.</p>
  
  <pre><code>// Email Settings
      $from=$details-&gt;from;
      $mailer=$details-&gt;mailer;
      $subject=$details-&gt;subject;
  
      $mail = new PHPMailer(true);                                // Passing `true` enables exceptions
      try {
          //Server settings
          $mail-&gt;SMTPDebug = $settings-&gt;debug;                    // Enable verbose debug output
          $mail-&gt;isSMTP();                                        // Set mailer to use SMTP
          $mail-&gt;Host = $settings-&gt;host;          // Specify main and backup SMTP servers
          $mail-&gt;SMTPAuth = true;                                 // Enable SMTP authentication
          $mail-&gt;Username = $settings-&gt;login;                     // SMTP username
          $mail-&gt;Password = $pass;                                // SMTP password
          $mail-&gt;SMTPSecure = $settings-&gt;security;                // Enable TLS encryption, `ssl` also accepted
          $mail-&gt;Port = $settings-&gt;port;                          // TCP port to connect to
  
          //From
          $mail-&gt;setFrom($from, $mailer);
  
          //Recipients
          $mail-&gt;addAddress($details-&gt;to,$details-&gt;recipientName);     // Add a recipient           // Name is optional
          $mail-&gt;addReplyTo($from, $mailer);
          foreach($quote-&gt;cc as $cc)
          {
              $mail-&gt;addCC($cc);
          }
          //Attachments
          foreach($quote-&gt;attachments as $file)
          {
              $mail-&gt;addAttachment($file);    // Add attachments  // Optional name
          }
          //Content
          $mail-&gt;isHTML(true);                                  // Set email format to HTML
          $mail-&gt;Subject = $subject;
          $mail-&gt;Body    = $message;
          $mail-&gt;AltBody = 'Non-HTML Mail Clients Not Supported, Please View This email in a browser or get a HTML friendly Email Client.';
          $mail-&gt;send();
      }
      catch (Exception $e)
      {
          $return=array(1,'Error: '.$mail-&gt;ErrorInfo);
      }
  return $return;
  }
  </code></pre>
  
  <p>The error That I'm getting is </p>
  
  <pre><code>2017-12-04 07:15:26 SERVER -&amp;gt; CLIENT: 220 smtp.gmail.com ESMTP w9sm23070345pfk.16 - gsmtp&lt;br&gt;
  2017-12-04 07:15:26 CLIENT -&amp;gt; SERVER: EHLO projects&lt;br&gt;
  2017-12-04 07:15:26 SERVER -&amp;gt; CLIENT: 250-smtp.gmail.com at your service, [49.213.37.11]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8&lt;br&gt;
  2017-12-04 07:15:26 CLIENT -&amp;gt; SERVER: STARTTLS&lt;br&gt;
  2017-12-04 07:15:27 SERVER -&amp;gt; CLIENT: 220 2.0.0 Ready to start TLS&lt;br&gt;
  SMTP Error: Could not connect to SMTP host.&lt;br&gt;
  2017-12-04 07:15:27 CLIENT -&amp;gt; SERVER: QUIT&lt;br&gt;
  2017-12-04 07:15:27 &lt;br&gt;
  2017-12-04 07:15:27 &lt;br&gt;
  SMTP Error: Could not connect to SMTP host.&lt;br&gt;
  [1,"Error: SMTP Error: Could not connect to SMTP host."]
  </code></pre>
  
  <p>Any help is appreciated.</p>
  
  <p>PS : The settings I'm using are  </p>
  
  <ol>
  <li>Host : smtp.gmail.com.  </li>
  <li>Username and password : I've checked and I can log into my account on gmail web.  </li>
  <li>Security : tsl  </li>
  <li>Port : 587.  </li>
  </ol>
  
  <p>I doubt if its my code. coz if it was my code, then It would't have worked with my organisation's email server, which is not as sophisticated as gmail.</p>