Laravel 5 Mail template showing html tags
Asked Answered
R

2

7

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?

Reiff answered 31/5, 2018 at 13:57 Comment(0)
K
7

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>
Kyne answered 31/5, 2018 at 14:17 Comment(1)
Thanks it was helpful :)Goring
R
2

Use {!! $content !!} instead of {{ $content }} It will work perfectly.I had faced the same problem.

Reliable answered 12/3, 2019 at 6:35 Comment(1)
Hey Just Send me the link Of that Wordpress Website.Mencius

© 2022 - 2024 — McMap. All rights reserved.