Email 将电子邮件中的令牌与用户进行比较';s帐户激活令牌

Email 将电子邮件中的令牌与用户进行比较';s帐户激活令牌,email,token,cakephp-3.0,activation,Email,Token,Cakephp 3.0,Activation,我需要向用户发送一封电子邮件,其中包含一个链接,用户可以单击该链接激活其帐户。这是我的密码: //ADD METHOD FROM USERS CONTROLLER, THIS SENDS THE EMAIL WHEN A NEW USER IS ADDED public function add() { $user = $this->Users->newEntity(); if ($this->request->is('post')) {

我需要向用户发送一封电子邮件,其中包含一个链接,用户可以单击该链接激活其帐户。这是我的密码:

//ADD METHOD FROM USERS CONTROLLER, THIS SENDS THE EMAIL WHEN A NEW USER IS ADDED

 public function add()
    {
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data);


        $newAuthToken = bin2hex(openssl_random_pseudo_bytes(16));

        $user['authtoken'] = $newAuthToken;
        $user['activated'] = null;

        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));

            $ms='Click on the link below to complete registration ';
            $ms.='urlhere.com/users/activate/t:'.$newAuthToken.'';
            $ms=wordwrap($ms,70);


            $email = new Email('default');
            $email->viewVars();
            $email->template('default')->viewVars(array('user' => $user))
            ->emailFormat('html')
            ->to($user['email'])
            ->from('admin@example.com')
            ->subject('Hello ' . $user['email'])
            ->send($ms);


            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
    }

    $groups = $this->Users->Groups->find('list', ['limit' => 200]);
    $answers = $this->Users->Answers->find('list', ['limit' => 200]);
    $courses = $this->Users->Courses->find('list', ['limit' => 200]);
    $this->set(compact('user', 'groups', 'answers', 'courses'));
    $this->set('_serialize', ['user']);
}
此函数应将电子邮件令牌(来自链接)与用户表中的令牌进行比较,并将时间戳设置为激活(如果匹配):

//ACTIVATE FUNCTION FROM USERS CONTROLLER, SHOULD SET TIMESTAMP FOR ACTIVATED
public function activate($id = null)
{
if (!empty($this->passedArgs['t'])){
    $tokenhash = $this->passedArgs['t'];
    $results = $this->User->find('first', array('conditions' => array('authtoken' => $tokenhash)));


        if($results['authtoken']==$tokenhash) {
        $this->User->id = $results['id'];
        $this->User->saveField('activated', current_datetime());
        $this->Flash->success(__('The user has been saved.'));
        return $this->redirect(['action' => 'index']);
        exit;
    } else {
        $this->Flash->error('Tokens do not match');
        return $this->redirect(['action' => 'index']);

    }
}
}

你知道为什么这不起作用吗?

你有错误吗?死亡的蓝屏?什么都没有?它是否表现得好像一切正常,但没有发送电子邮件?电子邮件已发送,但发送到了错误的地址?电子邮件到达了,但不包含链接?它确实包含链接,但用户不能单击它?或者别的什么?它会将我重定向到登录屏幕,没有flash消息,激活的字段也不会更新。这是你应该问的问题!请编辑它,并添加您的身份验证组件(您可能正在使用)配置/调用。您正在
激活
功能中使用
->id
->saveField
。这是蛋糕2的概念。为什么不像在
add
函数中那样保存它?会出现错误吗?死亡的蓝屏?什么都没有?它是否表现得好像一切正常,但没有发送电子邮件?电子邮件已发送,但发送到了错误的地址?电子邮件到达了,但不包含链接?它确实包含链接,但用户不能单击它?或者别的什么?它会将我重定向到登录屏幕,没有flash消息,激活的字段也不会更新。这是你应该问的问题!请编辑它,并添加您的身份验证组件(您可能正在使用)配置/调用。您正在
激活
功能中使用
->id
->saveField
。这是蛋糕2的概念。为什么不像在
add
功能中那样保存它?