Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: HTML in notification

I'm using the default notification system (Laravel 5.3) to send an email. I want to add HTML tags in message. This does not work (it displays the strong tags in plain text):

public function toMail($notifiable)
{
    return (new MailMessage)
                ->subject('Info')
                ->line("Hello <strong>World</strong>")
                ->action('Voir le reporting', config('app.url'));
}

I know it's normal because text is displayed in {{ $text }} in the mail notification template. I tried to use the same system as in csrf_field() helper:

->line( new \Illuminate\Support\HtmlString('Hello <strong>World</strong>') )

But it does not work: it displays strong as plain text.

Can I send HTML tags without changing the view? (I don't want to change the view: protecting text is OK for all other cases). Hope it's clear enough, sorry if not.

like image 574
rap-2-h Avatar asked Jan 05 '17 13:01

rap-2-h


Video Answer


1 Answers

Run php artisan vendor:publish command which will copy email.blade.php to resources/views/vendor/notifications from vendor directory.

Open this view and change {{ $line }} to {!! $line !!} in two places. In Laravel 5.3 these are 101 and 137 lines in the view.

This will display unescaped line strings which will allow you to use HTML tags in notification emails.

like image 71
Alexey Mezenin Avatar answered Oct 27 '22 23:10

Alexey Mezenin