Laravel Markdown 邮件中的动态页眉和页脚

发布于 2025-01-14 06:09:30 字数 1136 浏览 5 评论 0原文

按照文档,我使用 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 中 我需要在相应部分中有 headertitlefootertext 的值。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2025-01-21 06:09:30

您无法使用 MailMessage 执行此操作。您需要创建一个 Mailable 类并将其传递到 NotificationtoMail() 函数中。

  1. 创建一个新的 Mailable 类。

  2. 确保您的 Mailable 类使用您的 markdown Blade 模板文件。

  3. 照常在 markdown() 函数中传递数据。

// app/Mail/NewOrderMail.php

public function build()
{
    return $this->markdown('emails.new-order', [
        'headerTitle' => 'Header title here',
        'footerText' => 'Footer text here',
    ]);
}
  1. Mailable 的 markdown Blade 模板文件中,可以将数据传递到布局文件,如下所示:
// resources/views/emails/new-order.blade.php

@component('mail::message', ['headerTitle' => $headerTitle, 'footerText' => $footerText])   
{{-- ...the rest of your email template... --}}
@endcomponent
  1. resources/views/vendor/mail/html/message 中.blade.php,将变量传递给您想要将数据传递到的每个 @component
// resources/views/vendor/mail/html/message.blade.php

@component('mail::layout')

{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url'), 'headerTitle' => $headerTitle ?? config('app.name')])
{{ $headerTitle }}
@endcomponent
@endslot

{{-- ...the rest of your layout template... --}}

{{-- Footer --}}
@slot('footer')
@component('mail::footer', ['footerText' => $footerText ?? 'Default Text'])
{{ $footerText }}
@endcomponent
@endslot

@endcomponent
  1. 中返回您的 Mailable 类toMail() 函数Notification 类:
// app/Notifications/NewOrderNotification.php

public function toMail($notifiable)
{
    return (new NewOrderMail($notifiable))->to($notifiable);
}

重要: 确保将 resources/views/vendor/mail/html/message 中的变量合并为空。 blade.php 布局文件,以确保当电子邮件模板文件未传递您在布局文件中定义的任何变量时,事情不会中断。

所有文件名仅用作示例。将它们替换为您自己的。

You cannot do this with a MailMessage. You need to create a Mailable class and pass it in the toMail() function of your Notification.

  1. Create a new Mailable class.

  2. Make it so that your Mailable class uses your markdown Blade template file.

  3. Pass the data as usual in the markdown() function.

// app/Mail/NewOrderMail.php

public function build()
{
    return $this->markdown('emails.new-order', [
        'headerTitle' => 'Header title here',
        'footerText' => 'Footer text here',
    ]);
}
  1. In your Mailable's markdown Blade template file, can pass the data to the layout file like so:
// resources/views/emails/new-order.blade.php

@component('mail::message', ['headerTitle' => $headerTitle, 'footerText' => $footerText])   
{{-- ...the rest of your email template... --}}
@endcomponent
  1. In resources/views/vendor/mail/html/message.blade.php, pass the variables to each of the @component that you want to pass the data to:
// resources/views/vendor/mail/html/message.blade.php

@component('mail::layout')

{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url'), 'headerTitle' => $headerTitle ?? config('app.name')])
{{ $headerTitle }}
@endcomponent
@endslot

{{-- ...the rest of your layout template... --}}

{{-- Footer --}}
@slot('footer')
@component('mail::footer', ['footerText' => $footerText ?? 'Default Text'])
{{ $footerText }}
@endcomponent
@endslot

@endcomponent
  1. Return your Mailable class in the toMail() function of your Notification class:
// app/Notifications/NewOrderNotification.php

public function toMail($notifiable)
{
    return (new NewOrderMail($notifiable))->to($notifiable);
}

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.

凑诗 2025-01-21 06:09:30

由于找不到更好的解决方案,我最终被迫使用会话变量,以便我可以在页眉和页脚模板中使用它们。

  1. 设置您想要在 Http/Notifications/yourNotificationTemplate.php 的页脚/页眉中使用的会话变量:

     公共函数 toMail($notifying){
         session(['headertitle' => $this->headertitle]);
         session(['footertext' => $this->footertext]);
         $from = 'no-reply.'.$this->subdomain.'@'.env('APP_DOMAIN');
         返回(新邮件消息)
         ->来自($来自)
         ->markdown('emails.neworder',
             [
                 'name'=>$this->name,
                 '订单'=> $这个->订单,
                 '电子邮件正文'=> $this->电子邮件正文,
             ]
         );
     }
    
  2. 然后您就可以在模板中的任何位置使用它。 /views/vendor/mail/thml/header.blade.php 中的 FE:

@php ($headertext = session('headertitle'))
<tr>
<td class="header">
@if (null!==(session('headertext')))
<h1>{{$headertext}}</h1>
@else
{{ $slot }}
@endif
</td>
</tr>

Not finding a better solution, I was eventually forced to use session variables so I could use them in the header and footer templates.

  1. Set session variables witch you want to use in footer/header in Http/Notifications/yourNotificationTemplate.php:

     public function toMail($notifiable){
         session(['headertitle' => $this->headertitle]);
         session(['footertext' => $this->footertext]);
         $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,
             ]
         );
     }
    
  2. Then you are able to use it anywhere in yours templates. F.E in /views/vendor/mail/thml/header.blade.php:

@php ($headertext = session('headertitle'))
<tr>
<td class="header">
@if (null!==(session('headertext')))
<h1>{{$headertext}}</h1>
@else
{{ $slot }}
@endif
</td>
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文