Laravel: HTML in notification

I use the default notification system (Laravel 5.3) to send an email. I want to add HTML tags to the message. This does not work (displays strong tags as 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 this is normal because the text is displayed {{ $text }}in the email notification template. I tried to use the same system as in csrf_field():

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

But this does not work: it shows strong text.

Can I send HTML tags without changing the view? (I do not want to change the view: text protection is suitable for all other cases). Hope this is clear enough, sorry if not.

+5
4

, MailClass, MailMessage.

, app\Notifications

<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;

class MailExtended extends MailMessage
{
    /**
     * The notification data.
     *
     * @var string|null
     */
    public $viewData;

    /**
     * Set the content of the notification.
     *
     * @param string $greeting
     *
     * @return $this
     */
    public function content($content)
    {
        $this->viewData['content'] = $content;

        return $this;
    }

    /**
     * Get the data array for the mail message.
     *
     * @return array
     */
    public function data()
    {
        return array_merge($this->toArray(), $this->viewData);
    }
}

:

return (new MailMessage())

:

return (new MailExtended())

content var . , (php artisan vendor:publish), email.blade.php resources/views/vendor/notifications :

@if (isset($content))
<hr>
    {!! $content !!}
<hr>
@endif

: D

+5

php artisan vendor:publish, email.blade.php resources/views/vendor/notifications vendor.

{{ $line }} {!! $line !!} . Laravel 5.3 101 137 .

unescaped line , HTML .

+7

, @eric-lagarda, , email.blade.php email.blade.php , Laravel , email.blade.php HTML content <code>. , , . email.blade.php email.blade.php email.blade.php ( / ):

@if (isset($content))
<hr>
{!! $content !!}
<hr>
@endif
+1

If you just want to add a little basic style to the template, you can use Markdown in the method line()without having to change any other code.

0
source

Source: https://habr.com/ru/post/1665877/


All Articles