Laravel 5.4^ - 如何自定义通知邮件布局?
Laravel 5.4^ - How to customize notification email layout?
我正在尝试自定义 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 框架视图。
我知道我可以在我的通知 class 返回的 MailMessage
上设置视图,但我想保持标准 line()
、greeting()
等. 函数.
有谁知道如何使用 /resources/vendor/mail/html
中的视图获取发送电子邮件的通知?
下面是我的/resources/views/vendor/notifications/email.blade.php
文件,但是没有地方可以自定义header/footer/整体布局。
@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
update for laravel 5.7+
php artisan vendor:publish
然后你会得到:
[<number>] Tag: laravel-mail
[<number>] Tag: laravel-notifications
然后只需在前面输入该数字即可发布文件进行编辑
然后在
/resources/views/vendor/mail/html/
您可以编辑所有组件并自定义您想要的任何内容。
例如,我编辑了句子 "All rights reserved"。到此文件中该图像底部的 "All test reserved":
/resources/views/vendor/mail/html/message.blade.php
这就是我得到的:
我最终只使用了自定义视图,而不是试图让内置的 Laravel 视图工作。
我在通知中添加了以下 use
声明 class
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。
@布莱恩
您只需更改模板文件中的 @component 指令即可使用自定义模板。例如:
将 @component('mail::message')
替换为 @component('vendor.mail.html.message')
,假设您的模板位于 /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
如果您想了解有关组件如何工作以及如何正确传递数据的更多详细信息,可以查看媒体文章。
您正在根据组件制作电子邮件 @component('mail::message')
这是默认设置,这只是文档中描述的一个。此组件不允许您修改 header。但是,如果您查看它的文件,
\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
不要执行此处建议的操作。
This works. Just remember that you should edit the templates contained in the 'vendor/mail/html' folder AND NOT the contents of the 'vendor/mail/markdown' folder, unless of course you are using markdown instead of the line() / greeting() email building functions
相反,运行 artisan 命令,然后在资源文件夹中编辑最终生成的文件。永远不要覆盖供应商文件,就好像您正在使用本地版本一样,然后将其推送到实时服务器并 运行 composer install,您将不再有这些更改。
Laravel 的继承允许您轻松覆盖 pre-defined 方法和文件,因此利用它来实现更清晰的版本控制和更好的回滚核心功能更改的能力。
Laravel 5.8
我在文件中找到电子邮件布局 -> vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/layout.blade.php.
就像我不使用 markdown 发送电子邮件一样,我需要布局默认值 laravel(是的,因为我想要 :))。
我做了什么?我为我发送了重置密码的电子邮件,将电子邮件保存为 html,然后将 html 复制到我的编辑器,它已准备好更改 \o/。
我正在尝试自定义 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 框架视图。
我知道我可以在我的通知 class 返回的 MailMessage
上设置视图,但我想保持标准 line()
、greeting()
等. 函数.
有谁知道如何使用 /resources/vendor/mail/html
中的视图获取发送电子邮件的通知?
下面是我的/resources/views/vendor/notifications/email.blade.php
文件,但是没有地方可以自定义header/footer/整体布局。
@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
update for laravel 5.7+
php artisan vendor:publish
然后你会得到:
[<number>] Tag: laravel-mail
[<number>] Tag: laravel-notifications
然后只需在前面输入该数字即可发布文件进行编辑
然后在
/resources/views/vendor/mail/html/
您可以编辑所有组件并自定义您想要的任何内容。 例如,我编辑了句子 "All rights reserved"。到此文件中该图像底部的 "All test reserved":
/resources/views/vendor/mail/html/message.blade.php
这就是我得到的:
我最终只使用了自定义视图,而不是试图让内置的 Laravel 视图工作。
我在通知中添加了以下 use
声明 class
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。
@布莱恩 您只需更改模板文件中的 @component 指令即可使用自定义模板。例如:
将 @component('mail::message')
替换为 @component('vendor.mail.html.message')
,假设您的模板位于 /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
如果您想了解有关组件如何工作以及如何正确传递数据的更多详细信息,可以查看媒体文章。
您正在根据组件制作电子邮件 @component('mail::message')
这是默认设置,这只是文档中描述的一个。此组件不允许您修改 header。但是,如果您查看它的文件,
\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
不要执行此处建议的操作。
This works. Just remember that you should edit the templates contained in the 'vendor/mail/html' folder AND NOT the contents of the 'vendor/mail/markdown' folder, unless of course you are using markdown instead of the line() / greeting() email building functions
相反,运行 artisan 命令,然后在资源文件夹中编辑最终生成的文件。永远不要覆盖供应商文件,就好像您正在使用本地版本一样,然后将其推送到实时服务器并 运行 composer install,您将不再有这些更改。
Laravel 的继承允许您轻松覆盖 pre-defined 方法和文件,因此利用它来实现更清晰的版本控制和更好的回滚核心功能更改的能力。
Laravel 5.8
我在文件中找到电子邮件布局 -> vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/layout.blade.php.
就像我不使用 markdown 发送电子邮件一样,我需要布局默认值 laravel(是的,因为我想要 :))。
我做了什么?我为我发送了重置密码的电子邮件,将电子邮件保存为 html,然后将 html 复制到我的编辑器,它已准备好更改 \o/。