Our goal is to use Laravel’s notification system to make communication smoother and more enjoyable across different channels, all while keeping things scalable, maintainable, and efficient. By taking advantage of Laravel’s built-in notifications, we want to create a harmonious way to send messages—like emails, SMS, and Slack alerts—that keeps the messaging part separate from our core application functions. 

This approach helps us reduce code duplication and encourages a more modular design, allowing developers to create notifications once and reuse them throughout the application very easily. Plus, by embracing this notification system, we can enhance performance by queuing high-volume emails to be processed in the background, minimizing delays and boosting the user experience. 

By following the next steps, you will learn how to create and send mail notifications in the laravel 11 application.

Step 1 – Create a notification: You can create a new notification by using the command. 

php artisan make:notification ExampleNotification
Here, you need to use your notification name instead of ExampleNotification.

Step 2 – Configure the Notification: Open the ExampleNotification.php file in the app/Notifications directory and make changes by following the below code.

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ExampleNotification extends Notification
{
    public function __construct()
    {
        // Pass any data required by the notification here
    }

    public function via($notifiable)
    {
        return ['mail']; // Indicates this notification will be sent via email
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
              >subject('Example Notification Subject')
              >greeting('Hello!')
              >line('This is an example notification email.')
              >action('Visit Laravel', url('/'))
              >line('Thank you for using our application!');
    }
}

Step 3 – Set Up Email Configuration: You have to configure the email set up in the .env file:

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=example@example.com MAIL_FROM_NAME="${APP_NAME}"

Replace these values with your actual SMTP credentials.

Step 4 – Sending the Notification: You can send the notification from a controller, a job, or even directly from your routes for testing. Here’s an example of how to send the notification from a controller:

use App\Notifications\ExampleNotification;
use Illuminate\Support\Facades\Notification;

class UserController extends Controller
{
    public function sendNotification()
    {
        $user = auth()->user(); // or any user you want to send the notification to
        $user->notify(new ExampleNotification());
        
        return 'Notification sent!';
    }
}

Step 5 – Set up Route for Testing: Add a route in web.php to test the notification.

use App\Http\Controllers\UserController;

Route::get('/send-email-notification', [UserController::class, 'sendNotification']);

Step 6 – Test the Notification: Visit the route or trigger the method that sends the notification, and check your email to confirm it’s working correctly.

Share.
Leave A Reply

Exit mobile version