Laravel 8:将异常呈现为电子邮件的html



我将我的Laravel应用程序升级到了最新版本Laravel 8。我现在的问题是,我不知道如何将应用程序抛出的异常呈现为html,以便将该html作为电子邮件发送。

我当前的代码(适用于Laravel 5(在AppExceptionsHandler类中呈现并发送异常:

public function sendEmail(Exception $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new SymfonyExceptionHandler();
$html = $handler->getHtml($e);
$routeName = URL::full();
Mail::send(new ExceptionEmail($html, $routeName));
} catch (Exception $ex) {
if (env("APP_DEBUG") == true) {
dd($ex);
}
}
}

问题是SymfonyComponentDebugExceptionFlattenException类在我升级的应用程序中已不存在。

在Laravel 8中,将异常呈现为html的合适方式是什么?

事先非常感谢。

好吧,我设法收到了电子邮件,这是代码

作曲家要求

"require": {
"php": "^7.3|^8.0",
.....
"jeremykenedy/laravel-exception-notifier": "^2.2",
"laravel/framework": "^8.40",
.....
},

app/Exceptions/Handler.php

<?php
namespace AppExceptions;
use AppMailExceptionOccurred;
use Exception;
use Throwable;
use IlluminateHttpRequest;
use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesMail;
use SymfonyComponentErrorHandlerErrorRendererHtmlErrorRenderer;
use SymfonyComponentErrorHandlerExceptionFlattenException;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use SymfonyComponentHttpFoundationResponse;
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 = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
}
/**
* Report or log an exception.
*
* @param Exception $e
* @return void
* @throws Throwable
*/
public function report(Throwable $e)
{
if ($this->shouldReport($e)) {
$this->sendEmail($e); // sends an email
}
parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Throwable $e
* @return Response
*
* @throws Throwable
*/
public function render($request, Throwable $e): Response
{
return parent::render($request, $e);
}
public function sendEmail(Throwable $exception)
{
try {
$e = FlattenException::createFromThrowable($exception);
$handler = new HtmlErrorRenderer(true);
$css = $handler->getStylesheet();
$content = $handler->getBody($e);
Mail::to('your_email_address_here')->send(new ExceptionOccurred($content,$css));
} catch (Throwable $exception) {
Log::error($exception);
}
}
}

app/Mail/ExceptionOccured.php

<?php
namespace AppMail;
use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
class ExceptionOccurred extends Mailable
{
use Queueable, SerializesModels;
private $content;
private $css;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($content,$css)
{
$this->content = $content;
$this->css = $css;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$emailsTo = str_getcsv(config('exceptions.emailExceptionsTo'), ',');
$ccEmails = str_getcsv(config('exceptions.emailExceptionCCto'), ',');
$bccEmails = str_getcsv(config('exceptions.emailExceptionBCCto'), ',');
$fromSender = config('exceptions.emailExceptionFrom');
$subject = config('exceptions.emailExceptionSubject');
if ($emailsTo[0] === null) {
$emailsTo = config('exceptions.emailExceptionsToDefault');
}
if ($ccEmails[0] === null) {
$ccEmails = config('exceptions.emailExceptionCCtoDefault');
}
if ($bccEmails[0] === null) {
$bccEmails = config('exceptions.emailExceptionBCCtoDefault');
}
if (! $fromSender) {
$fromSender = config('exceptions.emailExceptionFromDefault');
}
if (! $subject) {
$subject = config('exceptions.emailExceptionSubjectDefault');
}
return $this->from($fromSender)
->to($emailsTo)
->cc($ccEmails)
->bcc($bccEmails)
->subject($subject)
->view(config('exceptions.emailExceptionView'))
->with('content', $this->content)
->with('css', $this->css);
}
}

resources/views/emails/exception.blade.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>{!! $css ?? '' !!}</style>
</head>
<body>
{!! $content ?? '' !!}
</body>
</html>

请告诉我这是否有效。

最新更新