Home » Laravel MailMessage Component for Effortless Emailing

Laravel MailMessage Component for Effortless Emailing

Loved it? Please Share on Social Media

Introduction

Hey, what’s up, folks? I hope you are doing great. Today, I will talk about a slightly different and exciting topic in the Laravel ecosystem: the Laravel message component. In this tutorial, we will learn how to send a notification using the laravel mailmessage component without wasting too much time on writing codes. In the tutorial, I’ve shown some easy steps to jumpstart the Laravel mail message component quickly.

Creating file for Laravel MailMessage Component

First, you must write down the below command to generate the notification file.

php artisan make:notification UserCreated

Once you finish the task, you will find a UserCreated file in the app‘s Notifications folder. Now open the file and follow the next steps.

Quick Tip About Laravel MailMessage Component: 

When sending notifications to a user/admin/customer, you must add a Notifiable trait on your desired model where you want to send messages.

Customizing the file for Sending Laravel MailMessage

The next thing is customizing our UserCreated file. You have to copy and paste the code below into your UserCreated file.

<?php

namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class UserCreated extends Notification

{

    use Queueable;

    /**
     * Create a new notification instance.
     */

    public function __construct()

    {
        //
    }

    /**

     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */

    public function via(object $notifiable): array

    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */

    public function toMail(object $notifiable): MailMessage

    {
        return (new MailMessage)

->subject('Your order has been created.')

->line('Thank you for using our app!')    
}

    /**
     * Get the array representation of the notification.
     *

     * @return array<string, mixed>
     */

    public function toArray(object $notifiable): array

    {
        return [
            //
        ];
    }
}

Adding Some Logic to Controller

Now, you need to create a controller called UserRegistrationController. After that, create a send_mail_notification function inside it. Then, you need to paste the code below on your controller.

public function send_mail_notification() {
    $user = User::find(1);
    Notification::send($user, new UserUpdated());
}

Next, we need to configure the send notification route. You need to copy and paste the code below into your web.php file.

Configuring Route for Sending Laravel MailMessage

Route::post('notification-mail', [UserRegistrationController::class, 'send_mail_notification']);

Finally, we need some credentials for testing our laravel mailmessage notifications. For this specific task, we will use MailTrap; you need to sign up there, get the API credentials from there, and paste your API credentials to your .env file. You will get credentials like mine, which you can see below.

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=**************
MAIL_PASSWORD=**************

At last, we will start our local server using our artisan command php artisan serve. If there is no problem, our project is now running at http://127.0.0.1:8000/ or http://locahost:8000/.

Conclusion:

Finished! That’s all about our laravel mailmessage tutorial. However, you might learn something different from this tutorial about the laravel mailmessage component. If you want to learn more you can check out official documentation. Still, It will give you some fundamental ideas for making this functionality on your application in the future. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. You can contact us via our Contact Us page

FAQ’s

Which email driver is best for Laravel?

I recommend using these drivers: Mailgun Driver, Postmark Driver, SES Driver, and MailerSend Driver because the Laravel core team recommends it.

How to create a mail in Laravel?

You need to write php artisan make:mail your_mail_name and enter ola! You have created your mail in Laravel. Isn’t that simple?

How to encrypt email in Laravel?

You can encrypt your email using the laravel MAIL_ENCRYPTION environment variable. If your mail service provider provides an encrypted email, you can use it in your application.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.