Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Console\Commands;
- use App\Modules\Email\Services\EmailSenderService;
- use App\Modules\Email\Models\Email;
- use Illuminate\Console\Command;
- use Exception;
- class SendEmail extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'email:send {--limit=100 : Number of emails}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Will send all (not sent) outgoing emails';
- /**
- * @var EmailSenderService
- */
- private $service;
- /**
- * Create a new command instance.
- *
- * @param EmailSenderService $service
- */
- public function __construct(EmailSenderService $service) {
- parent::__construct();
- $this->service = $service;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $limit = $this->option('limit');
- $query = new Email;
- $query = $query->where('is_sent', 0)->where('direction', Email::DIRECTION_OUT);
- if ($limit) {
- $query = $query->limit($limit);
- }
- $emails = $query->get();
- foreach ($emails as $email) {
- try {
- $wasSent = $this->service->sendAdvancedEmail(
- $email->getToEmail(),
- $email->getFromEmail(),
- $email->getCcEmail(),
- $email->getBcc(),
- $email->getSubject(),
- $email->getBodyAny()
- );
- if ($wasSent) {
- $email->markAsSent();
- $email->save();
- $this->line('Email '.$email->getId().' was sent to '.$email->getToEmail());
- }
- } catch (Exception $e) {
- $this->error('Error while sending email: '.$email->getId());
- $this->error($e->getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement