You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel中实现邮件通知页眉与页脚的动态化?

如何在Laravel邮件通知中自定义动态页眉和页脚

当然可以实现!Laravel其实提供了灵活的方式让你给每个租户自定义邮件的页眉、页脚,同时传递动态变量到这些区域。下面我给你详细讲解两种常用的解决方案:

方法一:修改默认的通知模板(保留MailMessage的便捷方法)

Laravel的邮件通知默认使用框架内置模板,你可以先把这些模板发布到本地,再修改页眉和页脚部分,同时传递自定义变量。

步骤1:发布默认通知模板

首先运行Artisan命令,把Laravel的通知模板发布到项目中:

php artisan vendor:publish --tag=laravel-notifications

执行后,你会在resources/views/vendor/notifications目录下看到header.blade.php、footer.blade.php和email.blade.php等Blade模板文件。

步骤2:在通知类中传递额外变量

在你的toMail方法里,使用with()方法把租户相关的动态数据传递给模板:

/**
 * Get the mail representation of the notification.
 *
 * @param mixed $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    $url = url('/invoice/'.$this->invoice->id);
    // 假设你的Notifiable模型关联了Tenant,这里获取当前租户信息
    $tenant = $notifiable->tenant; 

    return (new MailMessage)
        ->greeting('Hello!')
        ->line('One of your invoices has been paid!')
        ->action('View Invoice', $url)
        ->line('Thank you for using our application!')
        // 传递租户相关变量到模板
        ->with([
            'tenantName' => $tenant->name,
            'tenantLogo' => $tenant->logo_url,
        ]);
}

步骤3:修改页眉模板

打开resources/views/vendor/notifications/header.blade.php,直接使用你传递的变量自定义页眉:

@if(isset($tenantLogo))
    <img src="{{ $tenantLogo }}" alt="{{ $tenantName }} Logo" style="max-height: 50px; margin-bottom: 15px;">
@else
    <!-- 回退到默认应用名称 -->
    <h1 style="margin: 0; color: #2d3748;">{{ config('app.name') }}</h1>
@endif

同样的,你也可以修改footer.blade.php,加入租户的联系方式等动态内容。

方法二:使用完全自定义的邮件视图(更灵活的控制)

如果想要完全掌控邮件的所有布局(包括页眉、正文、页脚),可以创建自己的邮件视图,不用依赖默认的MailMessage结构。

步骤1:创建自定义邮件视图

在resources/views/notifications目录下创建新的Blade模板,比如invoice-paid.blade.php,编写完整的邮件HTML:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Invoice Paid Notification</title>
    <style>
        /* 自定义邮件样式 */
        .header { padding: 20px; background: #f7fafc; text-align: center; }
        .content { padding: 25px; max-width: 600px; margin: 0 auto; }
        .footer { padding: 20px; background: #f7fafc; text-align: center; color: #718096; }
    </style>
</head>
<body>
    <!-- 自定义页眉 -->
    <div class="header">
        <img src="{{ $tenantLogo }}" alt="{{ $tenantName }} Logo" style="max-height: 60px;">
    </div>

    <!-- 邮件正文 -->
    <div class="content">
        <p>Hello!</p>
        <p>One of your invoices ({{ $invoice->number }}) has been paid!</p>
        <p><a href="{{ $url }}" style="background: #3182ce; color: white; padding: 10px 20px; border-radius: 4px; text-decoration: none;">View Invoice</a></p>
        <p>Thank you for using our application!</p>
    </div>

    <!-- 自定义页脚 -->
    <div class="footer">
        <p>© {{ date('Y') }} {{ $tenantName }}. All rights reserved.</p>
        <p>Contact us at {{ $tenant->email }}</p>
    </div>
</body>
</html>

步骤2:在通知类中使用自定义视图

修改toMail方法,用view()指定你的自定义视图,并传递所需变量:

/**
 * Get the mail representation of the notification.
 *
 * @param mixed $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    $url = url('/invoice/'.$this->invoice->id);
    $tenant = $notifiable->tenant;

    return (new MailMessage)
        ->view('notifications.invoice-paid', [
            'invoice' => $this->invoice,
            'url' => $url,
            'tenantName' => $tenant->name,
            'tenantLogo' => $tenant->logo_url,
        ]);
}

关键注意点

  • 不管用哪种方法,with()方法传递的变量都会自动注入到所有相关的Blade视图中(包括页眉、页脚和正文模板)。
  • 如果你的租户数据不是关联在Notifiable模型上,可以根据实际业务逻辑获取(比如从请求上下文、当前租户中间件中获取)。

内容的提问来源于stack exchange,提问作者Lasse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:15:19