title: PHP send email for Forgot Password
body: <p>So I am building a localhost page, and it contains users, so I created a forgot password page to reset password if they forget, and to update the randomized password into the database. </p>
  
  <p>This is the codes for ForgotPassword, and I use it by validating email and not ID.</p>
  
  <pre><code>&lt;h2 style="text-align:center;"&gt; Forgot Password &lt;/h2&gt;
  &lt;table align="center" bgcolor="#FFFFFF" width="500" height ="180"&gt;
  &lt;form action="doForgotPassword.php" method="post"&gt;
              &lt;tr&gt;&lt;td align="right" height="50" width="200"&gt;First Name&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="first_name" /&gt;&lt;/td&gt;&lt;/tr&gt;
  
              &lt;tr&gt;&lt;td align="right" height="50"&gt;Email&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="email" /&gt;&lt;/td&gt;&lt;/tr&gt;
  
              &lt;tr&gt;&lt;td colspan="2" align="right"&gt;&lt;input type="submit" value="Submit"/&gt;&lt;/td&gt;&lt;/tr&gt;
  
          &lt;/table&gt; &lt;/form&gt; &lt;/td&gt;&lt;/table&gt;
  </code></pre>
  
  <p>And this is the codes for DoForgotPassword, I managed to change it in the database but not able to email the user.
  I am having trouble at this line</p>
  
  <pre><code>    &lt;?php
      public function sendMail($email =$_POST['email'] , $id)
  </code></pre>
  
  <p>This is the rest of the codes after it.</p>
  
  <pre><code>    {
  $to = $email /* separated by comma for another email (useful if you want to keep records(sending to yourself))*/;
  $subject = 'INSERT_SUBJECT_HERE';
  
  $bound_text = "----*%$!$%*";
  $bound = "--".$bound_text."\r\n";
  $bound_last = "--".$bound_text."--\r\n";
  
  $headers = "From: noreply@somewhere.com\r\n";
  $headers .= "MIME-Version: 1.0\r\n" .
          "Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
  
  $message = " you may wish to enable your email program to accept HTML \r\n".
          $bound;
  
  $message .=
  'Content-Type: text/html; charset=UTF-8'."\r\n".
  'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
  '
  </code></pre>
  
  <p>
              
              
              
              <p>
              
              </p>
              
              
              </p>
  
  <pre><code>        &lt;font size="3" color="#000000" style="text-decoration:none;font-family:Lato light"&gt;
          &lt;div class="info" Style="align:left;"&gt;
  
          &lt;p&gt;information here&lt;!--(im sure you know how to write html ;))--&gt;&lt;/p&gt;
  
          &lt;br&gt;
  
              '. /* &lt;p&gt;Charge:    '.$charge.'    &lt;/p&gt; */'
          &lt;br&gt;
  
              &lt;p&gt;Reference Number: '.$id.'&lt;/p&gt;
  
                          &lt;/div&gt;
  
          &lt;/br&gt;
          &lt;p&gt;-----------------------------------------------------------------------------------------------------------------&lt;/p&gt;
          &lt;/br&gt;
          &lt;p&gt;( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )&lt;/p&gt;
          &lt;/font&gt;
          &lt;/div&gt;
          &lt;/body&gt;
      '."\n\n".
                                                                  $bound_last;
  
          $sent = mail($to, $subject, $message, $headers); // finally sending the email
  
  
      }
                  $email =$_POST['email'];
  
  
          function createRandomPassword() {
          $chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
          $i = 0;
          $pass = '' ;
  
          while ($i &lt;= 8) {
              $num = mt_rand(0,61);
              $tmp = substr($chars, $num, 1);
              $pass = $pass . $tmp;
              $i++;
          }
          return $pass;
          }
  
          $pw = createRandomPassword();
          $query = "UPDATE users SET password= SHA1('$pw') WHERE email = '$email' ";
          $result = mysqli_query($link, $query);
            if ($result){
            $query3 = "SELECT * FROM users where email = '$email'";
          $sql = mysqli_query($link, $query3) or die(mysqli_error());  
         $rownum = mysqli_num_rows($sql);
  
           if(!$rownum  ) {
       echo "We can not find your email in our records";
          }
  
         }
  </code></pre>
  
  <p>And this is the resulting code</p>
  
  <pre><code>     if($result){
  
           if(isset($_POST['id'])){
             $id = $_POST['id'];
  
     sendMail($email, $id);
  
             }
           }
                  ?&gt;
  </code></pre>