Setting up basic email notifications in Laravel 10.x and queueing them
Notifications in Laravel allow you to send messages to users over a variety of channels including mail, SMS, Slack, and more. In this guide, we'll focus on setting up basic email notifications.
Here’s a step-by-step guide on how to use notifications in Laravel:
1. Set Up a New Laravel Project
If you don't have a Laravel project set up yet:
laravel new notifications-demo
cd notifications-demo
2. Database Setup
For demonstration purposes, we’ll assume you have set up a database and configured the .env
file accordingly.
Run migrations to ensure your users table is set up:
php artisan migrate
3. Generate a Notification
Use the make:notification
Artisan command to generate a new notification class:
php artisan make:notification SampleNotification
This will create a new notification class in the app/Notifications
directory.
4. Configure the Notification
Edit the SampleNotification
class (app/Notifications/SampleNotification.php
).
For this example, let’s use the toMail
method to send an email notification:
use Illuminate\Notifications\Messages\MailMessage;
public function toMail($notifiable)
{
return (new MailMessage)
->line('This is the introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
5. Setting up Mail Configuration
For sending email, configure your mail settings in .env
:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=from@example.com
MAIL_FROM_NAME="${APP_NAME}"
Note: The above settings are for Mailtrap, which is used for development purposes. In production, you'll want to use a different provider or SMTP settings.
6. Sending the Notification
To send the notification, first ensure your user model (typically app/Models/User.php
) uses the Notifiable
trait:
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
}
Now, you can send the notification:
use App\Notifications\SampleNotification;
$user = App\Models\User::first(); // get the first user as an example
$user->notify(new SampleNotification());
You can put this code in a route, a controller, a job, or anywhere else in your application where you want to trigger the notification.
7. Queueing Notifications (Optional)
For improved performance, especially if you're sending a lot of notifications or sending them via a slow channel, you might want to queue them.
First, make sure your notification class implements the ShouldQueue
interface and uses the Queueable
trait:
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class SampleNotification extends Notification implements ShouldQueue
{
use Queueable;
// ...
}
Ensure you have a queue driver configured (like Redis, database, etc.) and start the Laravel queue worker:
php artisan queue:work
Now, when you send a notification, it will be handled in the background by the queue worker.
That's a basic introduction to using notifications in Laravel! There's a lot more you can do with notifications, including sending them over different channels, customizing the notification content based on the channel, and more. Always refer to the official Laravel documentation for the most detailed and up-to-date information.