繁体   English   中英

codeigniter忘记密码邮件未送达

[英]codeigniter forgot password mail is not delivered

所以我有这个功能的小问题。

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('whatever@c.com');
            $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');
            }



        }

    }

这是我在模型中应该接收电子邮件的函数:

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; 
} 
}

重置密码时我没有任何错误,我收到令牌发送到邮件的正信息,但收件箱中没有收到任何邮件。

在控制器中

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' => 'mymail@gmail.com', # 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('whatever@c.com');
            $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');
            }
        }
    }
}

在模型中

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;
    }        
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM