【问题标题】:Laravel send email to (notify) user using another email?Laravel 使用另一封电子邮件向(通知)用户发送电子邮件?
【发布时间】:2020-01-26 23:35:12
【问题描述】:

一些用户有另一个电子邮件地址,该地址存储为users 表中的secondary_email 列。

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

【问题讨论】:

    标签: php laravel laravel-6


    【解决方案1】:

    您将覆盖通知的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

    【讨论】:

      【解决方案2】:

      您可以在通知实体模型上定义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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多