Add user object to mails sent to the user

Hello,

I’m theming some user emails, for example the password reset notification email, and I think it would be useful to send the whole user object to the mail template if you want to take the firstname lastname or any other fields you attached to the user :

Here is the actual function :

public function sendPasswordResetNotification($token)
{
    $url = $this->passwordResetUrl ?: Cms::entryUrl('resetPassword');
    $url .= str_contains($url, '?') ? '&' : '?';
    $url .= http_build_query([
        'reset' => $token,
        'email' => $this->getEmailForPasswordReset()
    ]);

    $data = [
        'url' => $url,
        'token' => $token,
        'count' => Config::get('auth.passwords.users.expire')
    ];

    $data += $this->getNotificationVars();

    Mail::send('user:recover_password', $data, function($message) {
        $message->to($this->email, $this->full_name);
    });
}

I saw that the method getNotificationVars() add some more variables here

public function getNotificationVars(): array
{
    $vars = [
        'full_name' => $this->full_name,
        'first_name' => $this->first_name,
        'last_name' => $this->last_name,
        'login' => $this->login,
        'email' => $this->email,
        'username' => $this->username,
    ];

    // Extensibility
    $result = Event::fire('rainlab.user.getNotificationVars', [$this]);
    if ($result && is_array($result)) {
        $vars = call_user_func_array('array_merge', $result) + $vars;
    }

    return $vars;
}

But why not adding the whole user object in case you extended it ?

Thank you

Hi @PubliAlex ,

You can listen to rainlab.user.getNotificationVars and send the object.

You right, that was just a small suggestion. Except of defining one by one the fields you want to send, it could be more extensible to send the whole user object. Why not letting the event to do some more complex things, but I would be annoyed to be able to listen to an event to add, for example, a simple phone number added by userplus