[FEATURE] Integrate Swift Mailer's spool transport
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Command / SendEmailCommand.php
1 <?php
2 namespace TYPO3\CMS\Core\Command;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Symfony\Component\Console\Command\Command;
18 use Symfony\Component\Console\Input\InputArgument;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Output\OutputInterface;
21 use Symfony\Component\Console\Style\SymfonyStyle;
22 use TYPO3\CMS\Core\Mail\Mailer;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Command for sending spooled messages.
27 *
28 * Inspired and partially taken from symfony's swiftmailer package.
29 *
30 * @link https://github.com/symfony/swiftmailer-bundle/blob/master/Command/SendEmailCommand.php
31 */
32 class SendEmailCommand extends Command
33 {
34 protected function configure()
35 {
36 $this
37 ->setDescription('Sends emails from the spool')
38 ->addOption('message-limit', null, InputArgument::REQUIRED, 'The maximum number of messages to send.')
39 ->addOption('time-limit', null, InputArgument::REQUIRED, 'The time limit for sending messages (in seconds).')
40 ->addOption('recover-timeout', null, InputArgument::REQUIRED, 'The timeout for recovering messages that have taken too long to send (in seconds).');
41 }
42
43 protected function execute(InputInterface $input, OutputInterface $output)
44 {
45 $io = new SymfonyStyle($input, $output);
46
47 $mailer = $this->getMailer();
48
49 $transport = $mailer->getTransport();
50 if ($transport instanceof \Swift_Transport_SpoolTransport) {
51 $spool = $transport->getSpool();
52 if ($spool instanceof \Swift_ConfigurableSpool) {
53 $spool->setMessageLimit((int) $input->getOption('message-limit'));
54 $spool->setTimeLimit((int) $input->getOption('time-limit'));
55 }
56 if ($spool instanceof \Swift_FileSpool) {
57 if (null !== $recoverTimeout) {
58 $spool->recover((int) $input->getOption('recover-timeout'));
59 } else {
60 $spool->recover();
61 }
62 }
63 $sent = $spool->flushQueue($mailer->getRealTransport());
64 $io->text(sprintf('<comment>%d</comment> emails sent', $sent));
65 } else {
66 $io->warning('Transport is not a Swift_Transport_SpoolTransport.');
67 }
68 }
69
70 /**
71 * Returns the TYPO3 mailer.
72 *
73 * @return \TYPO3\CMS\Core\Mail\Mailer
74 */
75 protected function getMailer(): Mailer
76 {
77 return GeneralUtility::makeInstance(Mailer::class);
78 }
79 }