我需要向用户发送一封电子邮件,其中包含可以点击以激活其帐户的链接。这是我的代码:
//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']);
}
以下是将电子邮件令牌(来自链接)与Users表中的令牌进行比较的功能,如果匹配则将时间戳设置为已激活:
//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']);
}
}
}
关于为什么这不起作用的任何想法?