I have the main mail template (resources/views/mail.blade.php). This is a common template to use all of my mail like for forgot password or for change new password. The content for mail.blade.php is below:
<table>
<tr><td>SiteName</td>
</tr>
<tr><td>{{$content}}</td></tr>
</table>
I'm storing the content of email template (in mySql db) through CKEditor and it looks like:
<p>Dear {{$username}},</p>
<p>This is your new password: {{$newPassword}}</p>
Now I using mail function in laravel 5.5 as below:
$content = str_replace(array('username', 'newPassword'), array($userName, $request->confirm_password), addslashes($emailTemplate->templateBody));
Mail::send(['html' => 'mail'], ['content' => $content], function ($message) use($emailTemplate, $user){
$message->from($emailTemplate->fromEmail, $emailTemplate->fromName);
$message->to($user->email);
});
After sending email in mailtrap.io, I see the mail looks like:
SiteName
<p>Dear Niladri,</p> <p>This is your new password: 123456</p>
Please note, the table, tr, td where SiteName is written in the mail.blade is working and no HTML code is showing in the email. That's fine. But only the content from the CKEditor is showing with HTML tags (<p></p>
).
Have I did anything wrong?
To use HTML content from a PHP variable within a .blade.php
file, you need to use {!! $variable !!}
instead of {{ $variable }}
. The first will render your HTML, the second will output it as a string, including the HTML tags. Your mail.blade.php
file should look like:
<table>
<tr>
<td>SiteName</td>
</tr>
<tr>
<td>{!! $content !!}</td>
</tr>
</table>
Use {!! $content !!}
instead of {{ $content }}
It will work perfectly.I had faced the same problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With