How to send email notification in laravel 5.5

Published Date: 05-May-2018 | Tags: Laravel 5.5  Email & SMS  

This tutorial I want to show you how to send email notification with laravel 5.5 by using its built-in email template.

 

Step 1: Configure Email Sender

Sending an email, normally it has sender and receiver. So to be able to send email from the system, we have to configure sender.

Please edit .env file and create or update its variables as below.

APP_NAME="YOUR_APP_NAME"

MAIL_DRIVER=smtp
MAIL_HOST="YOUR_EMAIL_SERVER"
MAIL_PORT=25
MAIL_USERNAME="SENDER_EMAIL_ADDRESS"
MAIL_PASSWORD="EMAIL_PASSWORD"
MAIL_ENCRYPTION=null
MAIL_FROM_NAME="SENDER NAME"
MAIL_FROM_ADDRESS="SENDER_EMAIL_ADDRESS"

 

Step 2: Create Email Notification File

Please go to command prompt or terminal, and navigate to your project folder (/demo) and run this command.

php artisan make:notification TemplateEmail

The above command will create a class file TemplateEmail.php in /demo/app/Notifications.

Note:‚Äč You can use any class name, in here I use TemplateEmail.

 

Step 3: Create Email Content

Now we want to use built-in email template from laravel, so we can update toMail() function in this file /demo/app/Notifications/TemplateEmail.php. Please update the content of the function as below.

<?php

namespace App\Notifications;

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

class TemplateEmail extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
//          ->subject('Your Subject') // it will use this class name if you don't specify
//          ->greeting('') // example: Dear Sir, Hello Madam, etc ...
            ->level('info')// It is kind of email. Available options: info, success, error. Default: info
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
//          ->salutation('')  // example: best regards, thanks, etc ...
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

 

Step 4: How to Send Email

To be able to send notification, we have to use Notifiable trait in User model (/demo/app/User.php). 

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
   
    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

 Then you can use the code below to send.

 

$user = new User();
$user->email = 'senghok.eang@gmail.com';   // This is the email you want to send to.
$user->notify(new TemplateEmail());

Note: Till this step, you can can route or controller by yourself to test it. I think some of you may know how to do already.

But if you still don't know how to send the email, please check this similiar post for more detail How to send custom email in laravel 5.5.

This is the email you will see.

 laravel-email-template

Hope you can do it.



Related Posts

© 2018 All Rights Reserved.