【发布时间】:2020-11-29 17:28:56
【问题描述】:
由于拥有不同渠道的灵活性,我正在迁移到 Laravel Notifications。我正在从标准的 Laravel Mail 功能迁移。
利用我现有的功能,我正在使用自定义 Markdown 电子邮件模板,并在我构建的各个邮件类的 build 方法中返回它们,例如...
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$domain = isset($this->emailData[0]['domain']) ? $this->emailData[0]['domain'] : '';
$domain = htmlspecialchars($domain);
return $this->markdown('emails.domains.expiry')
->subject(isset($domain) && !empty($domain) ? "Domain Expiry Alert: $domain" : 'Domain Expiry Alert');
}
这样做我可以自定义我的各种电子邮件模板。
不巧的是,我为我的通知类变体的toMail 方法尝试了这种方法,但 Laravel 通知似乎不支持降价文件?
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$domain = isset($this->emailData[0]['domain']) ? $this->emailData[0]['domain'] : '';
$domain = htmlspecialchars($domain);
return $this->markdown('emails.domains.expiry')
->subject(isset($domain) && !empty($domain) ? "Domain Expiry Alert: $domain" : 'Domain Expiry Alert');
}
相反,他们期待以下...
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
有一个用于自定义模板的部分,但这是通用模板,并没有为我提供可以传递数据的单个模板。例如,我需要将 $emailData 数组传递给我的模板。
如何让toMail 方法与我构建的 Laravel 邮件通知类一起使用?
【问题讨论】: