Laravel 9发送电子邮件通知错误“电子邮件必须有“To”、“Cc”或“Bcc”头,”

lsmepo6l  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(170)

我正在尝试向一组用户发送通知。根据Laravel文档,我应该能够发送通知如下:

  1. Notification::send($users, new ConsumableNotification($arguments));

字符串
只要$users是可通知对象的集合或数组。然而,我遇到了这个错误:

  1. Symfony\Component\Mime\Exception\LogicException
  2. An email must have a "To", "Cc", or "Bcc" header.


我已经尝试向个人用户发送通知如下

  1. foreach ($users as $user) {
  2. $user->notify(new ConsumableNotification($arguments));
  3. }


出现同样的错误。但是,明确发送邮件:

  1. foreach ($users as $user) {
  2. Mail::to($user)
  3. ->send(new ConsumableAlert($arguments));
  4. }


工作,所以我假设通知类中出现了错误。通知类定义如下:

  1. class ConsumableNotification extends Notification
  2. {
  3. use Queueable;
  4. /**
  5. * Create a new notification instance.
  6. *
  7. * @return void
  8. */
  9. public function __construct(
  10. public ConsumableTypes $consumableType,
  11. public float|null $remains = null,
  12. public Carbon|null $expires = null,
  13. )
  14. {
  15. //
  16. }
  17. /**
  18. * Get the notification's delivery channels.
  19. *
  20. * @param mixed $notifiable
  21. * @return array
  22. */
  23. public function via($notifiable)
  24. {
  25. return ['mail', 'database'];
  26. }
  27. /**
  28. * Get the mail representation of the notification.
  29. *
  30. * @param mixed $notifiable
  31. * @return \Illuminate\Notifications\Messages\MailMessage
  32. */
  33. public function toMail($notifiable)
  34. {
  35. return (new ConsumableAlert(
  36. $this->consumableType,
  37. $this->remains,
  38. $this->expires
  39. ));
  40. }
  41. /**
  42. * Get the array representation of the notification.
  43. *
  44. * @param mixed $notifiable
  45. * @return array
  46. */
  47. public function toArray($notifiable)
  48. {
  49. return [
  50. 'consumable_type_id' => $this->consumableType->id,
  51. 'remains' => $this->remains,
  52. 'expires' => $this->expires
  53. ];
  54. }
  55. }


有人发现这个问题吗?

llmtgqce

llmtgqce1#

在花了很多时间之后,我看到需要在通知类中添加to方法,如下所示:

  1. /**
  2. * Get the mail representation of the notification.
  3. *
  4. * @param mixed $notifiable
  5. * @return \Illuminate\Mail\Mailable
  6. */
  7. public function toMail($notifiable)
  8. {
  9. return (new MyMailable($arguments))
  10. ->to($notifiable->email);
  11. }

字符串
不知何故,我在阅读文档时错过了5次。

相关问题