‪TYPO3CMS  ‪main
SendEmailCommand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Console\Command\Command;
21 use Symfony\Component\Console\Input\InputInterface;
22 use Symfony\Component\Console\Input\InputOption;
23 use Symfony\Component\Console\Output\OutputInterface;
24 use Symfony\Component\Console\Style\SymfonyStyle;
29 
37 class ‪SendEmailCommand extends Command
38 {
42  protected function ‪configure()
43  {
44  $this
45  ->addOption('message-limit', null, InputOption::VALUE_REQUIRED, 'The maximum number of messages to send.')
46  ->addOption('time-limit', null, InputOption::VALUE_REQUIRED, 'The time limit for sending messages (in seconds).')
47  ->addOption('recover-timeout', null, InputOption::VALUE_REQUIRED, 'The timeout for recovering messages that have taken too long to send (in seconds).')
48  ->setAliases(['swiftmailer:spool:send']);
49  }
50 
54  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
55  {
56  $io = new SymfonyStyle($input, ‪$output);
57 
58  $mailer = $this->‪getMailer();
59 
60  $transport = $mailer->getTransport();
61  if ($transport instanceof ‪DelayedTransportInterface) {
62  if ($transport instanceof ‪FileSpool) {
63  $transport->setMessageLimit((int)$input->getOption('message-limit'));
64  $transport->setTimeLimit((int)$input->getOption('time-limit'));
65  $recoverTimeout = (int)$input->getOption('recover-timeout');
66  if ($recoverTimeout) {
67  $transport->recover($recoverTimeout);
68  } else {
69  $transport->recover();
70  }
71  }
72  $sent = $transport->flushQueue($mailer->getRealTransport());
73  $io->comment($sent . ' emails sent');
74  return Command::SUCCESS;
75  }
76  $io->error('The Mailer Transport is not set to "spool".');
77 
78  return Command::FAILURE;
79  }
80 
84  protected function ‪getMailer(): ‪MailerInterface
85  {
86  // TODO: DI should be used to inject the MailerInterface
87  return GeneralUtility::makeInstance(MailerInterface::class);
88  }
89 }
‪TYPO3\CMS\Core\Command\SendEmailCommand
Definition: SendEmailCommand.php:38
‪TYPO3\CMS\Core\Command\SendEmailCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: SendEmailCommand.php:54
‪TYPO3\CMS\Core\Mail\MailerInterface
Definition: MailerInterface.php:28
‪TYPO3\CMS\Core\Command\SendEmailCommand\getMailer
‪getMailer()
Definition: SendEmailCommand.php:84
‪TYPO3\CMS\Core\Mail\FileSpool
Definition: FileSpool.php:39
‪TYPO3\CMS\Core\Command\SendEmailCommand\configure
‪configure()
Definition: SendEmailCommand.php:42
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Command
Definition: CacheFlushCommand.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52