In my AuthServiceProvider I was trying to add the users name to to email body. I tried the following method to accomplish that but it didn't work as expected.
When the page redirect to the verification page directly after registration it fails to get the auth user and results in an error. But when that page is closed and tried to login from the login page the it could get the auth()->user();
public function boot()
{
$this->registerPolicies();
VerifyEmail::toMailUsing(function ($notifiable, $url) {
$user_name = Auth::user()->name;
return (new MailMessage)
->greeting("Hello {$user_name}!")
->subject('Verify Email Address')
->line('Please click the button below to verify your email address.')
->action('Verify Email Address', $url)
->line('If you did not create an account, no further action is required.');
});
}
Well, this can be achieved by using VerifyEmail::toMailUsing(). I used this function in my AuthServiceProvider. And the user's name can be accessed by using the $notifiable. This has all the information about the registered user.
public function boot()
{
$this->registerPolicies();
VerifyEmail::toMailUsing(function ($notifiable, $url) {
return (new MailMessage)
->greeting("Hello {$notifiable->name}!")
->subject('Verify Email Address')
->line('Please click the button below to verify your email address.')
->action('Verify Email Address', $url)
->line('If you did not create an account, no further action is required.');
});
}