Framework: Laravel 5.7 Notification ->line there it can't display html tag but it was working in 5.5

Created on 14 Jan 2019  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 5.7.20
  • PHP Version: 7.2.13
  • Database Driver & Version: MySQL 5.5.5

Description:

I have the same code notification in Laravel 5.5 and 5.7, for Laravel 5.5, I am able to use in ->line, but in 5.7 it will escape it.

In my notification blade view file, I set {{ ]} or {!! !!}, I still can't display html in my email. In 5.5, I do not need to escape it still work.

 \\this is when I not escape
 <strong>2019-01-13 15:41</strong>



  \\this is when I escape
  <strong>2019-01-13 15:37</strong>

what I want is display bold like below.

2019-01-13 15:37

both method also displayed the html tag, It will not bold the text. The same coding work in 5.5.32 (which will bold the text) but not Laravel version 5.7.20

Steps To Reproduce:

In Notification File

public function toMail($notifiable)
{
    return (new MailMessage)
        ->line('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>');
}
needs more info

Most helpful comment

You don't need withoutDoubleEncoding in 5.7 so you can remove that.

As you can see lines are being escaped in the template: https://github.com/laravel/framework/blob/d818fd18ccdce9f42ed95193ec5632d3afc84007/src/Illuminate/Notifications/resources/views/email.blade.php#L15

So you'll need to indicate that the line has HTML in it. Try this:

->line(new HtmlString('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>'));

All 7 comments

Hey, there was significant HTML Entity Encoding changes in 5.5 -> 5.6.
https://laravel.com/docs/5.6/upgrade

Does your notification email extend email.blade.php? if so,

Make sure you have the latest email.blade.php for 5.7:
https://github.com/laravel/framework/blob/5.7/src/Illuminate/Notifications/resources/views/email.blade.php

I am running fresh new laravel 5.7 project, I just reference back the code from 5.5 to 5.7 for the notification class only.
I read the upgraded note and I also added in AppServiceProvider.php, but it seem like the same.

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::withoutDoubleEncoding();
    }
}

Do you try in your project to run the follow code?
can you get bold font?

You don't need withoutDoubleEncoding in 5.7 so you can remove that.

As you can see lines are being escaped in the template: https://github.com/laravel/framework/blob/d818fd18ccdce9f42ed95193ec5632d3afc84007/src/Illuminate/Notifications/resources/views/email.blade.php#L15

So you'll need to indicate that the line has HTML in it. Try this:

->line(new HtmlString('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>'));

Thank @driesvints !
This is the exact solution I need! This kind of small detail really need to have a place to mention out. I have been searching everywhere. Appreciate that!

Sorry, I test in my fresh new Laravel project, it is working. However, I work my own project. It does not work.
Few information are different. I am using a custom markdown template. The template is copy from
laravel-project/resources/views/vendor/notifications/email.blade.php
which is generated after the command php artisan vendor:publish --tag=laravel-notifications

This is working:-

return (new MailMessage)
            ->line(new HtmlString('The <strong>introduction</strong> to the notification.'))
            ->line('The <strong>introduction</strong> to the notification.')
            ->line(new HtmlString('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>'))
            ->line('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>')
            ->action('Notification Action', url('/'));

image

This is not working. Which is using my own markdown

return (new MailMessage)
            ->line(new HtmlString('The <strong>introduction</strong> to the notification.'))
            ->line('The <strong>introduction</strong> to the notification.')
            ->line(new HtmlString('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>'))
            ->line('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>')
            ->action('Notification Action', url('/'))
            ->markdown('mail.notification.permission');

image

my mail.notification.permission file is copy exactly from laravel-project/resources/views/vendor/notifications/email.blade.php

I think I need view not a markdown. But I change ->view('mail.notification.permission'); I got error No hint path defined for [mail]. (View: /Users/shiro/Sites/laravel-project/resources/views/mail/notification/permission.blade.php)

which file I should copy in order for me using html, not the markup format.

@shiroamada seems like something is just wrongly configured somewhere. It's best that you take this to one of the support channels below as as I doubt this is a bug with the framework.

Thanks @driesvints you saved me aswell

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixsanz picture felixsanz  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

fideloper picture fideloper  路  3Comments

JamborJan picture JamborJan  路  3Comments

kerbylav picture kerbylav  路  3Comments