I have put together a forgot password page that should insert a token to my lost password db table and send the user a email with a link to reset the password but I am not getting the email and just get there was a error message on the forgot password page after clicking the submit button and bit unsure what the issue is. My code is below
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$db = new mysqli("localhost", "username", "password", "databasename");
if(isset($_POST['submit'])){
$email = $_POST['email'];
$stmt = $db->prepare("SELECT * FROM `users` where `customer_email` = ?");
$stmt->bind_param('s', $email);
$stmt->execute();
$res = $stmt->get_result();
if($res->num_rows < 1){
echo "No such email has been found";
} else{
$fetch = $res->fetch_assoc();
$userid = $fetch['user_id'];
$token = bin2hex(openssl_random_pseudo_bytes(45));
$from = "noreply@domain.co.uk";
$url = 'https://www.domain.co.uk/account/passwordreset.php?token='.$token;
if(mail($email, $url, $from)){
//if(mail($to,$subject,$message,$url,$headers)){
$stmt = $db->prepare("INSERT INTO `lost_password`(user_id, token) values(?,?)");
$stmt->bind_param('is', $userid, $token);
$stmt->execute();
$to = $email;
$subject = "New Password Instructions";
$message = " Please visit $url to reset your password";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <noreply@domain.co.uk>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
if($stmt->affected_rows == 1){
echo "We have emailed you instructions on how to reset your password";
} else {
echo "there was an error";
}
}
}
}
?>