PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] How to Make Forgot Password in Codeigniter, Password send in email

 July 07, 2022     codeigniter, email, mysql, phpmailer     No comments   

Issue

Hello guyz i have create small kind of application in codeigniter,in this application i am doing forgot password module, i have created a function but dont know why its not working, i need random password have to been send in mail which will be autogenerated , but email method does not work, so give me some suggestion.

Here is My view:

<form action="<?php echo base_url() . "welcome/forgotpassword" ?>" method="POST">

            <div class="form-group has-feedback">
                    <input type="email" class="form-control" placeholder="Email" name="user_email" />
                    <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
             </div>
              <div class="row">
              <div class="col-xs-4">
                     <input type="submit"  class="btn btn-primary btn-block btn-flat"  value="Send">

              </div>
             </div>
    </form>

Here is my Controller:

public function forgotpassword(){

    $email = $this->input->post('user_email');
    $findemail = $this->main_model->ForgotPassword($email);
    $this->load->view('forgotpassword');
    if ($findemail) {
        $this->main_model->sendpassword($findemail);
    } else {

          $this->session->set_flashdata('msg', 'Email not found!');

    }
}

Here is My model:

 public function sendpassword($data) {
    $email = $data['user_email'];
    print_r($data);
    $query1 = $this->db->query("SELECT *  from user_registration where user_email = '" . $email . "'");
    $row = $query1->result_array();


    if ($query1->num_rows() > 0) {

        $passwordplain = "";
        $passwordplain = rand(999999999, 9999999999);
        $newpass['user_password'] = md5($passwordplain);
        $this->db->where('user_email', $email);
        $this->db->update('user_registration', $newpass);
        $mail_message = 'Dear ' . $row[0]['full_name'] . ',' . "\r\n";
        $mail_message .= 'Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>' . $passwordplain . '</b>' . "\r\n";
        $mail_message .= '<br>Please Update your password.';
        $mail_message .= '<br>Thanks & Regards';
        $mail_message .= '<br>Your company name';
        require FCPATH . 'assets/PHPMailer/PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPSecure = "tls";
        $mail->Debugoutput = 'html';
        $mail->Host = "ssl://smtp.googlemail.com";
        $mail->Port = 465;
        $mail->SMTPAuth = true;
        $mail->Username = "xxxxxxxxx@gmail.com";
        $mail->Password = "xxxxxxxx";
        $mail->setFrom('xxxxxxx@gmail.com', 'admin');
        $mail->IsHTML(true);
        $mail->addAddress('user_email', $email);
        $mail->Subject = 'OTP from company';
        $mail->Body = $mail_message;
        $mail->AltBody = $mail_message;

        if (!$mail->send()) {


            $this->session->set_flashdata('msg', 'Failed to send password, please try again!');
        } else {

            echo $this->email->print_debugger();
            $this->session->set_flashdata('msg', 'Password sent to your email!');
        }

    }
}

Solution

This function may have something to do with it... can you show us that?

$findemail = $this->main_model->ForgotPassword($email);

When you use print_r($data) is anything returned? If not, $query1 will be 0 or null and everything will break.

// core function
public function sendpassword($data) {
    // include your libary at the top
    require FCPATH . 'assets/PHPMailer/PHPMailerAutoload.php';
    // email retrieved from the ForgotPassword() method.
    $email = $data['user_email'];
    // get the user_info array row
    $query1 = $this->db->query("SELECT * from user_registration where user_email = '" . $email . "'");
    $row = $query1->result_array();
    if ($query1->num_rows() > 0) {
        // assign users name to a variable
        $full_name = $row['full_name'];
        // generate password from a random integer
        $passwordplain = rand(999999999, 9999999999);
        // encrypt password
        $encrypted_pass = $this->pass_gen($passwordplain);
        $newpass['user_password'] = $encrypted_pass;
        // update password in db
        $this->db->where('user_email', $email);
        $this->db->update('user_registration', $newpass);
    // begin email functions
    $result = $this->email_user($full_name, $email, $passwordplain);
    echo $result;
    }
}

// email sending
public function email_user($full_name, $email, $passwordplain) {
    // compose message
    $mail_message = 'Dear ' . $full_name. ',' . "\r\n";
    $mail_message .= 'Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>' . $passwordplain . '</b>' . "\r\n";
    $mail_message .= '<br>Please Update your password.';
    $mail_message .= '<br>Thanks & Regards';
    $mail_message .= '<br>Your company name';
    // email config
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPSecure = "tls";
    $mail->Debugoutput = 'html';
    $mail->Host = "ssl://smtp.googlemail.com";
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->Username = "xxxxxxxxx@gmail.com";
    $mail->Password = "xxxxxxxx";
    $mail->setFrom('xxxxxxx@gmail.com', 'admin');
    $mail->IsHTML(true);
    $mail->addAddress('user_email', $email);
    $mail->Subject = 'OTP from company';
    $mail->Body = $mail_message;
    $mail->AltBody = $mail_message;
    // send the mail
    if (!$mail->send()) {
        return $this->email->print_debugger();
        $this->session->set_flashdata('msg', 'Failed to send password, please try again!');
    } else {
        return $this->email->print_debugger();
        $this->session->set_flashdata('msg', 'Password sent to your email!');
    }
}

// Password encryption
public function pass_gen($password) {
    $encrypted_pass = md5($password);
    return $encrypted_pass;
}


Answered By - maz3runn3r
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing