我正在尝试自定义通过电子邮件发送通知时使用的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框架。
我知道我可以在我的Notification类返回的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
运行此命令
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
更新laravel 5.7+
php artisan vendor:publish
然后你会得到:
[<number>] Tag: laravel-mail
[<number>] Tag: laravel-notifications
然后只需在前面键入该数字即可发布文件进行编辑
然后在
/resources/views/vendor/mail/html/
您可以编辑所有组件并自定义您想要的任何内容。例如,我编辑了句子“保留所有权利”。在该文件中该图像底部的“所有测试保留”:
/resources/views/vendor/mail/html/message.blade.php
这就是我得到的:
确保在config / mail.php中具有正确的配置:
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
]
],
我写了一篇关于如何创建通知和修改模板的文章,包括页眉和页脚。
它包括有关Laravel组件如何工作以及如何将数据传递到新电子邮件模板的说明。
最重要的部分是将以下代码放在您的电子邮件模板中:
@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
如果您需要有关组件如何工作以及如何正确传递数据的更多详细信息,可以查看中型文章。
@Brian您只需更改模板文件中的@component指令即可使用自定义模板。例如:
假设您的模板位于@component('mail::message')
,将@component('vendor.mail.html.message')
替换为/resources/views/vendor/mail/html/message.blade.php
我最终只是使用自定义视图,而不是试图使内置的Laravel工作。
我在我的Notification类中添加了以下use
语句
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
然后在toMail
方法:
public function toMail($notifiable)
{
$view_file = 'emails.teamInvitation';
$view = View::make($view_file, ['sender' => $this->sender, 'invitationToken' => $this->invitationToken, 'team' => $this->team ]);
$view = new HtmlString(with(new CssToInlineStyles)->convert($view));
return (new MailMessage)
->subject('PreSource Invitation From ' . $this->sender->name )
->view('emails.htmlBlank', ['bodyContent' => $view]);
}
emails.teamInvitation
是我的实际电子邮件模板。
我将视图编译为一个字符串,然后将样式表转换为内联。
emails.htmlBlank
是一个视图文件,但它所做的只是回应bodyContent
。这是必要的,因为MailMessage->view
方法需要一个视图文件,而不是HtmlString。
不要做这里建议的事情。
这有效。请记住,您应该编辑'vendor / mail / html'文件夹中包含的模板而不是'vendor / mail / markdown'文件夹中的内容,除非您使用markdown而不是line()/ greeting(电子邮件构建功能
而是运行artisan命令,然后编辑您最终使用的资源文件夹中生成的文件。永远不会覆盖供应商文件,就好像您正在使用本地版本,然后将其推送到实时服务器并运行composer install,您将不再拥有这些更改。
Laravel的继承允许您轻松覆盖预定义的方法和文件,因此可以利用它来实现更清晰的版本控制,并更好地回滚对核心功能的更改。
您正在根据组件@component('mail::message')
制作电子邮件这是默认设置,这只是文档中描述的一个。此组件不允许您修改标头。但是,如果你查看它的文件,
\vendor\laravel\framework\src\Illuminate\Mail\resources\views\markdown\message.blade.php
你会看到它使用另一个组件@component('mail::layout')
,
只需将message.blade.php
文件的内容复制到您的.blade.php
中,并将{{ $slot }}
替换为您之前在文件中的内容。
现在,您可以在文件中拥有所有灵活性。
更
如果你想修改样式,请转到文件\config\mail.php
并改变markdown
部分
'markdown' => [
'theme' => 'default0',
'paths' => [
resource_path('views/vendor/mail'),
base_path('resources/views/emails/vendor'),
],
],
在这种情况下,我用我自己的\resources\views\emails\vendor\html\themes\default0.css
替换了默认主题
或者,如果你不想自定义路径 - 将你的default0.css
放入/resources/views/vendor/mail/html/themes
- 这是一个默认路径,你不需要提及它。
在Laravel 5.7上测试过