CodeIgniter:忘记密码(无法发送电子邮件和更改密码)

时间:2015-05-15 16:26:33

标签: php codeigniter email passwords

因此,当我尝试将新的随机密码更新到数据库并将其发送到客户端电子邮件地址时,我遇到了问题。

这是我的模特:

        function check_email($email)
        {
                $this->db->select('email');
                $this->db->from('user');
                $this->db->where('email', $email);
                $this->db->limit(1);

                $query = $this->db->get();

                if($query->num_rows() == 1)
                {
                        return TRUE;
                }
                else
                {
                        return FALSE;
                }
        }

        function reset($new, $id)
        {
                $data = array(
                        'password'=> $new
                        );
                $this->db->where('user_id', $id);
                $query = $this->db->update('user', $data);

                if($query)
                {
                        return TRUE;
                }
                else
                {
                        return FALSE;
                }
        }

这是我的控制者:

        public function lupa()
        {

                $this->form_validation->set_rules('email', 'Email', 'trim|required');

                if ($this->form_validation->run() === FALSE)
                {
                        echo validation_errors(); 
                }
                else
                {
                        $session_data = $this->session->userdata('logged_in');
                        $id = $session_data['user_id'];
                        $email = $this->input->post('email');
                        $result = $this->password_model->check_email($email);

                        if($result)
                        {
                                $new = $this->generate_password(6);
                                $query = $this->password_model->reset(md5($new), $id);

                                if($query)
                                {
                                        $this->load->library('email');

                                        $this->email->from('example@gmail.com', 'Your name');
                                        $this->email->to('admin@gmail.com');     
                                        $this->email->subject('Password reset');
                                        $this->email->message('You have requested the new password, Here is you new password:'. $new);  
                                        $this->email->send();

                                        redirect('login', 'refresh');

                                }

                        }
                        else
                        {
                            echo 'email tidak ada di database';
                        }
                }
        }


        public function generate_password( $length = 8 ) 
        {
                $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
                $password = substr( str_shuffle( $chars ), 0, $length );
                return $password;
        }

这是我的观点:

            <?=form_open_multipart('change_password/lupa');?>


                 <p>Enter your e-mail address below to reset your password.</p>
                  <input type="text" name="email" placeholder="Email" autocomplete="off" class="form-control placeholder-no-fix">

              </div>
              <div class="modal-footer">
                  <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
                  <button class="btn btn-success" type="submit">Submit</button>
                  <?php echo validation_errors(); ?>
              </div>
              </form>

我没有错误,但新的随机密码无法保存到我的数据库中,为什么我没有收到电子邮件?

我写错了代码吗?

感谢您的关注:)

0 个答案:

没有答案