title: PHP SMTP Mail script not working
body: <p>I'm coding a php smtp mail script to send a mail to my users when they lose their password. However it's not working. </p>
  
  <p>The processor always returns:</p>
  
  <blockquote>
    <p>Message could not be sent...</p>
  </blockquote>
  
  <p>I've tried it with Gmail smtp and I'm getting the same results. </p>
  
  <p>Can you help? There are two files, the form, and the processor. </p>
  
  <p>Below are the codes:</p>
  
  <p>form:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'&gt;
  &lt;link rel = 'stylesheet' href = 'style.css' type = 'text/css'&gt;
  &lt;title&gt;Reset Password&lt;/title&gt;
  &lt;/head&gt;
  &lt;?php
  $db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
  $sql = $db-&gt;query('select id from posts order by id desc');
  $row = $sql-&gt;fetch(PDO::FETCH_ASSOC);
  echo "&lt;a href = 'browse.php?page=".$row['id']."'&gt;Browse&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href = 'index.php'&gt;Home&lt;/a&gt;";
  echo "&lt;hr&gt;";
  echo '&lt;form name = "forgot-pass" action = "reset-pass-notif.php" method = "post"&gt;
  &lt;label&gt;&lt;em&gt;Input your registered email address&lt;/em&gt;&lt;/label&gt;&lt;br&gt;
  &lt;input type = "text" name = "email"&gt;&lt;br&gt;
  &lt;em&gt;Enter Image Text:&lt;/em&gt;&lt;img src="captcha.php" /&gt;&lt;br /&gt;
  &lt;input name="captcha" type="text" /&gt;&lt;br&gt;
  &lt;input type = "submit" name = "submit" value = "Send Reset-Password Link"&gt;
  &lt;/form&gt;
  &lt;hr&gt;
  &lt;a href = "browse.php?page='.$row["id"].'"&gt;Browse&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href = "index.php"&gt;Home&lt;/a&gt;';
  ?&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>processor:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'&gt;
  &lt;link rel = 'stylesheet' href = 'style.css' type = 'text/css'&gt;
  &lt;title&gt;Reset Password Notification&lt;/title&gt;
  &lt;/head&gt;
  &lt;?php
  error_reporting(E_ALL);
  session_start();
  $db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
  $sql0 = $db-&gt;query('select id from posts order by id desc limit 1');
  $row0 = $sql0-&gt;fetch(PDO::FETCH_ASSOC);
  echo '&lt;a href = "browse.php?page='.$row0['id'].'"&gt;Browse&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href = "index.php"&gt;Home&lt;/a&gt;';
  echo '&lt;hr&gt;';
  $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
  $sql = $db-&gt;prepare('select * from users where email = :email');
  $sql-&gt;bindParam(':email', $email);
  $sql-&gt;execute();
  $sql-&gt;setFetchMode(PDO::FETCH_ASSOC);
  $row = $sql-&gt;fetch();
  $username = $row['username'];
  $user_check = $row['user_hash'];
  $subject = 'Reset your Password';
  $link = '&lt;a href = "http://example.com/reset-pass-land.php?username='.$row['username'].'&amp;user_check='.$row['user_hash'].'"&gt;http://bquotes.xyz/reset-pass-land.php?username='.$row['username'].'&amp;user_check='.$row['user_hash'].'&lt;/a&gt;';
  $message = 'Reset your password here: '.$link.' If you cannot click on the link, copy it and paste in your browser address bar';
  $sql = $db-&gt;prepare('select email from users where email = :email');
  $sql-&gt;bindParam(':email', $email);
  $sql-&gt;execute();
  $sql-&gt;setFetchMode(PDO::FETCH_ASSOC);
  $row = $sql-&gt;fetch();
  
  //require_once ('class.phpmailer.php');
  require 'PHPMailerAutoload.php';
  
  $mail = new PHPMailer;
  
  $mail-&gt;SMTPDebug = 3;                               // Enable verbose debug output
  
  $mail-&gt;isSMTP();                                      // Set mailer to use SMTP
  $mail-&gt;Host = 'mail.domain.com';  // Specify main and backup SMTP servers
  $mail-&gt;SMTPAuth = true;                               // Enable SMTP authentication
  $mail-&gt;Username = 'user@domain.com';                 // SMTP username
  $mail-&gt;Password = 'pass';                           // SMTP password
  //$mail-&gt;SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
  $mail-&gt;Port = 587;                                  // TCP port to connect to
  $mail-&gt;setFrom('user@domain.com', 'BQuotes Webmaster');
  //$mail-&gt;addAddress($email);     // Add a recipient
  /*$mail-&gt;addAddress('ellen@example.com');               // Name is optional
  $mail-&gt;addReplyTo('info@example.com', 'Information');
  $mail-&gt;addCC('cc@example.com');
  $mail-&gt;addBCC('bcc@example.com');
  
  $mail-&gt;addAttachment('/var/tmp/file.tar.gz');         // Add attachments
  $mail-&gt;addAttachment('/tmp/image.jpg', 'new.jpg'); */   // Optional name
  $mail-&gt;isHTML(true);                                  // Set email format to HTML
  
  $mail-&gt;Subject = $subject;
  $mail-&gt;Body    = $message;
  //$mail-&gt;AltBody = 'This is the body in plain text for non-HTML mail clients';
  
  /*if(!$mail-&gt;send()) {
      //echo '&lt;em&gt;Message could not be sent. Mailer Error:&lt;/em&gt; ', "&lt;em&gt;", $mail-&gt;ErrorInfo, "&lt;/em&gt;";
        echo '&lt;span class = "red"&gt;&lt;em&gt;Message could not be sent. You have one or more invalid fields.&lt;/em&gt;&lt;/span&gt;';
  } else {
      echo '&lt;span class = "green"&gt;&lt;em&gt;Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.&lt;/em&gt;&lt;/span&gt;';
  }*/
  
  if (($email)&amp;&amp;($username)&amp;&amp;!empty($_POST['captcha'])&amp;&amp;($_POST['captcha']==$_SESSION['code'])&amp;&amp;($mail-&gt;send())){
  echo '&lt;span class = "green"&gt;&lt;em&gt;Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.&lt;/em&gt;&lt;/span&gt;';
  }
  
  else {echo '&lt;span class = "red"&gt;&lt;em&gt;Message could not be sent. You have one or more invalid fields.&lt;/em&gt;&lt;/span&gt;';
  }
  
  echo "&lt;hr&gt;";
  echo '&lt;a href = "browse.php?page='.$row0['id'].'"&gt;Browse&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href = "index.php"&gt;Home&lt;/a&gt;';
  ?&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks!</p>