Laravel 8 通知降价电子邮件模板

Laravel 8 Notifications markdown email template

我正在迁移到 Laravel Notifications because of the flexibility of having different channels. I'm migrating from the standard Laravel Mail 功能。

利用我现有的功能,我正在使用自定义降价电子邮件模板,并将它们返回到我构建的单个邮件 classes 的 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');
}

这样做我可以自定义我的各种电子邮件模板。

不幸的是,我在我的通知 class 变体的 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 邮件通知 classes 一起工作?

您需要 return 来自 toMail 函数的 MailMessage。要从您的通知中发送降价电子邮件,请按如下方式修改您的 toMail 函数:

public function toMail($notifiable)
{
  $domain = isset($this->emailData[0]['domain']) ? $this->emailData[0]['domain'] : '';
  $domain = htmlspecialchars($domain);

  return (new MailMessage)
          ->subject(isset($domain) && !empty($domain) ? "Domain Expiry Alert: $domain" : 'Domain Expiry Alert')
          ->markdown('emails.domains.expiry');
}

参考:Laravel Documentation