Laravel: HTML 在通知中
Laravel: HTML in notification
我正在使用默认 notification system (Laravel 5.3) 发送电子邮件。我想在消息中添加 HTML 标签。这不起作用(它以纯文本显示强标签):
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Info')
->line("Hello <strong>World</strong>")
->action('Voir le reporting', config('app.url'));
}
我知道这很正常,因为邮件通知模板中的 {{ $text }}
中显示了文本。我尝试使用与 csrf_field()
助手相同的系统:
->line( new \Illuminate\Support\HtmlString('Hello <strong>World</strong>') )
但它不起作用:它显示为纯文本。
我可以在不更改视图的情况下发送 HTML 标签吗?(我不想更改视图:在所有其他情况下都可以保护文本)。希望它足够清楚,如果不是很抱歉。
运行 php artisan vendor:publish
命令将从 vendor
目录复制 email.blade.php
到 resources/views/vendor/notifications
。
打开此视图并在两个地方将 {{ $line }}
更改为 {!! $line !!}
。在 Laravel 5.3 中,这些是视图中的 101
和 137
行。
这将显示 unescaped line
字符串,允许您在通知电子邮件中使用 HTML 标签。
好吧,您还可以创建一个新邮件Class 扩展 MailMessage
Class.
例如,您可以在 app\Notifications
中创建此 class
<?php
namespace App\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
class MailExtended extends MailMessage
{
/**
* The notification's 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
),您可以在 resources/views/vendor/notifications
中编辑 email.blade.php
并附加:
@if (isset($content))
<hr>
{!! $content !!}
<hr>
@endif
我们就是这样做的,而且工作起来很有魅力:D
对于像我一样遵循@eric-lagarda 方法的任何人,请记住不要像通常在视图中那样在自定义 email.blade.php
视图中标记内容,因为它会被 Laravel 的 markdown 解析器作为代码,将整个 content
HTML 包装在 <code>
HTML 标签中。这让我很头疼,但由于 this 的回答,我设法弄清楚了问题所在。
因此,您要附加到 email.blade.php
视图的代码将是这样的(请注意大括号前缺少的 spaces/tabulation):
@if (isset($content))
<hr>
{!! $content !!}
<hr>
@endif
如果只是想给模板添加一点基本样式,可以在line()
方法中使用Markdown,无需修改任何其他代码。
截至 2022 年 5 月,HtmlString
class 运行良好。
我用 Laravel 7、8.
完成了这个
试试这个,它应该有效
->line(new HtmlString("<b>This is bold HTML text</b>"))
确保在顶部导入它
use Illuminate\Support\HtmlString;
我正在使用默认 notification system (Laravel 5.3) 发送电子邮件。我想在消息中添加 HTML 标签。这不起作用(它以纯文本显示强标签):
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Info')
->line("Hello <strong>World</strong>")
->action('Voir le reporting', config('app.url'));
}
我知道这很正常,因为邮件通知模板中的 {{ $text }}
中显示了文本。我尝试使用与 csrf_field()
助手相同的系统:
->line( new \Illuminate\Support\HtmlString('Hello <strong>World</strong>') )
但它不起作用:它显示为纯文本。
我可以在不更改视图的情况下发送 HTML 标签吗?(我不想更改视图:在所有其他情况下都可以保护文本)。希望它足够清楚,如果不是很抱歉。
运行 php artisan vendor:publish
命令将从 vendor
目录复制 email.blade.php
到 resources/views/vendor/notifications
。
打开此视图并在两个地方将 {{ $line }}
更改为 {!! $line !!}
。在 Laravel 5.3 中,这些是视图中的 101
和 137
行。
这将显示 unescaped line
字符串,允许您在通知电子邮件中使用 HTML 标签。
好吧,您还可以创建一个新邮件Class 扩展 MailMessage
Class.
例如,您可以在 app\Notifications
<?php
namespace App\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
class MailExtended extends MailMessage
{
/**
* The notification's 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
),您可以在 resources/views/vendor/notifications
中编辑 email.blade.php
并附加:
@if (isset($content))
<hr>
{!! $content !!}
<hr>
@endif
我们就是这样做的,而且工作起来很有魅力:D
对于像我一样遵循@eric-lagarda 方法的任何人,请记住不要像通常在视图中那样在自定义 email.blade.php
视图中标记内容,因为它会被 Laravel 的 markdown 解析器作为代码,将整个 content
HTML 包装在 <code>
HTML 标签中。这让我很头疼,但由于 this 的回答,我设法弄清楚了问题所在。
因此,您要附加到 email.blade.php
视图的代码将是这样的(请注意大括号前缺少的 spaces/tabulation):
@if (isset($content))
<hr>
{!! $content !!}
<hr>
@endif
如果只是想给模板添加一点基本样式,可以在line()
方法中使用Markdown,无需修改任何其他代码。
截至 2022 年 5 月,HtmlString
class 运行良好。
我用 Laravel 7、8.
试试这个,它应该有效
->line(new HtmlString("<b>This is bold HTML text</b>"))
确保在顶部导入它
use Illuminate\Support\HtmlString;