title: smtp connect() failed using PHPMailer
body: <p>I want to send a mail using <code>SMTP</code> and <code>PHP</code> and for that I am using <code>PHPMailer</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php
  require 'class.phpmailer.php';
  require 'PHPMailerAutoload.php';
  
  $mail = new PHPMailer;
  $body = 'Test Mail';
  echo $body + '\r\n';
  $mail-&gt;IsSMTP(); // telling the class to use SMTP
  //$mail-&gt;Host       = "smtp.gmail.com"; // SMTP server
  $mail-&gt;SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  
  $mail-&gt;SMTPAuth   = true;                  // enable SMTP authentication
  $mail-&gt;SMTPSecure = 'tls';                 // sets the prefix to the server
  $mail-&gt;Host       = 'smtp.gmail.com';      // sets GMAIL as the SMTP server
  $mail-&gt;Port       = 587;                   // set the SMTP port for the GMAIL server
  $mail-&gt;Username   = 'mail@gmail.com';  // GMAIL username
  $mail-&gt;Password   = '&lt;password&gt;';            // GMAIL password
  $mail-&gt;SetFrom('mail@gmail.com', '&lt;Name&gt;');
  $mail-&gt;AddReplyTo("replytomail@mail.com","&lt;Name&gt;");
  $mail-&gt;Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
  $mail-&gt;MsgHTML($body);
  $address = "mailto@mail.com";
  $mail-&gt;AddAddress($address, "&lt;Name&gt;");
  if(!$mail-&gt;Send()) {
      echo "Mailer Error: " . $mail-&gt;ErrorInfo;
  } else {
  echo "Message sent!";
  }
  ?&gt;
  </code></pre>
  
  <p>The error is :</p>
  
  <pre><code>2016-01-28 04:37:27 SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP 11sm12390018pfq.87 - gsmtp
  2016-01-28 04:37:27 CLIENT -&gt; SERVER: EHLO ebusinessservices.in
  2016-01-28 04:37:27 SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [118.67.244.60] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
  2016-01-28 04:37:27 CLIENT -&gt; SERVER: STARTTLS
  2016-01-28 04:37:28 SERVER -&gt; CLIENT: 220 2.0.0 Ready to start TLS
  2016-01-28 04:37:56 SMTP Error: Could not connect to SMTP host.
  2016-01-28 04:37:56 SMTP NOTICE: EOF caught while checking if connected
  2016-01-28 04:37:56 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  </code></pre>
  
  <p>I tried to apply the answers from StackOverflow, checked credentials, tried using SSL with port 465 but getting same error.</p>
  
  <p>In my gmail account, <code>Less Secure Apps</code> are allowed and 2-step verification is disabled. I tried to apply all the settings google provided for this to work but still getting same error.</p>
  
  <p>Even according to some answers, I removed special characters from my password just to make sure that should not work as escape character but still its not working.</p>
  
  <p>Please let me know the suggestions over this.</p>
  
  <blockquote>
    <p>I am trying to run the script from Server i.e. File is uploaded to web server.</p>
  </blockquote>
  
  <p><strong>Update 1 : This is a error log after trying PHPMailer inbuilt script using my google credentials.</strong> </p>
  
  <pre><code>SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP b28sm13608414pfd.24 - gsmtp
  CLIENT -&gt; SERVER: EHLO &lt;name&gt;
  SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [118.67.244.60]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
  CLIENT -&gt; SERVER: STARTTLS
  SERVER -&gt; CLIENT: 220 2.0.0 Ready to start TLS
  SMTP Error: Could not connect to SMTP host.
  SMTP NOTICE: EOF caught while checking if connected
  SMTP connect() failed.     https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  </code></pre>