How to send custom email in laravel 5.5

Published Date: 25-Nov-2017 | Tags: Laravel 5.5  Email & SMS  

This tutorial I want to show you how to send a custom email notification with laravel 5.5.

Suppose that your server is running on port 80 and your project folder is /demo.


  • I use laravelcollective to create form. So please install laravelcollective if you not yet install it. You can visit here for the installation guide.
  • I use bootstrap 4 for my UI, so please check bootstrap if you are new to bootstrap.


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.




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 CustomEmail

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

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


Step 3: Create Email Content

To use custom email template, first we should update __construct() and toMail() functions in this file /demo/app/Notifications/CustomEmail.php.

Please update the content of the functions as below.


namespace App\Notifications;

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

class CustomEmail extends Notification
    use Queueable;

     * Create a new notification instance.
     * @return void
    public function __construct($subject, $message)
        $this->subject = $subject;
        $this->message = $message;

     * 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)
            ->view('email_message', ['message' => $this->message]);

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

Then create view email_message.blade.php in /demo/resources/views/layout. for mail body. 

{!! nl2br($msg_body) !!}


Step 4: Add Notifiable Trait in User Model

To send notification, we have to use Notifiable trait in User model (/demo/app/User.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',


Step 5: Add Route

Add these lines to file /demo/routes/web.php.

Route::match(['get', 'post'], 'laravel-send-custom-email', 'EmailController@customEmail');


Step 6: Create View

Create layout view file default.blade.php in /demo/resources/views/layout. (create folder layout if it does not exist) 

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
<!-- Styles -->
    <link rel="stylesheet" href="" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<body style="padding-top: 100px;padding-bottom: 50px;background: whitesmoke">
<nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-info">
    <a class="navbar-brand" target="_blank" href="">CODOVEL.COM</a>
<script src=""></script>
<script src="" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>


Create view file custom_email.blade.php in /demo/resources/views.

        .form-group.required label:after {
            content: " *";
            color: red;
            font-weight: bold;
    <div class="container">
        <div class="col-md-8 offset-md-2">
            <h1>New Email</h1>
                {!! Form::model($customer,['method'=>'put']) !!}
                {!! Form::open() !!}
            <div class="form-group row required">
                {!! Form::label("to_email","To",["class"=>"col-form-label col-md-3 col-lg-2"]) !!}
                <div class="col-md-8">
                    {!! Form::text("to_email",null,["class"=>"form-control".($errors->has('to_email')?" is-invalid":""),"autofocus",'placeholder'=>'Email']) !!}
                    {!! $errors->first('to_email','<span class="invalid-feedback">:message</span>') !!}
            <div class="form-group row required">
                {!! Form::label("subject","Subject",["class"=>"col-form-label col-md-3 col-lg-2"]) !!}
                <div class="col-md-8">
                    {!! Form::text("subject",null,["class"=>"form-control".($errors->has('subject')?" is-invalid":""),'placeholder'=>'Subject']) !!}
                    {!! $errors->first('subject','<span class="invalid-feedback">:message</span>') !!}
            <div class="form-group row required">
                {!! Form::label("message","Message",["class"=>"col-form-label col-md-3 col-lg-2"]) !!}
                <div class="col-md-8">
                    {!! Form::textarea("message",null,["class"=>"form-control".($errors->has('message')?" is-invalid":""),'placeholder'=>'Message']) !!}
                    {!! $errors->first('message','<span class="invalid-feedback">:message</span>') !!}
            <div class="form-group row">
                <div class="col-md-3 col-lg-2"></div>
                <div class="col-md-4">
                    {!! Form::button("Send",["type" => "submit","class"=>"btn btn-primary"])!!}
            {!! Form::close() !!}


Step 6: Create Controller

Create controller file EmailController in /demo/app/Http/Cotrollers.

namespace App\Http\Controllers;

use App\Notifications\CustomEmail;
use App\User;
use Illuminate\Http\Request;

class EmailController extends Controller
   public function customEmail(Request $request)
        if ($request->isMethod('get'))
            return view('custom_email');
        else {
            $rules = [
                'to_email' => 'required|email',
                'subject' => 'required',
                'message' => 'required',
            $this->validate($request, $rules);
            $user = new User();
            $user->email = $request->to_email;
            $user->notify(new CustomEmail($request->subject, $request->message));
            $request->session()->put('status', true);
            return back();


Step 7: Test

Open your browser and type http://localhost/demo/public/laravel-send-custom-email. Then you will see it.


Hope you can do it.


Related Posts

© 2018 All Rights Reserved.