2016-04-06 30 views
1

何らかのエラーが発生したときにメールアドレスにメールを送信したいと思います。今まで私は開発環境に取り組んできました。これを達成するために hereのコードを使用し、そのコードをapp/Exceptions/Handler.phpに実装しました。メールLaravel 5のエラー

エラーが発生すると、エラーを表示せず、電子メールも送信しません。

私のコードは以下の通りです:

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('[email protected]', 'Name')->subject('Error Report'); 
     }); 
     return parent::report($e); 
    } 
    return parent::report($e); 
} 

答えて

0

数時間の研究の後、私はこの背後にある理由は何ができるかアイデアを得るために管理しています。私たちはすでにエラーを処理していますが、このエラーを処理している間に、別のエラーが発生した場合はどうなりますか?つまり、メール機能のビューファイルにエラーがありました。すでに処理している間に、それ以上処理することはできませんでした。

したがって、Answerは正しいです。それは私の特定の問題でした。しかし、コードはリンクされたスレッドとあまり違いはありませんが、私はいくつかの即興でコードを共有したいと思います。

  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('[email protected]','[email protected]'); 
       $mail = Mail::send('emails.error_report', $mail_content, function ($message) use ($emails) { 
         $message->to($emails, 'Maninder')->subject('Error Report'); 
        }); 
      } 
    return parent::report($e); 
    } 

イルミネーション\サポート\ファサード\メールを追加することを忘れないでください。 app/Exceptions/Handler.phpファイルの一番上にあります。

1日の作業コードを使用した後、私は少し修正を行った後に得たadmin.soに報告する必要がされていないエラーに関するメールのトンは、管理者が電子メールを取得する得たことに気づいたときにのみ致命的なエラーがあなたはすべての例外を通知する場合occurs.though、あなたはそれが上にあるように、コードを維持するか、以下のように修正を行うことができます。

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

の代わりに:

if ($e instanceof Exception) { 

新規参入のためのハッピーコーディング私のような! ;)