Laravel 5向电子邮件发送错误

Wes*_*t55 11 php laravel

我试图找出如何在Laravel 5中向我的电子邮件发送错误.我没有太多运气找到任何好的资源.

以前有很好的软件包:https: //github.com/TheMonkeys/laravel-error-emailer 这是在Laravel 4中为你做的.

他们还没有发布Laravel5更新,因为他们改变了错误处理的方式......我也不熟悉.

我有一些我需要监控的Laravel 5应用程序,但除了检查存储上的错误日志之外,我还需要一种更有效的方法.

任何帮助将不胜感激.我知道还有其他人也在寻找这些信息.

Sub*_*ash 27

你可以通过捕获所有错误来做到这一点App\Exceptions\Handler::report().所以在你App/Exceptions/Handler.php添加一个report函数,如果它还没有.

/**
 * 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);
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请参阅laravel文档表单邮件错误.

  • 我认为您需要添加“使用邮件”;如果不是,您将收到“Class 'Illuminate\Foundation\Exceptions\Mail' not found in”的代码 (2认同)

Wes*_*t55 7

编辑:我找到了为Laravel构建的第三方日志系统

www.understand.io

非常好的解决方案,不给我发电子邮件,但这适用于我需要的.

我玩弄了这个并浏览了Laravel核心文件,并提出了类似于您在错误页面上看到的内容.

您只需创建一个视图文件,即回显电子邮件的$ content

public function report(\Exception $e)
{
    if ($e instanceof \Exception) {

        $debugSetting = Config::get('app.debug');

        Config::set('app.debug', true);
        if (ExceptionHandler::isHttpException($e)) {
            $content = ExceptionHandler::toIlluminateResponse(ExceptionHandler::renderHttpException($e), $e);
        } else {
            $content = ExceptionHandler::toIlluminateResponse(ExceptionHandler::convertExceptionToResponse($e), $e);
        }

        Config::set('app.debug', $debugSetting);

        $data['content'] = (!isset($content->original)) ? $e->getMessage() : $content->original;

        Mail::queue('errors.emails.error', $data, function ($m) {
            $m->to('email@email.com', 'Server Message')->subject('Error');
        });
    }

    return parent::report($e);
}
Run Code Online (Sandbox Code Playgroud)

  • 我想说的是,你的答案对你的问题太具体了.但我建议的答案将帮助其他许多人解决您的问题.好的问题或答案应该是帮助尽可能多的人,而不仅仅是1. (3认同)

hog*_*gan 5

其他答案似乎很正确。我们不久前已经这样做了,并发现了一个大问题:如果邮件命令失败,可能会导致无限循环,抛出错误并尝试发送相应的电子邮件,这将再次导致失败......这将很快填满日志并杀死您的服务器。

请记住这一点,在这种情况下不要发送电子邮件。

旁注:我决定将其放在答案中,因为它与所有答案相关,不应隐藏在一个评论中。

  • 我们可以只使用 `try { // 将错误发送给某人 } catch { // 捕获任何异常并且什么都不做 // 这样就永远不会发生无限循环 }` (5认同)