File "forgot_password.php"

Full path: /home/mysamm/public_html/mysamm_login/forgot_password.php
File size: 4.63 B
MIME-type: text/x-php; charset=us-ascii
Charset: utf-8

Download   Open   Edit   Advanced Editor &nnbsp; Back

<?php
include("admin/includes/header.php");    
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $email = trim($_POST['email']);
        
        if (empty($email)) {
            $msg = "Please enter your email!";
            $msgtype = "red";
            } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $msg = "Invalid email format!";
            $msgtype = "red";
            } else {
            $db = new Connection();
            
            // Check if email exists
            $result = $db->QueryCount("SELECT * FROM admin_users WHERE email = '$email'");
            
            if ($result > 0) {
                
                // Generate token
                $user = $db->GetRow("SELECT * FROM admin_users WHERE email = '$email'");
                //print_r($user);
                //die();
                $user_id= $user['id'];
                $newPassword = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), 0, 8);
                
                $hashedPassword = password_hash($newPassword, PASSWORD_BCRYPT);
                //print_r("UPDATE admin_users SET password = '$hashedPassword' WHERE id = $user_id");
                //die();
                $update = $db->execute("UPDATE admin_users SET password = '$hashedPassword' WHERE id = $user_id");
                
                $email = $user['email']; // Email to send to
                //print_r($email);
                //die();
                $subject = "Reset Password";
                $message = "
    <html>
    <head>
        <title>Password Reset</title>
    </head>
    <body>
        <p>Dear ".$user['name'].",</p>
        <p>You requested to reset your password. Here is your new password:</p>
        <p><strong>Your new password: </strong>" . $newPassword . "</p>       
        
    </body>
    </html>
";
                $headers = ""; // Headers for email
                $msg = ''; // Placeholder for message
                $msgtype = ''; // Placeholder for message type
                
                // Using sendmail_api function to send the email
                $res = sendmail_api(
                $to_email = array($email),
                $subject = $subject,
                $message = $message,
                $cc_emails = "", // Optional: You can add CC emails
                $bcc_emails = "", // Optional: You can add BCC emails
                $from_email = 'admin@mysamm.com', // Optional: Sender email
                $sending_attachments_email = array() // Optional: Attachments
                );
               if ($res['status'] == 1) {
                    $msg = "New Password sent to your email!";
                    $msgtype = "success";
                    } else {
                    $msg = "Failed to send email. Try again!";
                    $msgtype = "danger";
                }
                } else {
                $msg = "Email not found!";
                $msgtype = "danger";
            }
        }
        header("Location: forgot_password.php");
        exit();
    }
?>


<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card" style="margin-top: 100px;">
                <div class="card-header text-center">
                    <h3 class="card-title">Forget Password</h3>
                </div>
                <div class="card-body">
                    
                    <?php
                        if (isset($msg)) {
                            echo '<div class="alert alert-' . htmlspecialchars($msgtype) . ' text-center" role="alert">' . htmlspecialchars($msg) . '</div>';
                            unset($msg);
                            unset($msgtype);
                        }
                    ?>
                    
                    <form method="post" action="">
                        <div class="form-group">
                            <label for="email">Email<span class="text-danger">*</span>:</label>
                            <input type="email" id="email" name="email" class="form-control" required />
                        </div>
                        
                        <button type="submit" name="reset_request" class="btn btn-primary btn-block">Submit</button>
                        
                        <div class="text-center" style="margin-top: 10px;">
                            <a href="index.php">Back to Login</a>
                        </div>
                    </form>
                    
                </div>
            </div>
        </div>
    </div>
</div>