htmllaravelemaillaravel-5html-email

Laravel-generated email not formatting HTML correctly


I am struggling with email formatting issue, with Laravel. I get the email content (HTML) from the database, which doesn't really matter, but then quotes get added around, the format is wrong and my email looks like this:

t formatting my email corres

Here is my code, thanks a lot for your help!

I tried with 'content' => htmlspecialchars($content) and 'content' => htmlentities($content) but none work, and for the blade file:

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

gives me an error. I also tried

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

(also an error of unexpected character) and

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

(here was the original one)

<?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'));
    }
}

Solution

  • I tried a few things to try and fix my email displaying incorrectly. In the end clearing my view cache solved my problem, which I hadn't seen anyone else suggest. This most likely wasn't your issue here but I will include it in my answer to hopefully help anyone else with my issue.

    Publish the Laravel email views

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

    Make sure there are no indents in your html views

    resources/views/vendor/mail/html
    

    Make sure to escape any html inserted via variable

    {!! $content !!}
    

    Clear cached views

    php artisan view:clear