Search code examples
laravellaravel-6laravel-authorizationlaravel-authentication

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


I am trying to send notifications to the user after registration with the default auth of laravel 6. I tried using the notification facade, but it gives the same URL for all emails. I tried using the ->notify, but it gives me the error. By the way, I am using smtp.office365.com

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

RegisterController

$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 [
        //
    ];
}

Solution

  • To fix the error message change the value of MAIL_ENCRYPTION to tls in your configuration file and also make sure the MAIL_FROM_ADDRESS is exactly your Office365 email adress.

    Tell us more about the ->department property. Isn't it possible that the department can be the same? Or is it null in the mail?

    In the code you've wrote $u->deparment without a t. So change it to this:

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

    This could eventually solve your problem.