How to Send Error Exceptions on an Email

How to Send Error Exceptions on an Email

Overview

Laravel sends error exceptions mail, this tutorial shows you how you can send error exceptions on developer mail. Here you will learn to send error exceptions on developer mail or as you want any other mail.

If you have deployed your larval application on a live server. Your live laravel app is running fine. If there were no issues with the laravel application. Sometimes, major error exceptions came for your laravel app. The app will be down and the user will leave your application

In Laravel, all exceptions are handled by the App\Exceptions\Handler class. This class contains two methods: report and render.

Laravel sends an error exception to mail steps

In some cases, what if you were immediately informed via e-mail (or any other service) about the bug and you fix it ASAP. In Laravel, this can be done easily. We will learn how to send error exceptions mail step by step in this post.

We are going to show you how you can send error exception mail in your laravel app. Just follow the few steps and it has finished.

  • Create Mail Class
  • Create an Email View
  • Send Error Exception Mail

Create Mail Class

First of all, create a mail class using the below command:

 php artisan make:mail ExceptionMail

This will create a class ExceptionMail in the app/Maildirectory.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ExceptionMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * The body of the message.
     *
     * @var string
     */
    public $content;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($content)
    {
        $this->content = $content;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.exception_email')
                    ->with('content', $this->content);
    }
}

Create Email View

Now, we will create a new view file inside the emails folder that file name email_exception.blade.php. and the below-given code in add your email_exception.blade.php file:

{!! $content !!}

Send Error Exception Mail

Go to App\Exceptions\Handler file and update the below code into your file.

<?php

namespace App\Exceptions;

use Mail;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
use App\Mail\ExceptionOccured;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {   
       if ($this->shouldReport($exception)) {
            $this->sendEmail($exception); // sends an email
        }
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }

    public function sendEmail(Exception $exception)
    {
        try {
            $e = FlattenException::create($exception);

            $handler = new SymfonyExceptionHandler();

            $html = $handler->getHtml($e);

            Mail::to('[email protected]')->send(new ExceptionMail($html));
        } catch (Exception $ex) {
            dd($ex);
        }
    }
}

In case, error exception is thrown by your application. You will receive an email with full information about the errors.

Conclusion

In this tutorial, you have learned step by step how to send error exceptions mail on laravel application.

If you were immediately informed via e-mail (or any other service) about the bug and you fix it ASAP. In Laravel, this can be done easily. We have learned how to send error exceptions mail step by step in this post.

Thanks to here, Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *