简体   繁体   中英

Mail The Error In Laravel 5

I want to send mail to an email address whenever any kind of error occurs. Until now I have been working on the development environment. To accomplish this I used the code from here and implemented that code in app/Exceptions/Handler.php .

When an error occurs, it neither displays the error nor sends an email.

My code is below :

public function report(Exception $e)
{
     if ($e instanceof \Exception) {
        //dd($e->getLine()); <-- gives output 
        // emails.exception is the template of your email
        // it will have access to the $error that we are passing below
        $mail = Mail::send('emails.error_report', ['content' => $e->getMessage()], function ($m) {
            $m->to('mail@domain.com', 'Name')->subject('Error Report');
        });
        return parent::report($e);
     }
     return parent::report($e);
}

After research of few hours, I have managed to get idea what could be the reason behind this. What I think is, we are already handling an error, and while handling this error, what happens if another error occurs? means, I had an error in the mail function view file. which could not be handled further, while already handling an error.

So the Answer was correct. it was my specific problem. But still i would like to share the code with some improvisations,though code is not much different from the linked thread.

         public function report(Exception $e)
    {
         if ($e instanceof Exception) {
                // whenever any error occurs, a mail will be sent to desired email. 
                // content variable will contain message that you can send to emails.error_report(or whatever) view. 
                // you can use this variable simply echoing $content.
                $content = "There was an error occured on Your Website.following are details of Error:<br><br>Message : ".$e->getMessage()."<br>Line : ".$e->getLine()."<br>File : ".$e->getFile()."";
                $mail_content = [
                        'content' => $content,
                    ];
                // if you want to mail that error message to multiple email addresses put those addresses in $emails array. 
                $emails  = array('mail1@domain.com','mail2@gmail.com');
                $mail = Mail::send('emails.error_report', $mail_content, function ($message) use ($emails) {
                        $message->to($emails, 'Maninder')->subject('Error Report');
                    });
            }
    return parent::report($e);
    }

Please do not forget to add use Illuminate\\Support\\Facades\\Mail; on the top of app/Exceptions/Handler.php file.

After using working code for 1 day, i noticed that i got tons of mails regarding errors that are not needed to be reported to the admin.so i got after making little modification, admin will get email only when any fatal error occurs.though, if you want to be notified for all exceptions you can keep the code as it is above or make modification as below:

 if ($e instanceof \Symfony\Component\Debug\Exception\FatalErrorException) {

Instead of:

if ($e instanceof Exception) {

Happy coding for new comers like me! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM