Laravel Markdown 邮件中的动态页眉和页脚
按照文档,我使用 markdown mailables 创建了自己的邮件模板: https ://laravel.com/docs/9.x/mail#generating-markdown-mailables
要点是我需要动态自定义页眉和页脚中的信息(在每种情况下都会有所不同)。我在 toMail 函数中传递的信息仅在我的自定义模板 neworder.blade.php 的范围内可用:
public function toMail($notifiable){
$from = 'no-reply.'.$this->subdomain.'@'.env('APP_DOMAIN');
return (new MailMessage)
->from($from)
->markdown('emails.neworder',
[
'name'=>$this->name,
'order'=> $this->order,
'emailbody'=> $this->emailbody,
'headertitle' => $this->headertitle,
'footertext' => $this->footertext
]
);
}
按照相同的文档,我导出了 Markdown 邮件组件以使用此命令自定义它们:
php artisan vendor:publish --tag=laravel-mail
从这里我可以自定义文件,例如/vendor/mail/html/themes/header.blade.php,其中的修改有效地影响标头。我不明白的是如何传递我可以在这些文件范围内使用的变量,就像在 /views/email/neworder.blade.php 中 我需要在相应部分中有 headertitle
和 footertext
的值。
Following the documentation, I have created my own mail template using markdown mailables: https://laravel.com/docs/9.x/mail#generating-markdown-mailables
The point is that I need to customize information in both the header and footer dynamically (in each situation it will be different). The information I pass in the toMail function is only available in the scope of my custom template neworder.blade.php:
public function toMail($notifiable){
$from = 'no-reply.'.$this->subdomain.'@'.env('APP_DOMAIN');
return (new MailMessage)
->from($from)
->markdown('emails.neworder',
[
'name'=>$this->name,
'order'=> $this->order,
'emailbody'=> $this->emailbody,
'headertitle' => $this->headertitle,
'footertext' => $this->footertext
]
);
}
Following the same documentation, I have exported the Markdown mail components to customize them with this command:
php artisan vendor:publish --tag=laravel-mail
From here I can customize files like /vendor/mail/html/themes/header.blade.php, where the modifications effectively affect the header. What I can't figure out is how to pass variables that I can use in the scope of these files just like in /views/email/neworder.blade.php
I need to have the values of headertitle
and footertext
in the corresponding sections.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法使用
MailMessage
执行此操作。您需要创建一个Mailable
类并将其传递到Notification
的toMail()
函数中。创建一个新的
Mailable
类。确保您的
Mailable
类使用您的 markdownBlade
模板文件。照常在
markdown()
函数中传递数据。Mailable
的 markdownBlade
模板文件中,可以将数据传递到布局文件,如下所示:resources/views/vendor/mail/html/message 中.blade.php
,将变量传递给您想要将数据传递到的每个@component
:中返回您的
函数Mailable
类toMail()Notification
类:重要: 确保将
resources/views/vendor/mail/html/message 中的变量合并为空。 blade.php
布局文件,以确保当电子邮件模板文件未传递您在布局文件中定义的任何变量时,事情不会中断。所有文件名仅用作示例。将它们替换为您自己的。
You cannot do this with a
MailMessage
. You need to create aMailable
class and pass it in thetoMail()
function of yourNotification
.Create a new
Mailable
class.Make it so that your
Mailable
class uses your markdownBlade
template file.Pass the data as usual in the
markdown()
function.Mailable
's markdownBlade
template file, can pass the data to the layout file like so:resources/views/vendor/mail/html/message.blade.php
, pass the variables to each of the@component
that you want to pass the data to:Mailable
class in thetoMail()
function of yourNotification
class:IMPORTANT: Make sure to null coalesce your variables in your
resources/views/vendor/mail/html/message.blade.php
layout file to make sure things don't break when an email template file does not pass any of the variables you defined in the layout file.All filenames are only used as examples. Replace them with your own.
由于找不到更好的解决方案,我最终被迫使用会话变量,以便我可以在页眉和页脚模板中使用它们。
设置您想要在 Http/Notifications/yourNotificationTemplate.php 的页脚/页眉中使用的会话变量:
然后您就可以在模板中的任何位置使用它。 /views/vendor/mail/thml/header.blade.php 中的 FE:
Not finding a better solution, I was eventually forced to use session variables so I could use them in the header and footer templates.
Set session variables witch you want to use in footer/header in Http/Notifications/yourNotificationTemplate.php:
Then you are able to use it anywhere in yours templates. F.E in /views/vendor/mail/thml/header.blade.php: