Laravel Breeze comes with Auth email templates, but what if you want to customize them? In this tutorial, we will add the user's name to the reset password email, which isn't shown by default.
First, we need to create a new Notification class which we will use to send a reset password email.
php artisan make:notification ResetPasswordNotification
Now we need to set that Laravel would use our just created notification. This can be done in the User Model within the sendPasswordResetNotification
method.
You can read more about
Reset Email Customization
in the official documentation.
app/Models/User.php:
use App\Notifications\ResetPasswordNotification; class User extends Authenticatable{ // ... public function sendPasswordResetNotification($token): void { $this->notify(new ResetPasswordNotification($token)); }}
Here, we pass the token.
Next, the notification itself.
app/Notifications/ResetPasswordNotification.php:
class ResetPasswordNotification extends Notification{ public function __construct(public readonly string $token) {} public function via($notifiable): array { return ['mail']; } public function toMail($notifiable): MailMessage { return (new MailMessage) ->subject(Lang::get('Reset Password Notification')) ->greeting(Lang::get('Hello') . ' ' . $notifiable->name . ',') ->line(Lang::get('You are receiving this email because we received a password reset request for your account.')) ->action(Lang::get('Reset Password'), $this->resetUrl($notifiable)) ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')])) ->line(Lang::get('If you did not request a password reset, no further action is required.')); } protected function resetUrl(mixed $notifiable): string { return url(route('password.reset', [ 'token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset(), ], false)); }}
The message is taken from the original notification which can be found here. The resetUrl
method is also taken from the same original notification. The only new thing here is we set a new greeting message. The greeting part is where by default Laravel adds Hello
. Here we do Hello, Users Name
.
The main thing here is $notifiable
variable. It contains the object of who are we sending notification to. In our case (and in most default use-cases), it's a User
model object.
Thank you so much. But then, what if I want to use my custom template?
Then you don't use the default functionality at all and create your own custom Mailable which would use your custom template.
I need to try that this week
Hi Sir, i am building a laravel project which is a combination of blog and eshop. For product's image and blog's featured image i am using spatie media library which is great. But after trying many methods and thinking many possible solution in my mind i couldn’t find a best way for managing the images for the blog post. Not the feature image but the normal image in a blog post. I am commenting here because in this post there is a example. The image on the top has a url structure of ../uploads/2023/04/... i want to know how to manage these image from the backend? How you do it for this website? I have tried UniSharp/laravel-filemanager and barryvdh/laravel-elfinder . The second one mostly satisfy my requirements but still, Is there any better way? And which method do this website use?
We have built our own uploader system, based on Filepond and then processing uploads to specific folders based on month. Nothing really special.
are these uploaded files are registered in database?
the issue i am thinking that if i directy store the image in storage it will work. but lets say after one day i remove that image from the WYSIWYG editor and after that i need to use that image image again but then i have to upload a new image file so there will be an extra image file on storage that i will never use again. how you solved this issue? how to approach this problem correctly ?
Yes, uploaded files are registered in database, with Spatie Media Library package.
If there are unused images, you can write a special artisan command for cleanup every week/month for images that are not used in any WYSIWYG editor texts in the database.
Hi Sir, How can I add multipe email template view for verify email address. Like I want to add 3 email template view for admin, vendor, user. How can I do that. I'm using breeze authentication. Thanks
In the user model
sendPasswordResetNotification()
method based on the role send the notification