Laravel:如何在注册控制器中向具有不同 url/ 的多个用户正确发送通知

Laravel: How to properly send notification to multiple users with different url/ in Registration Controller

我试图在使用默认身份验证 laravel 6 注册后向用户发送通知。我尝试使用通知门面,但它为所有电子邮件提供相同的 URL。我尝试使用 ->notify,但它给了我错误。顺便说一下,我正在使用 smtp.office365.com

Expected response code 354 but got code "503", with the message "503 5.5.1 Bad sequence of commands"

注册控制器

$users = User::where('department_id', $data['department'])
    ->where(function ($query) {
        $query->where('manager_level', 'sm1')
            ->orWhere('manager_level', 'sm2')
            ->orWhere('department_level', 'dh');
    })->get();

foreach ($users as $u) {
    $u->notify(new ConfirmUser(($u->department_level == 'dh') ? $u->department_level : $u->manager_level));
}
                    }

InConfirmUser

protected $level;

/**
 * Create a new notification instance.
 */
public function __construct($level)
{
    $this->level = $level;
}

/**
 * Get the notification's delivery channels.
 */
public function via($notifiable)
{
    return ['mail'];
}

/**
 * Get the mail representation of the notification.
 */
public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject('Confirm User')
        ->greeting("Dear {$notifiable->name},")
        ->line('Please click the button below to confirm that the concerned user is your staff')
        ->action('Confirm User', url('/').$this->level)
        ->line('If you did not know the concerend staff, no further action is required.');
}

/**
 * Get the array representation of the notification.
 */
public function toArray($notifiable)
{
    return [
        //
    ];
}

要修复错误消息,请在您的配置文件中将 MAIL_ENCRYPTION 的值更改为 tls,并确保 MAIL_FROM_ADDRESS 正是您的 Office365 电子邮件地址。

告诉我们更多关于 ->department 属性 的信息。部门不可以一样吗?还是邮件里null

在您编写的代码中 $u->deparment 没有 t。所以把它改成这样:

u->notify(new ConfirmUser($u->department));

这最终可能会解决您的问题。