2016-05-03 5 views
0

Ich benutze CakePHP 2.6. Ich muss Kontoaktivierungslink in E-Mail nach erfolgreicher Registrierung und senden, wenn der Benutzer auf Aktivierungslink klickt, dann das Statusfeld meiner Datenbank mit 1.Konto Aktivierung Link in E-Mail nach der Registrierung cakephep

aktualisiert werden

Meine E-Mail wird korrekt sein, aber ich weiß nicht wie um den Link innerhalb der E-Mail zu senden, indem Sie lastinsertid als eindeutigen Bezeichner in der E-Mail erhalten und den Account nach dem Klick aktualisieren.

bitte helfen Sie mir aus diesem heraus.

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 = '[email protected]'; 
        $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); 

        }    
       } 
      } 

unten ist mein Aktivierungscode

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')); 

    } 
} 

Antwort

1

Nun, das ist etwas logisch ist, ich meine das völlig abhängig ist, wie Sie das tun wollen. Ich schreibe meinen Weg :)

Machen Sie neue Spalte in Ihrer users Tabelle, d. H. activation_key, und legen Sie sie vor dem Einfügen in die Tabelle. Wie folgt aus:

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 = '[email protected]'; 
    $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); 

} 

Und dann in Ihrem function activate($activation_key='') erhalten, dass Aktivierungsschlüssel als Parameter, und von den Benutzern wie diese finden:

// 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. 
} 

Manchmal passiert es, dass CakePHP erlauben Sie nicht zu update $this->data wie in diesem Beispiel versuchen wir, $this->data['User']['password'] und $this->data['User']['activation_key'] Werte zu ändern, so können Sie einfach $postData = $this->data speichern und dann $postData zum Einfügen und weitere Operationen verwenden.

Update: Bitte bestätigen/debug wenn String::uuid() richtig für Sie zu arbeiten, wenn es Arbeit dosn't, versuchen, die unterhalb der Linie für diesen Zweck zu verwenden:

// Generating activation_key 
$this->data['User']['activation_key'] = $activation_key = time(); 

Thank you!

+0

Ich habe eine Spalte activation_key in der Tabelle hinzugefügt, aber keine Daten in activation_key Spalte innerhalb der Tabelle einfügen und Link wird erfolgreich eine E-Mail senden, aber nach dem Klicken auf diesen Link die Statusspalte wird nicht aktualisiert in Tabelle –

+0

Ich aktualisierte auch meinen Code ... .in meiner Frage .. –

+0

Ich habe meine Antwort für Sie aktualisiert! Und Sie müssen nicht 'if ($ userData ['User'] ['status'] == 0)' in Ihre Aktivierungsfunktion einchecken, da wir bereits Benutzerdaten mit ''User.status' => finden 0' Zustand. – hmd

Verwandte Themen