How to change password in php codeigniter using mail

Here is an example of how you can change a password in PHP CodeIgniter using email:

Step 1: Create a password reset form

Create a form in your view file (e.g. password_reset.php) that allows the user to enter their email address and a new password:

<?php echo form_open('password/reset');?>
    <label for="email">Email:</label>
    <input type="email" name="email" value="<?php echo set_value('email');?>">
    <br>
    <label for="new_password">New Password:</label>
    <input type="password" name="new_password" value="<?php echo set_value('new_password');?>">
    <br>
    <label for="confirm_password">Confirm Password:</label>
    <input type="password" name="confirm_password" value="<?php echo set_value('confirm_password');?>">
    <br>
    <input type="submit" name="submit" value="Reset Password">
<?php echo form_close();?>

Step 2: Create a password reset controller

Create a controller (e.g. Password.php) that handles the password reset process:

<?php

class Password extends CI_Controller {

    public function index()
    {
        $this->load->view('password_reset');
    }

    public function reset()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('new_password', 'New Password', 'required|min_length[8]');
        $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[new_password]');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('password_reset');
        }
        else
        {
            $email = $this->input->post('email');
            $user = $this->user_model->get_user_by_email($email);

            if ($user)
            {
                $new_password = $this->input->post('new_password');
                $user->password = password_hash($new_password, PASSWORD_DEFAULT);
                $this->user_model->update_user($user);

                // Send a confirmation email to the user
                $this->load->library('email');
                $this->email->from('[email protected]', 'Your Name');
                $this->email->to($email);
                $this->email->subject('Password Reset Confirmation');
                $this->email->message('Your password has been reset. Please log in with your new password.');
                $this->email->send();

                $this->session->set_flashdata('message', 'Password reset successfully. Please log in with your new password.');
                redirect('login');
            }
            else
            {
                $this->session->set_flashdata('error', 'Invalid email address.');
                redirect('password/reset');
            }
        }
    }

}

Step 3: Create a user model

Create a model (e.g. User_model.php) that handles user authentication and password reset:

<?php

class User_model extends CI_Model {

    public function get_user_by_email($email)
    {
        $query = $this->db->get_where('users', array('email' => $email));
        return $query->row();
    }

    public function update_user($user)
    {
        $this->db->update('users', $user);
    }

}

Step 4: Configure email settings

In your config/email.php file, configure the email settings:

<?php

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'your_smtp_host';
$config['smtp_user'] = 'your_smtp_user';
$config['smtp_pass'] = 'your_smtp_pass';
$config['smtp_port'] = 587;

Step 5: Test the password reset process

Test the password reset process by submitting the form with a valid email address and new password. The system should send a confirmation email to the user and update their password in the database.

Note: This is just an example code and you should modify it to fit your specific needs. Additionally, you should ensure that your email settings are configured correctly and that your email server is functioning properly.