Reputation: 1056
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:
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('[email protected]')
->markdown('emails.blabla')->with([
'title' => "Email onject",
'memberName' => $name,
'content' => $content,
])
->attach($this->attacheddoc, array(
'as' => 'attacheddoc.pdf',
'mime' => 'application/pdf'));
}
}
Upvotes: 1
Views: 2930
Reputation: 392
Better late than never: I just stumbled upon this issue, and the point in my case was that I was using markdown.
->markdown('emails.blabla')->with(
as stated in the Laravel Doc:
Do not use excess indentation when writing Markdown emails. Per Markdown standards, Markdown parsers will render indented content as code blocks.
In fact not only indentation was provoking in my case (laravel 11) blocks to be interpreted as code blocks and thus showing html code itself, but also line breaks inbetween html blocks for nicer reading. Ideally for the markdown to interpret the html as html and not as code, you have no empty lines and no indentation of more than one level.
@component('mail::message')
<h2>Hello {{$body['name']}},</h2>
<p>Cron {{$body['cronname']}} was successfully executed</p>
<div>
@foreach ($body['alerts'] as $alerts)
<div>
@foreach ($alerts as $alert)
@foreach ($alert as $al)
<p>{!! $al !!}</p>
@endforeach
@endforeach
</div>
@endforeach
</div>
<h3>next section</h3>
...
@endcomponent
Upvotes: 0
Reputation: 424
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
Upvotes: 4
Reputation: 4826
In your emails.blabla view us elike this it will escape HTML element
{{{ $content }}}
or try
{!! $content !!}
Upvotes: 1
Reputation: 5662
As per Laravel Documentation:
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 !!}.
Reference:
Laravel -> Blade Templates -> Displaying Unescaped Data
Upvotes: 1