spatie/laravel-welcome-notification
Send welcome emails to new Laravel users with a secure, expiring link to set their initial password. Adds migrations and a trait for your User model, plus a controller you extend to show the welcome form and save the password.
Installation:
composer require spatie/laravel-welcome-notification
Publish the config file (optional):
php artisan vendor:publish --provider="Spatie\WelcomeNotification\WelcomeNotificationServiceProvider"
First Use Case: Trigger the welcome notification when a user registers:
// In your registration controller or observer
$user->sendWelcomeNotification(now()->addDay());
This sends a notification with a secure password-reset link that expires after 24 hours.
Where to Look First:
config/welcome-notification.php (customize notification class, expiration logic, or email template).app/Notifications/WelcomeNotification.php (override the default to customize content).routes/web.php (ensure /welcome-notification/{token} is accessible for link redirection).Triggering the Notification:
sendWelcomeNotification($expiresAt) in registration logic (e.g., UserObserver, RegisterController).Carbon instance or timestamp for expiration (default: 24 hours).// Example in a registration observer
public function created(User $user)
{
$user->sendWelcomeNotification(now()->addHours(12));
}
Customizing the Notification:
WelcomeNotification:
php artisan make:notification CustomWelcomeNotification
Update the config to use your class:
'notification_class' => \App\Notifications\CustomWelcomeNotification::class,
toMail() or toArray() to modify content:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Welcome to ' . config('app.name'))
->line('Click here to set your password:')
->action('Set Password', url('/welcome-notification/' . $this->token));
}
Handling the Secure Link:
Spatie\WelcomeNotification\WelcomeNotificationToken./welcome-notification/{token} after clicking the link.WelcomeNotificationMiddleware):
Route::get('/welcome-notification/{token}', [WelcomeNotificationController::class, 'show'])
->middleware(['auth', 'welcome.notification']);
Integration with Auth:
MustVerifyEmail trait if sending email verification:
use Illuminate\Auth\MustVerifyEmail;
class User extends Authenticatable implements MustVerifyEmail
{
// ...
}
Testing:
$user = User::factory()->create();
$user->shouldReceive('sendWelcomeNotification')
->once()
->with(now()->addDay());
Token Expiration:
$expiresAt).welcome_notification_tokens table for stale tokens if users report broken links.$user->sendWelcomeNotification(now()->addWeeks(1));
Notification Delivery:
config/mail.php).php artisan queue:work).WelcomeNotification class is properly registered in config.Token Security:
hash helper by default.config(['welcome-notification.token_cipher' => 'AES-256-CBC']) for stronger encryption if needed.Route Conflicts:
/welcome-notification/{token} doesn’t conflict with existing routes.Database Concerns:
welcome_notification_tokens table.php artisan migrate
Log Token Generation:
Add a created observer to log tokens for debugging:
public function created(WelcomeNotificationToken $token)
{
\Log::debug('Welcome token created:', ['token' => $token->token]);
}
Check Mail Logs: Use Laravel’s mail logging to verify notifications are sent:
'mail' => [
'log' => env('MAIL_LOG', false),
],
Override Token Logic:
Customize token generation in app/Providers/WelcomeNotificationServiceProvider.php:
public function boot()
{
WelcomeNotificationToken::creating(function ($token) {
$token->custom_field = 'value';
});
}
Custom Token Storage: Override the token model to use a different database or cache:
class CustomWelcomeNotificationToken extends WelcomeNotificationToken
{
public $timestamps = false;
protected $connection = 'mysql_custom';
}
Update the config to point to your model.
Multi-Channel Notifications: Extend the notification to support SMS or push:
public function via($notifiable)
{
return ['mail', 'nexmo']; // Add SMS channel
}
Dynamic Expiration: Use a closure for dynamic expiration logic:
$user->sendWelcomeNotification(fn () => now()->addDays(config('welcome-notification.dynamic_days')));
Localization: Localize the notification content:
public function toMail($notifiable)
{
return (new MailMessage)
->subject(__('welcome.subject'))
->line(__('welcome.greeting', ['name' => $notifiable->name]));
}
Event-Based Triggers:
Listen for the WelcomeNotificationSent event to add side effects:
WelcomeNotificationSent::listen(function ($notification) {
\Log::info("Welcome sent to {$notification->notifiable->email}");
});
How can I help you explore Laravel packages today?