Просмотр файла app/Console/Commands/MessageSend.php

Размер файла: 1.53Kb
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use App\Models\Mailing;
  6. use Illuminate\Console\Command;
  7.  
  8. class MessageSend extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'message:send';
  16.  
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Message send';
  23.  
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33.  
  34. /**
  35. * Рассылает письма
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. $queues = Mailing::query()
  42. ->where('sent', 0)
  43. ->limit(setting('sendmailpacket'))
  44. ->get();
  45.  
  46. if ($queues->isNotEmpty()) {
  47. foreach ($queues as $queue) {
  48. $user = getUserById($queue->user_id);
  49.  
  50. if ($user) {
  51. $data = [
  52. 'to' => $user->email,
  53. 'subject' => $queue->subject,
  54. 'text' => $queue->text,
  55. 'unsubscribe' => $user->subscribe,
  56. ];
  57.  
  58. sendMail('mailer.default', $data);
  59. }
  60.  
  61. $queue->update([
  62. 'sent' => 1,
  63. 'sent_at' => SITETIME,
  64. ]);
  65. }
  66. }
  67.  
  68. $this->info('Message sent successfully.');
  69.  
  70. return 0;
  71. }
  72. }