Laravel has a function for user email verification after registration which you can enable pretty easily. But how to customize the email being sent? I will show you two ways.
Option 1: toMailUsing()
The official Laravel documentation suggests adding this code to the AppServiceProvider:
use Illuminate\Auth\Notifications\VerifyEmail;use Illuminate\Notifications\Messages\MailMessage; public function boot(): void{    // ...     VerifyEmail::toMailUsing(function (object $notifiable, string $url) {        return (new MailMessage)            ->subject('Verify Email Address')            ->line('Click the button below to verify your email address.')            ->action('Verify Email Address', $url);    });}
This works well if you just want to make small tweaks. But what if you want to build your own custom Notification class for this?
Option 2: Create Custom Notification Class
You can also create your own notification:
php artisan make:notification VerifyEmail
Then, in your Notification class, you extend the core class from the framework, and override the method buildMailMessage():
app/Notifications/VerifyEmail.php
use Illuminate\Auth\Notifications\VerifyEmail as Notification;use Illuminate\Notifications\Messages\MailMessage; class VerifyEmail extends Notification{    protected function buildMailMessage($url): MailMessage    {        return (new MailMessage())            ->subject(trans('auth.mail.verify.subject'))            ->line(trans('auth.mail.verify.line1', [                'count' => config('auth.verification.expire', 60),            ]))            ->action(trans('auth.mail.verify.action'), $url)            ->line(trans('auth.mail.verify.line2'));    }}
Finally, in the User Model you need to override the function sendEmailVerificationNotification() and pass your custom Notification class, instead.
app/Models/User.php:
use Azuriom\Notifications\VerifyEmail as VerifyEmailNotification; class User extends Authenticatable implements MustVerifyEmail{    // ...     public function sendEmailVerificationNotification(): void    {        $this->notify(new VerifyEmailNotification());    }}
                                                
                                                    
                                                    
                                                    
No comments or questions yet...