3

I want to allow an admin user to add new users, and these users should receive a notification email, but I don't want to use $user->sendEmailVerificationNotification();, because I want to customize the message I send them as well, so sometimes I can use the default sendEmailVerificationNotification();, but sometimes I want to use customSendEmailVerificationNotification()

What is the correct way to do that?

1 Answer 1

4

If all you need is to customize the message, as per the docs, you can pass a closure to the toMailUsing method provided by the Illuminate\Auth\Notifications\VerifyEmail notification.

// AuthServiceProvider

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    // ...

    VerifyEmail::toMailUsing(function ($notifiable, $url) {
        return (new MailMessage)
            ->subject('Verify Email Address')
            ->line('Click the button below to verify your email address.')
            ->action('Verify Email Address', $url);
    });
}

If you want to change the structure of the notification email as well you can publish the notification view by running:

php artisan vendor:publish --tag=laravel-notifications

After this, you can go ahead and make any changes to the view. Note that this will apply to all the notifications sent through the app, and not just the verify email.

Edit:

You can also override the sendEmailVerificationNotification() if you want to send the notifications conditionally:

use Illuminate\Auth\Notifications\VerifyEmail;
use App\Notifications\CustomVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
    public function sendEmailVerificationNotification()
    {
        $condition ? $this->notify(new CustomVerifyEmail) : $this->notify(new VerifyEmail);
    }
} 
Sign up to request clarification or add additional context in comments.

4 Comments

The problem is I want to be able to choose: For new accounts send the default verification email, but if an admin adds users to his account then send a customized email. In the example above, it will change for everyone
Check my updated answer :)
Thank you. I have a few questions: Where do I place this file? And how do I pass parameters to it? In my case I need to pass the newly created $user parameter in order to check whether it's an added user by admin or a newly registered account. Also, is it possible to instead of the above create a custom notification, call it on demand and somehow fetch the verification $url?
It is the default User model that comes with Laravel. If you are creating the users from an admin panel or something, you could just fire an event from there and have a separate listener and notification which I think would be ideal for your situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.