Issue
I am using cakephp 2.6. i need to send account activation link in email after successful registration and if the user clicks on activation link then the status field of my database to be updated with 1.
My email is going to be correctly but i don know how to send link inside email by getting lastinsertid as a unique identifier in email and update the account after click.
please help me to out of this.
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);
}
}
}
below is my account activation code
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'));
}
}
Solution
Well, this is something logical, I mean this is totally depends how you wanna do that. I am writing my way :)
Make new column in your users
table i.e. activation_key
, and set it before inserting it into the Table. Like this:
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);
}
And then in your function activate($activation_key='')
receive that activation key as a parameter, and find from users like this:
// 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.
}
Sometime, it happens that Cakephp do not allow you to update
$this->data
as in this example we are trying to change$this->data['User']['password']
and$this->data['User']['activation_key']
values so you can simply store$postData = $this->data
and then use$postData
for insertion and further operations.Update: Please confirm/debug if
String::uuid()
is working correctly for you, if it dosn't work, try to use the below line for this purpose:
// Generating activation_key
$this->data['User']['activation_key'] = $activation_key = time();
Thank you!
Answered By - hmd
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.