Laravel 5.4 - 如何自定义通知电子邮件布局?

Bri*_*laz 20 laravel laravel-5 laravel-5.4

我正在尝试自定义通过电子邮件发送通知时使用的HTML电子邮件布局.

我已经发布了邮件和通知视图.

php artisan vendor:publish --tag=laravel-mail

php artisan vendor:publish --tag=laravel-notifications

如果我修改/resources/views/vendor/notifications/email.blade.php文件,我只能更改发送的电子邮件的BODY内容.我也希望修改页脚,标题和电子邮件布局的其他所有部分.

我也尝试修改内部视图/resources/vendor/mail/html/,但每当发送通知时,它甚至不使用这些视图,而是使用默认的laravel框架.

我知道我可以设置在一个视图MailMessage通过我的通知类返回,但我想保持标准line(),greeting()等等功能.

有谁知道我如何通过视图发送电子邮件的通知/resources/vendor/mail/html

在此输入图像描述

以下是我的/resources/views/vendor/notifications/email.blade.php文件,但它没有任何地方可以自定义页眉/页脚/整体布局.

@component('mail::message')
{{-- Greeting --}}
@if (! empty($greeting))
# {{ $greeting }}
@else
@if ($level == 'error')
# Whoops!
@else
# Hello!
@endif
@endif

{{-- Intro Lines --}}
@foreach ($introLines as $line)
{{ $line }}

@endforeach

{{-- Action Button --}}
@if (isset($actionText))
<?php
    switch ($level) {
        case 'success':
            $color = 'green';
            break;
        case 'error':
            $color = 'red';
            break;
        default:
            $color = 'blue';
    }
?>
@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent
@endif

{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{{ $line }}

@endforeach

<!-- Salutation -->
@if (! empty($salutation))
{{ $salutation }}
@else
Regards,<br>{{ config('app.name') }}
@endif

<!-- Subcopy -->
@if (isset($actionText))
@component('mail::subcopy')
If you’re having trouble clicking the "{{ $actionText }}" button, copy and paste the URL below
into your web browser: [{{ $actionUrl }}]({{ $actionUrl }})
@endcomponent
@endif
@endcomponent
Run Code Online (Sandbox Code Playgroud)

lew*_*s4u 37

运行此命令

php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
Run Code Online (Sandbox Code Playgroud)

更新laravel 5.7+

php artisan vendor:publish
Run Code Online (Sandbox Code Playgroud)

然后你会得到:

  [<number>] Tag: laravel-mail
  [<number>] Tag: laravel-notifications
Run Code Online (Sandbox Code Playgroud)

然后只需在前面输入该数字即可发布文件进行编辑


然后在

/resources/views/vendor/mail/html/
Run Code Online (Sandbox Code Playgroud)

您可以编辑所有组件并自定义所需的任何内容.例如,我编辑了句子"保留所有权利".在该文件中该图像底部的"所有测试保留":

/resources/views/vendor/mail/html/message.blade.php
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

在此输入图像描述


小智 13

确保在config/mail.php中具有正确的配置:

'markdown' => [
    'theme' => 'default',
    'paths' => [
        resource_path('views/vendor/mail'),
    ]
],
Run Code Online (Sandbox Code Playgroud)


sha*_*any 9

我写了一篇关于如何创建通知和修改模板的文章,包括页眉和页脚.

它包括有关Laravel组件如何工作以及如何将数据传递到新电子邮件模板的说明.

https://medium.com/@adnanxteam/how-to-customize-laravel-5-4-notification-email-templates-header-and-footer-158b1c7cc1c

最重要的部分是将以下代码放在您的电子邮件模板中:

@component('mail::layout')
    {{-- Header --}}
    @slot('header')
        @component('mail::header', ['url' => config('app.url')])
            Header Title
        @endcomponent
    @endslot
{{-- Body --}}
    This is our main message {{ $user }}
{{-- Subcopy --}}
    @isset($subcopy)
        @slot('subcopy')
            @component('mail::subcopy')
                {{ $subcopy }}
            @endcomponent
        @endslot
    @endisset
{{-- Footer --}}
    @slot('footer')
        @component('mail::footer')
            © {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
        @endcomponent
    @endslot
@endcomponent
Run Code Online (Sandbox Code Playgroud)

如果您需要有关组件如何工作以及如何正确传递数据的更多详细信息,可以查看中型文章.


小智 7

@Brian 您只需更改模板文件中的 @component 指令即可使用自定义模板。例如:

替换@component('mail::message')@component('vendor.mail.html.message'),假设您的模板位于/resources/views/vendor/mail/html/message.blade.php