In this tutorial you will learn about the Laravel 7/6 Send Error Exceptions on Mail/Email and its application with practical example.
In this Laravel 7/6 Send Error Exceptions on Mail/Email tutorial I will show you how to send error exceptions on email in laravel. In this tutorial you will learn to send error exceptions on email in laravel. Here you will learn to send error exceptions on developer mail or as you want any other mail. In laravel most of 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 7/6 Send Error Exceptions on Mail/Email
In Laravel, all exceptions are handled by the
1 |
App\Exceptions\Handler class. This class contains two methods: report and render. |
Laravel sends an error exception to mail steps
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
1 |
php artisan make:mail ExceptionMail |
This will create a class ExceptionMail in the app/Maildirectory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?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 have to create new view file inside the emails folder that file name email_exception.blade.php. and put the following code in and add your email_exception.blade.php file:
1 |
{!! $content !!} |
Send Error Exception Mail
Now, go to App\Exceptions\Handler file and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?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('developer@gmail.com')->send(new ExceptionMail($html)); } catch (Exception $ex) { dd($ex); } } } |
Now, any error exception is thrown by your application. You will notified via an email with full error exception detail.