Laravel 通知:如何在通知的 Blade 模板中使用视图 HTML?

5
我正在使用Laravel版本5.7.20构建一个自定义的Markdown模板。该模板是从/resources/views/vendor/notifications/email.blade.php复制而来,该文件是在执行命令php artisan vendor:publish --tag=laravel-notifications后生成的。
下面代码可以用于显示HTML:
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。

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

我的mail.notification.permission文件与laravel-project/resources/views/vendor/notifications/email.blade.php完全相同。

我认为我需要使用视图而不是标记语言。但我更改了->view('mail.notification.permission')后,我得到了错误消息:No hint path defined for [mail]. (View: /Users/shiro/Sites/laravel-project/resources/views/mail/notification/permission.blade.php)

我应该复制哪个文件以便使用html而不是标记格式?

我还没有在通知中看到使用-view而不是-markdown的解决方案。或者在通知电子邮件中显示html的正确流程是什么?


你实际上想要在这里完成什么?你想让作为文本输出的HTML标签起作用吗?还是你只想使用模板文件而不是创建MailMessage对象? - Mike Harrison
我希望我的通知模板能够显示正确的HTML而不是htmlspecialchars或编码版本。正如您所看到的,HTML表格显示源代码,而粗体文本未显示,它被更改为标签。 - Shiro
2个回答

6

好的,我发现了错误。

如果想使用->view(),则Blade模板不能使用@component('mail::message'),它必须使用全新的Blade布局(像普通页面一样构建自己的视图)。然后你就不会收到No hint path defined for [mail]的错误。

Laravel Notification有很多语法和生态系统需要学习。对于那些想要简单通知的人来说,这是很棒的。但如果你想要自定义页面,我建议使用view()

我分享我的布局Blade文件,这是我从markdown发送后复制的。内容部分由您自己决定逻辑。

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('New Invoice')
            ->view( 'mail.notification.custom_invoice', ['invoice' => $this->invoice]);
    {

email_layout.blade.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; background-color: #f5f8fa; color: #74787E; height: 100%; hyphens: auto; line-height: 1.4; margin: 0; -moz-hyphens: auto; -ms-word-break: break-all; width: 100% !important; -webkit-hyphens: auto; -webkit-text-size-adjust: none; word-break: break-word;">
<style>
    @media  only screen and (max-width: 600px) {
        .inner-body {
            width: 100% !important;
        }

        .footer {
            width: 100% !important;
        }
    }

    @media  only screen and (max-width: 500px) {
        .button {
            width: 100% !important;
        }
    }
</style>

<table class="wrapper" width="100%" cellpadding="0" cellspacing="0" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; background-color: #f5f8fa; margin: 0; padding: 0; width: 100%; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%;">
    <tr>
        <td align="center" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box;">
            <table class="content" width="100%" cellpadding="0" cellspacing="0" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; margin: 0; padding: 0; width: 100%; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%;">
                <tr>
                    <td class="header" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; padding: 25px 0; text-align: center;">
                        <a href="{{ url('/') }}" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; color: #bbbfc3; font-size: 19px; font-weight: bold; text-decoration: none; text-shadow: 0 1px 0 white;">
                            {{ config('app.name', 'Laravel') }}
                        </a>
                    </td>
                </tr>


                <!-- Email Body -->
                <tr>
                    <td class="body" width="100%" cellpadding="0" cellspacing="0" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; background-color: #FFFFFF; border-bottom: 1px solid #EDEFF2; border-top: 1px solid #EDEFF2; margin: 0; padding: 0; width: 100%; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 100%;">
                        <table class="inner-body" align="center" width="570" cellpadding="0" cellspacing="0" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; background-color: #FFFFFF; margin: 0 auto; padding: 0; width: 570px; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 570px;">
                            <!-- Body content -->
                            <tr>
                                <td class="content-cell" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; padding: 35px;">
                                    @yield('content')
                                    <p style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; color: #74787E; font-size: 16px; line-height: 1.5em; margin-top: 0; text-align: left;">Regards,<br>{{ config('app.name', 'Laravel') }}</p>
                                    @yield('subcopy')
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>

                <tr>
                    <td style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box;">
                        <table class="footer" align="center" width="570" cellpadding="0" cellspacing="0" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; margin: 0 auto; padding: 0; text-align: center; width: 570px; -premailer-cellpadding: 0; -premailer-cellspacing: 0; -premailer-width: 570px;">
                            <tr>
                                <td class="content-cell" align="center" style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; padding: 35px;">
                                    <p style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; line-height: 1.5em; margin-top: 0; color: #AEAEAE; font-size: 12px; text-align: center;">© {{ date('Y') }} {{ config('app.name', 'Laravel') }}. All rights reserved.</p>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</body>
</html>


0
我也遇到了这个问题:在 Laravel 通知邮件中,HTML 标签被输出为纯文本(HTML 不显示/不起作用)。
首先:
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-notifications

Laravel 会将邮件和通知模板放置在 resources/views/vendor/mailresources/views/vendor/notifications 中。

问题出在我的完美主义上:我编辑了 HTML Blade 文件的模板/布局,并像普通的 HTML 文件一样添加了缩进。不要这样做!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接