你如何在 Laravel 默认邮件中自定义变量?

Con*_*ech 5 php laravel laravel-5 laravel-blade laravel-mail

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

php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
Run Code Online (Sandbox Code Playgroud)

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

{{-- 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
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*ris 8

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

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

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

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

班级MailMessage延伸SimpleMessage班级。该类具有和SimpleMessage方法 ,您可以使用它们来设置问候语或称呼语。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');
}
Run Code Online (Sandbox Code Playgroud)