You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9发送邮件通知报错:缺少To/Cc/Bcc邮件头

问题原因及解决方案

问题出在你的ConsumableNotification类的toMail方法中:你直接返回了一个ConsumableAlert(Mailable类),但Laravel的通知系统不会自动为这个Mailable设置收件人信息。

当你使用Mail::to($user)->send(...)时,手动通过to()方法指定了收件人,所以邮件能正常发送;但在通知系统中,返回的Mailable没有被设置收件人,导致Symfony抛出"缺少To/Cc/Bcc头"的错误。

修复方法

修改toMail方法,为返回的ConsumableAlert添加收件人:

public function toMail($notifiable)
{
    return (new ConsumableAlert(
        $this->consumableType,
        $this->remains,
        $this->expires
    ))->to($notifiable->routeNotificationForMail());
}

或者直接使用用户模型的邮箱字段(如果你的User模型有email属性):

public function toMail($notifiable)
{
    return (new ConsumableAlert(
        $this->consumableType,
        $this->remains,
        $this->expires
    ))->to($notifiable->email);
}

补充说明

Laravel通知的toMail方法通常返回MailMessage实例,这是通知系统推荐的方式,它会自动处理收件人信息(基于Notifiable模型的routeNotificationForMail方法)。如果你想复用已有的Mailable类,就必须手动指定收件人,因为通知系统不会自动为Mailable注入收件人。

内容的提问来源于stack exchange,提问作者SenneVP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 10:04:52