Q:

Im getting the previous pass_key value instead of the present one

Clicking forgot password for the first time will not display pass_key in the email view but stored in pass_key field
clicking forgot password for the second time and so on will get the value of pass_key from previous pass_key insertion

UsersController.php
<?php
public function forgotPassword()
    {
        if ($this->request->is('post')) {
            $query = $this->Users->findByEmail($this->request->data['email']);
            $user = $query->first();
            if (is_null($user)) {
                $this->Flash->error('Email address does not exist. Please try again');
            } else {
                $passkey =Text::uuid(); // create key 
                $timeout = time() + DAY;
                $url = Router::url(['controller' => 'users', 'action' => 'reset'], true) . '/' . $passkey; // Im just generate this link in the view and pass
                $status = $this->Users->updateAll(array('Users.pass_key' => $passkey,'timeout' => $timeout), array('Users.id' => $user->id));
             if($status){

                    $this->getMailer('User')->send('forgotPasswordEmail', [$user]);             
                   $this->Flash->success(__('We already sent a link to your email'));
            return $this->redirect(['action' => 'login']);
                } else {
                    $this->Flash->error('Error saving reset passkey/timeout');
                }
              }
        }
    }
?>

UserMailer.php
<?php
namespace App\Mailer;
use Cake\Mailer\Mailer;
class UserMailer extends Mailer
{

    public function forgotPasswordEmail($user)
    {
        // attach a text file 
        $this->attachments([
            'text for user.txt'=> [
                'file'=> 'files/instruction.txt',
                    'mimetype'=>'plain/text',
                    'contentId'=>'3734hf38'
            ],

        // attach an image file 
            'edit.png'=>[
                'file'=>'files/ourlogo.png',
                    'mimetype'=>'image/png',
                    'contentId'=>'734h3r38'
                ]
        ])
        ->to($user->email)
        ->emailFormat('html')
        ->subject(sprintf('Forgot Password link  %s', $user->username))
        ->viewVars([
        'username'=> $user->username,
        'useremail'=>$user->email,
        'passkey' => $user->pass_key,
        'userid' => $user->id
        ])

        // the template file you will use in this emial
        ->template('forgotPasswordEmail') // By default template with same name as method name is used.
        // the layout .ctp file you will use in this email
        ->layout('customLayout');
    }
}
?>

Template\Email\html\forgot_password_email.ctp  
<?php
use Cake\Routing\Router;
?>

<p>Your username is:  <?=$username?> </p>

<p>Click on the link below to Reset Your Password.</p>

<?php $resetUrl = Router::url(['controller' => 'users', 'action' => 'reset/' . $passkey]);?>

<p><a href="http://localhost<?php echo $resetUrl; ?>">Click here to Reset Your Password</a></p>

?>

  <?php
        $url = Router::url(['controller' => 'users', 'action' => 'reset'], true) . '/' . $passkey; // Im just generate this link in the view and pass
                        $status = $this->Users->updateAll(array('Users.pass_key' => $passkey,'timeout' => $timeout), array('Users.id' => $user->id));
                     if($status){

        var_dump($url);exit;  I get the exact pass_key but I dont know how to pass the url to forgot_password_email.ctp . I just know how to pass the using this [$user] 

 var_dump($user->pass_key);exit; will retrieve the previous transaction not current pass_key

        .............
    ?>

A:

Your problem is that you are generating the pass key and saving it to the database, but not updating the $user object, so when you pass that to the view it still has the old version.
Try this:
$this->Users->patchEntity($user, [
    'pass_key' => Text::uuid(), // create key 
    'timeout' => time() + DAY,
]);
if ($this->Users->save($user)) {

Here, we update the $user entity with the new values, so that they are there for the view, and use the save function instead of updateAll.
Also, in your view, you should change to the following:
$resetUrl = Router::url(['controller' => 'users', 'action' => 'reset', $passkey]);

