Search code examples
phplaravel-5.3

Laravel 5.3 nl2br in email


I'm trying to nl2br my email in Laravel 5.3.

Dear {{ $user->name }} {{ $user->last_name }}, <br><br>

{{ nl2br($request['message']) }}

<br><br>
Regards,<br><br>

But the result is:

Dear John doe, 

Blablabla<br /> <br /> Blablabla<br /> <br /> blablabla<br /> <br /> blablabla

How can I make sure that the <br /> is realy a breakline?


Solution

  • This expression {{ $var }} is used to encode HTML characters, you should do the following:

    {!! nl2br(htmlspecialchars($request['message'])) !!}
    

    htmlspecialchars() : this encodes the special characters to avoid security issues

    nl2br() : this transforms the new lines to <br> tags, but AFTER it has already been secured

    {!! !!} : this tells Blade to not encode HTML characters when outputting the variable

    Note that this is not related to sending emails, it's the same thing for a normal view.