您如何自定义 Laravel 默认电子邮件中的变量?

How do you customize variables in Laravel default emails?

我按照 在我的应用程序中发布了默认电子邮件模板:

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

效果很好,但显然有一些配置选项,例如:

{{-- Greeting --}}
@if (! empty($greeting))
# {{ $greeting }}
@else
@if ($level === 'error')
# @lang('Whoops!')
@else
# @lang('Hello!')
@endif
@endif

{{-- Salutation --}}
@if (! empty($salutation))
{{ $salutation }}
@else
@lang('Regards'),<br>{{ config('app.name') }}
@endif

现在我的电子邮件正在从 else 部分发送 "Hello!" 和 "Regards",但显然有一种方法可以使用变量为电子邮件模板设置这些默认值。发送电子邮件时如何设置 $greeting$salutation 变量?

您发布的模板是 notification mails 的默认模板。 例如创建此类通知时:

php artisan make:notification InvoicePaid --markdown=mail.invoice.paid

app/Notifications/InvoicePaid.php 创建了一个新的 InvoicePaid class。 此 class 包含具有以下内容的 toMail() 方法:

return (new MailMessage)->markdown('mail.invoice.paid');

MailMessage class 扩展了 SimpleMessage class。 SimpleMessage class 有方法 greeting()salutation() 可以用来设置问候语或称呼。

例如:

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
           ->greeting("Your custom greeting")
           ->salutation("Your salutation goes here")
           ->markdown('mail.invoice.paid');
}