Laravel 5.4^ - 如何自定义通知电子邮件布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42724118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4^ - How to customize notification email layout?
提问by Brian Glaz
I am trying to customize the HTML email layout that is used when sending notifications via email.
我正在尝试自定义通过电子邮件发送通知时使用的 HTML 电子邮件布局。
I have published both the mail and notification views.
我已经发布了邮件和通知视图。
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-notifications
If I modify the /resources/views/vendor/notifications/email.blade.php
file, I can only change the BODY content of the emails that get sent. I am looking to modify the footer, header, and every other part of the email layout as well.
如果我修改/resources/views/vendor/notifications/email.blade.php
文件,我只能更改发送的电子邮件的正文内容。我希望修改页脚、页眉和电子邮件布局的所有其他部分。
I tried also modifying the views inside /resources/vendor/mail/html/
, but whenever the notification gets sent, it is not even using these views and instead uses the default laravel framework ones.
我也尝试修改里面的视图/resources/vendor/mail/html/
,但是每当发送通知时,它甚至不使用这些视图,而是使用默认的 Laravel 框架视图。
I am aware I can set a view on the MailMessage
returned by my Notification class, but I want to keep the standard line()
, greeting()
, etc. functions.
我知道我可以设置在一个视图MailMessage
通过我的通知类返回,但我想保持标准line()
,greeting()
等等功能。
Does anyone know how I can get my notifications to send email using the views in /resources/vendor/mail/html
?
有谁知道如何使用 中的视图获取我的通知以发送电子邮件/resources/vendor/mail/html
?
The following is my /resources/views/vendor/notifications/email.blade.php
file, but it does not have anywhere to customize the header/footer/ overall layout.
以下是我的/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
回答by lewis4u
Run this command
运行这个命令
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
update for laravel 5.7+
Laravel 5.7+ 更新
php artisan vendor:publish
and then you will get:
然后你会得到:
[<number>] Tag: laravel-mail
[<number>] Tag: laravel-notifications
and then just type in that number in front to publish the file for editing
然后只需在前面输入该数字即可发布文件进行编辑
and then in
然后在
/resources/views/vendor/mail/html/
you can edit all the components and customize anything you want. For example i have edited the sentence "All rights reserved". to "All test reserved" at the bottom of that image inside this file:
您可以编辑所有组件并自定义您想要的任何内容。例如,我编辑了“保留所有权利”这句话。到此文件中该图像底部的“保留所有测试”:
/resources/views/vendor/mail/html/message.blade.php
and this is what i got:
这就是我得到的:
回答by Yo1
Make sure to have the right configuration in your config/mail.php :
确保在 config/mail.php 中有正确的配置:
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
]
],
回答by shabany
I wrote an article on how to create a notification and modify your template including the header and footer.
我写了一篇关于如何创建通知和修改模板(包括页眉和页脚)的文章。
It includes the explanation on how the Laravel components work and how to pass your data to a new email template.
它包括对 Laravel 组件如何工作以及如何将数据传递到新电子邮件模板的解释。
The most important part is placing the following code inside your email template:
最重要的部分是将以下代码放入您的电子邮件模板中:
@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
You can check the medium article in case you want more details on how the components work and how to properly pass the data.
如果您想了解有关组件如何工作以及如何正确传递数据的更多详细信息,可以查看媒体文章。
回答by Nam Dau
@Brian You can just make change to the @component directives in your template file to use your custom templates. For example:
@Brian 您只需更改模板文件中的 @component 指令即可使用自定义模板。例如:
Replace @component('mail::message')
with @component('vendor.mail.html.message')
, assuming your template is located at /resources/views/vendor/mail/html/message.blade.php
替换@component('mail::message')
为@component('vendor.mail.html.message')
,假设您的模板位于/resources/views/vendor/mail/html/message.blade.php
回答by Brian Glaz
I ended up just using a custom view rather than trying to get the built in Laravel ones to work.
我最终只是使用自定义视图,而不是尝试让内置的 Laravel 视图工作。
I added the following use
statement to my Notification class
我在use
Notification 类中添加了以下语句
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
Then in the toMail
method:
然后在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
is my actual email template.
emails.teamInvitation
是我实际的电子邮件模板。
I compile the view in to a string, and then convert the stylesheets to be inline.
我将视图编译为字符串,然后将样式表转换为内联样式。
emails.htmlBlank
is a view file but all it does is echo out bodyContent
. This is necessary because the MailMessage->view
method expects a view file, and not an HtmlString.
emails.htmlBlank
是一个视图文件,但它所做的只是 echo out bodyContent
。这是必要的,因为该MailMessage->view
方法需要一个视图文件,而不是一个 HtmlString。
回答by Brenden Clerget
Do NOT do what is suggested here.
不要做这里建议的事情。
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
这有效。请记住,您应该编辑“vendor/mail/html”文件夹中包含的模板,而不是“vendor/mail/markdown”文件夹中的内容,除非您使用的是 markdown 而不是 line() / greeting( ) 电子邮件构建功能
Instead, run the artisan commands and then edit the generated files in your resources folder that you end up with. Never overwrite the vendor files, as if you are working on a local version, then push it to a live server and run composer install, you will not have those changes anymore.
相反,运行 artisan 命令,然后在您最终得到的资源文件夹中编辑生成的文件。永远不要覆盖供应商文件,就像您正在处理本地版本一样,然后将其推送到实时服务器并运行 composer install,您将不再有这些更改。
Laravel's inheritance allows you to easily overwrite pre-defined methods and files, so take advantage of that for cleaner version control and better ability to roll back changes to core functionality.
Laravel 的继承允许您轻松覆盖预定义的方法和文件,因此请利用它进行更清晰的版本控制和更好的回滚核心功能更改的能力。
回答by Lívia Nascimento
Laravel 5.8
Laravel 5.8
I found email layout in file -> vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/layout.blade.php.
我在文件 -> vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/layout.blade.php 中找到了电子邮件布局。
Like I don't use markdown to send my emails, i need of layout default of laravel (yes, because i want :)).
就像我不使用 Markdown 发送电子邮件一样,我需要 Laravel 的默认布局(是的,因为我想要 :))。
What i did? I sent for me email for me of reset password, saved the email like html and then copied html to my editor and it ready to changes \o/.
我做了什么?我为我发送了重置密码的电子邮件,将电子邮件保存为 html,然后将 html 复制到我的编辑器,并准备更改 \o/。
回答by Yevgeniy Afanasyev
You are making email based on component @component('mail::message')
This is a default and this is only one described in documentation. This component does not allow you to modify header. However if you look into it's file,
您正在基于组件制作电子邮件@component('mail::message')
这是默认设置,这只是文档中描述的一种。此组件不允许您修改标题。但是,如果您查看它的文件,
\vendor\laravel\framework\src\Illuminate\Mail\resources\views\markdown\message.blade.php
you will see that it uses another component @component('mail::layout')
,
你会看到它使用了另一个组件@component('mail::layout')
,
Just copy content of message.blade.php
file into your .blade.php
and replace {{ $slot }}
with what you had in your file before.
只需将message.blade.php
文件内容复制到您的文件中.blade.php
并替换{{ $slot }}
为您之前文件中的内容即可。
And now you have all the flexibility in your file.
现在,您的文件具有所有灵活性。
Plus
加
if you want to modify styles, go to file \config\mail.php
如果要修改样式,请转到文件 \config\mail.php
and change markdown
section like so
并markdown
像这样改变部分
'markdown' => [
'theme' => 'default0',
'paths' => [
resource_path('views/vendor/mail'),
base_path('resources/views/emails/vendor'),
],
],
In this case I replaced default theme with my own \resources\views\emails\vendor\html\themes\default0.css
在这种情况下,我用我自己的替换了默认主题 \resources\views\emails\vendor\html\themes\default0.css
or, if you don't want customising paths - put your default0.css
into /resources/views/vendor/mail/html/themes
- it is a default path and you don't need to mention it.
或者,如果您不想自定义路径 - 将您的路径default0.css
放入/resources/views/vendor/mail/html/themes
- 它是默认路径,您无需提及它。
Tested on Laravel 5.7
在 Laravel 5.7 上测试