Laravel 使用另一封电子邮件向(通知)用户发送电子邮件?

Laravel send email to (notify) user using another email?

一些用户有另一个电子邮件地址存储在 users table 中的 secondary_email 列。

如何使用 $user->notify() 向这个辅助电子邮件地址发送通知?

您将覆盖通知的 toMail 方法。例如:

use App\Mail\InvoicePaid as Mailable;

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return Mailable
 */
public function toMail($notifiable)
{
    return (new Mailable($this->invoice))
           ->to($this->user->secondary_email ?? $this->user->email);
}

您可以阅读有关格式化通知的更多信息here

您可以在通知实体模型上定义一个routeNotificationForMail方法

  public function routeNotificationForMail($notification)
    {
        // Return email address only...
        return $this->email_address;

        // Return email address and name...
        return [$this->email_address => $this->name];
    }

然后修改函数以满足您的需要。 laravel documentation notifications