【问题标题】:Laravel 8 Notifications markdown email templateLaravel 8 通知降价电子邮件模板
【发布时间】: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 邮件通知类一起使用?

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    您需要从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

    【讨论】:

    • 谢谢!完全错过了文档的那一部分!
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2017-10-11
    • 2021-11-05
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多