codeigniter 我怎样用SMTP发送php邮件?

5t7ly7z5  于 2022-12-06  发布在  PHP
关注(0)|答案(2)|浏览(145)

你好,请有人帮助我为什么我有一个困难的时间,我不能识别的错误为什么它返回错误设置闪存数据,虽然电子邮件ID匹配的输出?我正在做一个忘记密码在我的登录页面使用PHP和codeigniter框架

    • 我需要安装SMTP才能启用电子邮件方法,还是需要安装某些东西?**

控制器:

public function index()
    {
        $this->load->model('model_users');
        if($_SERVER['REQUEST_METHOD']=='POST')
        {
            $this->form_validation->set_rules('email','Email','required');
            if($this->form_validation->run()==TRUE)
            {
                $email = $this->input->post('email');
                $validationemail = $this->model_forgotpass->validationemail($email);
                if($validationemail!=false)
                {
                    $row = $validationemail;
                    $user_id = $this->session->userdata('id');
                    $user_data = $this->model_users->getUserData($user_id);

                    $string = time().$user_id.$email;
                    $hash_string = hash('sha256',$string);
                    $currentDate = date('Y-m-d H:i');
                    $hash_expiry = date('Y-m-d H:i',strtotime($currentDate. ' 1 days'));
                    $data = array(
                        'hash_key'=>$hash_string,
                        'hash_expiry'=>$hash_expiry,
                    );
                    $this->model_forgotpass->updatePasswordhash($data,$email);

                    $resetLink = base_url().'reset/password?hash='.$hash_string;
                    $message = '<p>Your reset password Link is here:</p>'.$resetLink;
                    $subject = "Password Reset link";
                    $sentstatus = $this->sendEmail($email,$subject,$message);
                    if($sentstatus==true)
                    {
                        $this->model_forgotpass->updatePasswordhash($data,$email);
                        $this->session->set_flashdata('success','Reset password link successfully sent');
                        redirect(base_url('forgotpass/index'));
                    }
                    else
                    {
                        $this->session->set_flashdata('error','Email sending error');
                    }

                }
                else
                {
                    $this->session->set_flashdata('error','Invalid email id');
                }
            }
        }
        else
        {
            $this->load->view('forgotpass/index','refresh');
        }

        $this->load->view('forgotpass/index','refresh');
        
    }
    public function sendEmail($email,$subject,$message)
    {
        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.gmail.com',

            'smtp_port' => 465,
            'smtp_user' => 'xxx',
            'smtp_pass' => 'xxx',

            'mailtype' => 'html',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
        );
        $this->load->library('email',$config);
        $this->email->set_newline("\r\n");
        $this->email->from('noreply');
        $this->email->to($email);
        $this->email->subject($subject);
        $this->email->message($message);

        if($this->email->send())
         {
            return true;
         }
         else
         {
            return false;
         }
    }

这是我模型:

function validationemail($email)
    {
        if($email) {
            $sql = 'SELECT * FROM users WHERE email = ?';
            $query = $this->db->query($sql, array($email));
            $result = $query->num_rows();
            return ($result == 1) ? true : false;
        }

        return false;
        
    }
    function updatePasswordhash($data,$email)
    {
        $this->db->where('email',$email);
        $this->db->update('users',$data);
    }

我似乎无法识别这里的错误,它总是返回作为电子邮件发送错误,我仍然无法收到我的电子邮件中的消息,虽然我的用户名和密码是正确的,但为什么我不能处理它?

vojdkbi0

vojdkbi01#

使用PHP发送邮件最简单的方法是使用PHPMailer。我建议您研究一下这个。https://github.com/PHPMailer/PHPMailer

pnwntuvh

pnwntuvh2#

使用echo $this-〉email-〉print_debugger()打印错误消息;在电子邮件其他部分。

相关问题