Reputation: 111
I've made a password reset function for one of my projects.
I'm currently stuck, as I'm not sure how to link my code together.
I've got the email function, reset function and reset link all ready to go. But I'm not sure how to get the reset link to be mailed to the entered email address from the view and the email message to include my reset token and message.
Please have a look at my code and advise if possible, much appreciated.
This is my User controller code
public function Email($data){
$config = array(
'useragent' => 'codeIgniter',
'protocol' => 'mail',
'mailpath' => '/usr/sbin/sendmail',
'smtp_host' => 'localhost',
'smtp_user' => 'Myemail',
'smtp_pass' => 'mypass',
'smtp_port' => 25,
'smtp_timeout' => 55,
'wordwrap' => TRUE,
'wrapchars' => 76,
'mailtype' => 'html',
'charset' => 'utf-8',
'validate' => FALSE,
'priority' => 3,
'crlf' => "\r\n",
'newline' => "\r\n",
'bcc_batch_mode' => FALSE,
'bcc_batch_size' => 200,
);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('myemail');
$this->email->to(''); //This is where I'm not sure.
$this->email->subject("Reset Password");
$this->email->message(); //Next point where I'm not sure
$this->email->set_mailtype('html');
if($this->email->send()){
return TRUE;
}
else{
return FALSE;
}
}
public function forgotpassword(){
$this->load->view('templates/header');
$this->load->view('users/forgotpassword');
$this->load->view('templates/footer');
}
public function resetlink(){
$email = $this->input->post('email');
$result = $this->db->query("select * from users where
email ='".$email."'")->result_array();
if(count($result)>0){
$token = rand(100000,999999);
$this->db->query("update users set password =
'".$token."' where email = '".$email."'");
$message = "Please click on the password reset link
<br><a href='".base_url('users/reset?token=').$token."'>Reset
Password</a>";
$this->Email($email, 'Reset Password Link', $message);
}
else{
$this->session->set_flashdata('message', 'Email not
registered');
redirect(base_url('users/forgotpassword'));
}
}
public function resetpass(){
$data['token'] = $this->input->get('token');
$_SESSION['token'] = $data['token'];
$this->load->view('templates/header');
$this->load->view('users/resetpass');
$this->load->view('templates/footer');
}
public function updatepass(){
$_SESSION['token'];
$data = $this->input->post();
if ($data['password'] == $data ['cpassword']){
$this->db->query("update users set
password'".$data['password']."' where password='".$_SESSION['token']."'");
}
}
This is my forgot password view
<style>
body{
background-image:
url("/assets/images/posts/background12.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>
<br>
<br>
<form action="<?= base_url('users/resetlink')?>"
method="post">
<div class="row" >
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="form-group" >
<h2 align="center" >Reset your Password</h2>
<br>
<input type="email" class="form-control"
name="email" placeholder="Email" required autofocus>
</div>
<input style="margin-top: 15px" type="submit"
class="btn btn-info btn-block" value="Send reset link">
<h3 style="margin-top: 30px" align="center"><?= $this-
>session->flashdata('message') ?></h3>
</div>
<div class="col-md-3"></div>
</div>
</form>
Reset Password view
<style>
body{
background-image: url("/assets/images/posts/background12.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>
<br>
<br>
<form action="<?= base_url('users/resetpass') ?>">
<div class="row" >
<div class="col-md-3"></div>
<div class="col-md-6">
<h1>Reset Password</h1>
<div class="form-group" >
<h1 align="center"><?= $this->session->flashdata('message') ?></h1>
<input type="password" id="email" class="form-control"
name="password" placeholder="Password" required autofocus>
</div>
<div class="form-group" >
<input type="password" id="email" class="form-control"
name="cpassword" placeholder="Confirm Password" required autofocus>
</div>
<input style="margin-top: 15px" type="submit" class="btn btn-info
btn-block" value="Reset password">
</div>
<div class="col-md-3"></div>
</div>
</form>
That is all the relevant code I've got for this. Any help will be appreciated
Upvotes: 0
Views: 1426
Reputation: 23
If you change your Email function to
public function Email($email, $subject, $content){
$config = array(
'useragent' => 'codeIgniter',
'protocol' => 'mail',
'mailpath' => '/usr/sbin/sendmail',
'smtp_host' => 'localhost',
'smtp_user' => 'Myemail',
'smtp_pass' => 'mypass',
'smtp_port' => 25,
'smtp_timeout' => 55,
'wordwrap' => TRUE,
'wrapchars' => 76,
'mailtype' => 'html',
'charset' => 'utf-8',
'validate' => FALSE,
'priority' => 3,
'crlf' => "\r\n",
'newline' => "\r\n",
'bcc_batch_mode' => FALSE,
'bcc_batch_size' => 200,
);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('myemail');
$this->email->to($email); //email address passed into the function.
$this->email->subject($subject); // subject passed into the function
$this->email->message($content); //content passed into the function
$this->email->set_mailtype('html');
if($this->email->send()){
return TRUE;
}
else{
return FALSE;
}
}
However, according to the [Codeigniter documentation][1]
[1]: https://www.codeigniter.com/user_guide/libraries/email.html?highlight=complete%20web , you need to send a complete web page. Therefore you would need something like -
$message = "<html><head><title>Password Reset</title></head><body>Please click on the password reset link <br><a href='".base_url('users/reset?token=').$token."'>Reset Password</a></body></html>";
$this->Email($email, 'Reset Password Link', $message);
Upvotes: 1