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

Posted

技术标签:

【中文标题】Laravel 8:将异常呈现为电子邮件的 html【英文标题】:Laravel 8: render exception as html for email 【发布时间】:2021-02-11 03:30:07 【问题描述】:

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

我当前的代码(适用于 Laravel 5)在 App\Exceptions\Handler 类中呈现和发送异常:

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);
        
    

问题是 \Symfony\Component\Debug\Exception\FlattenException 类在我升级后的应用程序中不再存在。

现在在 Laravel 8 中将异常呈现为 html 的适当方法是什么?

非常感谢您。

【问题讨论】:

试试这个use Symfony\Component\ErrorHandler\Exception\FlattenException; 【参考方案1】:

好的,我成功收到了电子邮件,这是代码

作曲家要求

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

app/Exceptions/Handler.php

<?php

namespace App\Exceptions;

use App\Mail\ExceptionOccurred;
use Exception;
use Throwable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpFoundation\Response;

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/ExceptionOccurred.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

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);
    

资源/视图/电子邮件/exception.blade.php

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

如果可行,请告诉我。

【讨论】:

非常感谢。它就像一个魅力:-)。

以上是关于Laravel 8:将异常呈现为电子邮件的 html的主要内容,如果未能解决你的问题,请参考以下文章

如何将忘记密码的邮件陷阱配置到 gmail laravel 8

Vue(Laravel 8)无法正确呈现存储在表中的数组

python 使用Python的PIL将文本呈现为图像 - http://effbot.org/imagingbook/overview.htm

使用 Summernote 在 Laravel 中动态内联附件

Laravel中利用队列发送邮件的方法示例

Laravel 邮件队列无限循环异常