Laravel-generated 电子邮件格式不正确 HTML

Laravel-generated email not formatting HTML correctly

我正在努力解决 Laravel 的电子邮件格式问题。 我从数据库中获取了电子邮件内容 (HTML),这并不重要,但随后添加了引号,格式错误,我的电子邮件如下所示:

这是我的代码,非常感谢您的帮助!

我试过 'content' => htmlspecialchars($content) 和 'content' => htmlentities($content) 但是 none 有效,对于 blade 文件:

<div>
    {{!!$content!!}}
</div>

给我一个错误。我也试过了

<div>
    {{{$content}}}
</div>

(也是意外字符的错误)和

<div>
    {{$content}}
</div>

(这是原来的)

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cookie;

class InsuranceEmail extends Mailable
{
    use Queueable, SerializesModels;
    protected $attacheddoc;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($attacheddoc)
    {
        $this->attacheddoc=$attacheddoc;
    }

    /**
     * Build the message.rubr
     *
     * @return $this
     */
    public function build()
    {
        $name = Auth::user()->nom . " " .  Auth::user()->prenom;

        $sqlContent="SELECT texte from blabla";

        $content = DB::connection('blabla')->select( DB::connection('blabla')->raw($sqlContent))[0]->texte;
        $content = str_replace('#memberName#', $name, $content);
        $content = str_replace('"', '', $content); //I tried this, without any hope ;)

        return $this->from('contact@blabla.net')
                ->markdown('emails.blabla')->with([
                    'title' => "Email onject",
                    'memberName' => $name,
                    'content' => $content,
                ])
                ->attach($this->attacheddoc, array(
                    'as' => 'attacheddoc.pdf', 
                    'mime' => 'application/pdf'));
    }
}

在您的 emails.blabla 中查看我们这样它会逃脱 HTML 元素

{{{ $content }}}

或尝试

{!! $content !!}

根据 Laravel 文档:

By default, Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

参考:

Laravel -> Blade Templates -> Displaying Unescaped Data

我尝试了一些方法来修复我的电子邮件显示不正确的问题。最后清除我的视图缓存解决了我的问题,我没有看到其他人建议。这很可能不是您的问题,但我会将其包含在我的回答中,希望能帮助其他人解决我的问题。

发布 Laravel 电子邮件浏览量

php artisan vendor:publish --tag=laravel-mail

确保 html 视图中没有缩进

resources/views/vendor/mail/html

确保转义任何通过变量

插入的html
{!! $content !!}

清除缓存的视图

php artisan view:clear