Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 adding HTML to email

I'm currently trying to create a HTML email in Laravel 5 and I have some text (which contains <br/> elements) I want to insert in the email. I use the following piece of code to send the email:

Mail::send(array('html' => 'emails.newinvoice'), array('text' => $emailtext), function($message) use ($email, $subject, $contact_company)
{
    $message->to($email, $contact_company)->subject($subject);
});

So the $emailtext variable contains some text with HTML tags. In my emails.newinvoice layout view, I have the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head></head>
    <body>
        <p>{{{ $text }}}</p>
    </body>
</html>

When sending the email, the inserted text in my mail and the HTML elements in that text are displayed as normal characters which means that my email shows up like:

test<br/>test

Instead of

test
test

How can I make sure that the HTML tags in the inserted text get rendered as HTML and not as plain text?

like image 531
Devos50 Avatar asked Apr 27 '15 09:04

Devos50


2 Answers

You need to specify html key in the first parameter:

Mail::send( ['html' => 'emails.newinvoice'], ['text' => $emailtext], 
//           ^^^^

Also replace auto-escaped block {{ }} with unescaped {!! !!} in the template:

<p> {!! $text !!} </p>
like image 100
Limon Monte Avatar answered Nov 14 '22 13:11

Limon Monte


You need to use:

{!! $text !!}

instead of

{{ $text }}

Blade automatically escapes any html when echoing unless you explicitly tell it not to.

like image 19
Igor Pantović Avatar answered Nov 14 '22 13:11

Igor Pantović