gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:39:56 24 4
gpt4 key购买 nike

我关注了 this answer在我的应用程序中发布默认电子邮件模板:

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 部分的“问候”,但显然有一种方法可以使用变量为电子邮件模板设置这些默认值。我如何设置 $greeting$salutation发送电子邮件时的变量?

最佳答案

您发布的模板是 notification mails 的默认模板.
创建此类通知时,例如:
php artisan make:notification InvoicePaid --markdown=mail.invoice.paid
app/Notifications/InvoicePaid.php 创建了一个新的 InvoicePaid 类.
这个类包含一个 toMail()方法内容如下:
return (new MailMessage)->markdown('mail.invoice.paid');MailMessage类扩展了 SimpleMessage类(class)。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');
}

关于php - 你如何在 Laravel 默认邮件中自定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53771024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com