How to Send Notification via Email in Laravel Easily
In Laravel, you send email notifications by creating a
Notification class and using the notify() method on a user or model. Laravel handles the email sending via configured mail drivers automatically when you define the email content inside the notification class.Syntax
To send an email notification in Laravel, you typically:
- Create a notification class with
php artisan make:notification. - Define the
toMail()method to set email content. - Call
notify()on the notifiable model (usually a User).
This pattern uses Laravel's built-in notification system and mail configuration.
php
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; class InvoicePaid extends Notification { public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { return (new MailMessage) ->subject('Invoice Paid') ->line('Your invoice has been paid!') ->action('View Invoice', url('/invoices')) ->line('Thank you for using our application!'); } } // Sending notification $user->notify(new InvoicePaid());
Example
This example shows how to create and send a simple email notification to a user when an invoice is paid.
php
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; class InvoicePaid extends Notification { public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { return (new MailMessage) ->subject('Invoice Paid') ->greeting('Hello!') ->line('Your invoice #1234 has been successfully paid.') ->action('View Invoice', url('/invoices/1234')) ->line('Thank you for your business!'); } } // In a controller or service use App\Models\User; use App\Notifications\InvoicePaid; $user = User::find(1); // get user $user->notify(new InvoicePaid());
Output
An email is sent to the user's registered email with subject 'Invoice Paid' and the message content defined in toMail().
Common Pitfalls
- Not configuring mail settings in
.envcauses emails not to send. - Forgetting to add
use Notifiabletrait in the User model prevents notifications. - Not returning
['mail']invia()means no email is sent. - Using
notify()on a model that does not use theNotifiabletrait will fail.
php
/* Wrong: User model missing Notifiable trait */ class User extends Authenticatable { // Missing: use Notifiable; } /* Right: Add Notifiable trait */ use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; } /* Wrong: via() returns empty array */ public function via($notifiable) { return []; } /* Right: via() returns mail channel */ public function via($notifiable) { return ['mail']; }
Quick Reference
Summary tips for sending email notifications in Laravel:
- Configure mail in
.env(SMTP, Mailgun, etc.) - Create notification with
php artisan make:notification - Implement
toMail()method for email content - Use
notify()on models withNotifiabletrait - Test emails locally with tools like Mailtrap or log driver
Key Takeaways
Use Laravel's Notification system with a Notification class to send emails.
Ensure your User model uses the Notifiable trait to receive notifications.
Define the email content inside the toMail() method of your Notification class.
Configure your mail settings properly in the .env file to enable sending.
Call notify() on the user or notifiable model to send the email notification.