0

Hy everyone! I am following one tutorial to learn that how can we reset our password in php. I am working as localhost.I write the code, code is bug-free, error reporting is also on, mail() function is also working but i am receiving not any email. Also, on entering an invalid username, it is not giving any error too. I read out and tried all methods but no any method helped me ! Please check it out that where i am doing wrong ! Thanks.

<?php

    error_reporting(-1);
    ini_set('display_errors', 'On');
    set_error_handler("var_dump");

    require_once './include/db_connection.php';
    if (isset($_POST['button'])){
        $db_email="";
        $username = $_POST['username'];
        $email =$_POST['email'];
        $query="select * from signup where username='$username'";
        $result   = mysqli_query($link,$query);

        $count=mysqli_num_rows($result);

        // If the count is equal to one, we will send message other wise display an error message.

        if($count>0) {

            while ($row = mysqli_fetch_assoc($result)) {
                $db_email = $row['email'];
            }

            if($email == $db_email) {
                $code = rand(1000, 10000);
                $to =$db_email;
                $subject = "Password Reset Link";
                $body = "Do not reply of this automated email."
                      . "Enter the below link to reset passwowrd "
                      . "http://localhost/Validation/forgotpassword.php?code=$code&username=$username";

               // Always set content-type when sending HTML email
               $headers = "MIME-Version: 1.0" . "\r\n";
               $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

               // More headers
               $headers .= 'From: <webmaster@example.com>' . "\r\n";
               $headers .= 'Cc: myboss@example.com' . "\r\n";

               mysqli_query($link, "update signup set paareset='$code' where username='$username'");
               if(mail($to, $subject, $body,$headers)) {
                  echo 'Good';   
               } else {
                  echo 'mail() not working';
               }
               echo 'Check your inbox';

            } else {
                echo 'You have an incorrect email';
            }
        }

    }
?>


<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
<body>

    <form action="" method="post">

        <label> Enter your User ID : </label>
        <input  type="text" name="username" /><br>
        <label> Enter your Eamil address : </label>
        <input id="email" type="email" name="email" /><br>
        <input id="button" type="submit" name="button" value="Submit" />    
</body>
</form>
</html>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

2

You probably didn't set up any mail servers. You can always test your code with smtp4dev (https://smtp4dev.codeplex.com/) to be sure you even send an email.

If you do want to send emails to any email account you have to set up mailservers: smtp configuration for php mail

EDIT: You can't send emails with PHP without smtp servers.

Community
  • 1
  • 1
Lars Koole
  • 478
  • 5
  • 14