Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Send Errors to Email

Tags:

php

laravel

I am trying to figure out how I can send errors to my email in Laravel 5. I haven't had much luck finding any good resources.

There used to be good packages like: https://github.com/TheMonkeys/laravel-error-emailer That did this for you in Laravel 4.

They have yet to release a Laravel5 update because of the way they changed error handling... which I am also not to familiar with.

I have a few Laravel 5 applications which I need to monitor but I need a more efficient way of doing it besides checking error logs on storage.

Any help would be greatly appreciated. I know there are others out there seeking this information as well.

like image 287
West55 Avatar asked Oct 16 '15 06:10

West55


People also ask

How do you do error reporting in Laravel?

Through your config/app. php , set 'debug' => env('APP_DEBUG', false), to true . Or in a better way, check out your . env file and make sure to set the debug element to true.

What is SMTP in Laravel?

SMTP, Mailgun, Postmark, and Amazon SES are used in Laravel for sending simple, transactional, and bulk emails. Laravel has an email-sending library named SwiftMailer to send an email with an email template. This tutorial shows you how to send a simple email using SMTP.


1 Answers

You can do this by catching all the errors in the App\Exceptions\Handler::report(). So in you App/Exceptions/Handler.php add a report function if its not already there.

/**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $e
 * @return void
 */
public function report(\Exception $e)
{
    if ($e instanceof \Exception) {
        // emails.exception is the template of your email
        // it will have access to the $error that we are passing below
        Mail::send('emails.exception', ['error' => $e->getMessage()], function ($m) {
            $m->to('your email', 'your name')->subject('your email subject');
        });
    }

    return parent::report($e);
}

If you need more info, refer to laravel documentation form mailer and errors.

like image 137
Subash Avatar answered Oct 04 '22 18:10

Subash