I am working on a basic blog application in Codeigniter 3.1.8 and Bootstrap 4.
I have added a registration and login system to this application.
I have run into this issue while developing a password reset functionality: the email congaing he reset link is not send (or maybe not received).
The password reset form takes the email address used at registration:
<?php echo form_open(base_url('newpassword')); ?>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group mb-2">
<input type="submit" value="Reset password" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
The controller:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
private $headers = '';
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_link = '<a href="#">Dummy Reset Link</a>';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
$this->user_email = $this->input->post('email');
$this->body = "Your password reset link: $this->reset_link\n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
$this->headers = "From: noreply@yourdomain.com\n";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
if (mail($this->user_email, $this->subject, $this->body, $this->headers)) {
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
} else {
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}
The method that checks is the email exists (also used at registration, it works):
public function email_exists() {
$query = $this->db->get_where('authors', ['email' => $this->input->post('email')]);
return $query->num_rows() > 0;
}
Although I get the flash message confirming that the email was sent, I do not actually receive the email.
Where is my mystake?
I modified your class to use build-in Email library and mixed my code from a working project:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
// Sender email
private $my_email = "example@example.com";
// Sender name
private $my_name = "Example";
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_link = '<a href="#">Dummy Reset Link</a>';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
$this->user_email = $this->input->post('email');
$this->body = "Your password reset link: $this->reset_link\n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
// Loading the Email library
$config['protocol'] = 'sendmail';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
if($this->load->is_loaded('email')){
$this->email->initialize($config);
}
else{
$this->load->library('email',$config);
}
// Build the body and meta data of the email message
$this->email->from($this->my_email,$this->my_name);
$this->email->to($this->user_email);
$this->email->subject($this->subject);
$this->email->message($this->body);
if($this->email->send()){
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
}else{
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}