Reputation: 67
So I have a lil problem with this function.
public function forgot()
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('forgot');
$this->load->view('footer');
}
else{
$email = $this->input->post('email');
$clean = $this->security->xss_clean($email);
$userInfo = $this->user_model->getUserInfoByEmail($clean);
if(!$userInfo){
$this->session->set_flashdata('flash_message', 'Adres email nie istnieje');
redirect(site_url().'/main/login');
}
if($userInfo->status != $this->status[1]){ //if status is not approved
$this->session->set_flashdata('flash_message', 'Twoje konto nie zostało aktywowane');
redirect(site_url().'/main/login');
}
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'email',
'smtp_pass' => 'haslo',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$token = $this->user_model->insertToken($userInfo->id);
$qstring = base64_encode($token);
$url = site_url() . '/main/reset_password/token/' . $qstring;
$link = '<a href="' . $url . '">' . $url . '</a>';
$message = '';
$message .= '<strong>Zmiana hasła</strong><br>';
$message .= '<strong>Aby dokonać zmiany hasła przejdź na podany adres:</strong> ' . $link;
$toEmail = $this->input->post('email');
$to = $toEmail;
$this->email->clear();
$this->email->from('[email protected]');
$this->email->to($to);
$this->email->subject('Thanks for registering');
$this->email->message($message);
if(!$this->email->send())
{
echo $this->email->print_debugger();
$this->session->set_flashdata('flash_message', 'Password reset fail.');
redirect(site_url().'/main/forgot');
}
else
{
$this->session->set_flashdata('flash_message', 'Password reset done.');
redirect(site_url().'/main/login');
}
}
}
This is my function in model that suppose to get email:
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
I have no errors while reseting password, I'm recieving positiv information that token is sent to mail but im not recieving any mails in inbox.
Upvotes: 1
Views: 181
Reputation: 38584
In Controller
public function forgot()
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('forgot');
$this->load->view('footer');
}
else{
$email = $this->input->post('email');
$clean = $this->security->xss_clean($email);
$userInfo = $this->user_model->getUserInfoByEmail($clean);
if($userInfo == false)
{
$this->session->set_flashdata('flash_message', 'Adres email nie istnieje');
redirect(site_url().'/main/login');
}
elseif ($userInfo[0]['status'] != $this->status[1])
{
$this->session->set_flashdata('flash_message', 'Twoje konto nie zostało aktywowane');
redirect(site_url().'/main/login');.
}
else
{
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]', # Change this
'smtp_pass' => 'pass', # Change this too
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$token = $this->user_model->insertToken($userInfo[0]['id']);
$qstring = base64_encode($token);
$url = site_url() . '/main/reset_password/token/' . $qstring;
$link = '<a href="' . $url . '">Activation Link</a>';
$message = '';
$message .= '<strong>Zmiana hasła</strong><br>';
$message .= '<strong>Aby dokonać zmiany hasła przejdź na podany adres:</strong> ' . $link;
$toEmail = $this->input->post('email');
$this->email->clear();
$this->email->from('[email protected]');
$this->email->to($toEmail);
$this->email->subject('Thanks for registering');
$this->email->message($message);
if(!$this->email->send())
{
//echo $this->email->print_debugger();
$this->session->set_flashdata('flash_message', 'Password reset fail.');
redirect(site_url().'/main/forgot');
}
else
{
$this->session->set_flashdata('flash_message', 'Password reset done.');
redirect(site_url().'/main/login');
}
}
}
}
In Model
public function getUserInfoByEmail($email)
{
$query = $this->db->query("SELECT * FROM users WHERE email = '$email' ");
$result = $query->result_array();
$count = count($result);
if(empty($count)){
return false;
}
else{
return $result;
}
}
Upvotes: 1