Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Mail template showing html tags

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?

like image 759
Niladri Banerjee - Uttarpara Avatar asked May 31 '18 13:05

Niladri Banerjee - Uttarpara


2 Answers

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>
like image 56
Tim Lewis Avatar answered Oct 18 '22 17:10

Tim Lewis


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

like image 39
Chandni Handa Avatar answered Oct 18 '22 17:10

Chandni Handa