注册cakephp后,电子邮件中的帐户激活链接

时间:2016-05-03 06:36:42

标签: php cakephp

我正在使用cakephp 2.6。我需要在注册成功后在电子邮件中发送帐户激活链接 如果用户点击激活链接,那么我的数据库的状态字段将用1更新。

我的电子邮件是正确的,但我不知道如何通过将lastinsertid作为电子邮件中的唯一标识符来发送电子邮件中的链接,并在点击后更新帐户。

请帮我解决这个问题。

function add() {       
        $this->layout = "layout_login";      
            $this->pageTitle = "Add Admin";
            $this->set("pageTitle", "Add Admin");      
        if (!empty($this->request->data)) {           
            $this->User->set($this->data['User']);
            $isValidated = $this->User->validates();
            if ($isValidated) {   
               // Generating UUID
                $this->data['User']['activation_key'] = $activation_key = String::uuid();
                $this->User->save($this->data, array('validate' => false)); 
                $this->Session->setFlash(__('Please check your email for account verification.')); 
                    $subject = "Account Activation link By Kaya Dispatch";                  

                    $to = trim($this->request->data['User']['email']);
                    $this->Email->sendAs = 'html';
                    $this->Email->from = 'luckybajpai87@gmail.com';
                    $this->Email->to = $to;
                    $this->Email->subject = $subject;   
                    $activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]);
                    $message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>";
                    $message .= "Your account has been created successfully by Administrator.<br/>";
                    $message .= "Please find the below details of your account: <br/><br/>";
                    $message .= "<b>Activate your account by clicking on the below url:</b> <br/>";
                    $message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>";
                    $message .= "<br/>Thanks, <br/>Support Team";
                    $this->Email->send($message);

                    }            
                } 
            } 

下面是我的帐户激活码

function activate($activation_key) {
        $userData = $this->User->find('first', array(
        'conditions' => array(
                'User.activation_key' => $activation_key,
                'User.status' => 0
        )
    ));
    if( !empty($userData)){
           if ($userData['User']['status'] == 0)
            {
                $activeStatus = 1;
            } 
    $status = $this->User->updateAll(array('User.status' => $activeStatus), array('User.id' => $id));
    if ($status)
            {   $this->Session->setFlash(__('Status updated successfully'));

            } else
            {   $this->Session->setFlash(__('Something went wrong, please try again.'));

            }
             $this->redirect(array('controller' => 'Users', 'action' => 'index'));

    }
}

1 个答案:

答案 0 :(得分:1)

嗯,这是合乎逻辑的,我的意思是这完全取决于你想要怎么做。我正在写我的方式:)

<Image IsVisible="true"> <Image.HeightRequest> <OnIdiom x:TypeArguments="x:Double"> <OnIdiom.Phone> <OnPlatform x:TypeArguments="x:Double" iOS="72" Android="100" WinPhone="10" /> </OnIdiom.Phone> <OnIdiom.Tablet> <OnPlatform x:TypeArguments="x:Double" iOS="212" Android="140" WinPhone="10" /> </OnIdiom.Tablet> </OnIdiom> </Image.HeightRequest> <Image.WidthRequest> <OnIdiom x:TypeArguments="x:Double"> <OnIdiom.Phone> <OnPlatform x:TypeArguments="x:Double" iOS="125" Android="150" WinPhone="10" /> </OnIdiom.Phone> <OnIdiom.Tablet> <OnPlatform x:TypeArguments="x:Double" iOS="265" Android="190" WinPhone="10" /> </OnIdiom.Tablet> </OnIdiom> </Image.WidthRequest> <Image.Source> <OnPlatform x:TypeArguments="ImageSource"> <OnPlatform.iOS> <FileImageSource File="logo.png" /> </OnPlatform.iOS> <OnPlatform.Android> <FileImageSource File="logo.png" /> </OnPlatform.Android> <OnPlatform.WinPhone> <FileImageSource File="logo.png" /> </OnPlatform.WinPhone> </OnPlatform> </Image.Source> </Image> 表中创建新列,即users,并在将其插入表格之前进行设置。像这样:

activation_key

然后在if ($isValidated) { $password = $this->data['User']['password']; $this->data['User']['password'] = md5($this->data['User']['password']); // Generating UUID $this->data['User']['activation_key'] = $activation_key = String::uuid(); $this->User->save($this->data, array('validate' => false)); $this->Session->setFlash("<div class='success-message flash notice'>Admin has been created successfully.</div>"); $subject = "Account Activation link send on your email"; $name = $this->request->data['User']['fname'] . " " . $this->request->data['User']['lname']; $to = trim($this->request->data['User']['email']); $this->Email->sendAs = 'html'; $this->Email->from = 'luckybajpai87@gmail.com'; $this->Email->to = $to; $this->Email->subject = $subject; $activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]); // Always try to write clean code, so that you can read it :) : $message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>"; $message .= "Your account has been created successfully by Administrator.<br/>"; $message .= "Please find the below details of your account: <br/><br/>"; $message .= "<b>First Name:</b> " . $this->data['User']['fname'] . "<br/>"; $message .= "<b>Last Name:</b> " . $this->data['User']['lname'] . ((!empty($this->data['User']['phone'])) ? "<br/>"; $message .= "<b>Phone:</b> " . $this->data['User']['phone'] : "") . "<br/>"; $message .= "<b>Address:</b> " . $this->data['User']['address1'] . " " . $this->data['User']['address2'] . "<br/>"; $message .= "<b>Email:</b> " . $this->data['User']['email'] . "<br/>"; $message .= "<b>Username:</b> " . $this->data['User']['username'] . "<br/>"; $message .= "<b>Password:</b> " . $password . "<br/>"; $message .= "<b>Activate your account by clicking on the below url:</b> <br/>"; $message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>"; $message .= "<br/>Thanks, <br/>Support Team"; $this->Email->send($message); } 中将该激活密钥作为参数接收,并从以下用户中找到:

function activate($activation_key='')
  

有时候,Cakephp不允许您更新// Finding user data from users table on behalf of activation key // and Status should be 0 (deactivate). So that, a URL can be use only ONCE. $userData = $this->User->find('first', array( 'conditions' => array( 'User.activation_key' => $activation_key, 'User.status' => 0 ) )); if( !empty($userData) ){ // $userData as you have User's data update the status as 1 and set activation_key as empty ""; }else{ // So, you don't find any user, it is an invalid request. } ,因为在此示例中我们尝试更改$this->data$this->data['User']['password']值,因此您只需存储$this->data['User']['activation_key']然后使用$postData = $this->data进行插入和进一步操作。

     

更新:如果$postData正常工作,请确认/调试,如果它不起作用,请尝试使用以下行:

String::uuid()

谢谢!

相关问题