[FEATURE] Integrate Swift Mailer's spool transport 82/49182/9
authorRemo H <r3h6@outlook.com>
Sun, 24 Jul 2016 18:23:36 +0000 (20:23 +0200)
committerBenni Mack <benni@typo3.org>
Sat, 10 Feb 2018 23:44:13 +0000 (00:44 +0100)
Adds new configuration to the install tool and provides an
extbase schedular task and command.

A small refactoring was required:
* Moved transport creation from mailer to a factory

Possible issues are:
* sending of memory-spooled messages is a bit hacky

Resolves: #76349
Releases: master
Change-Id: I9736d4f943eea2052bf935ac1fc055c336894397
Reviewed-on: https://review.typo3.org/49182
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
15 files changed:
typo3/sysext/core/Classes/Command/SendEmailCommand.php [new file with mode: 0644]
typo3/sysext/core/Classes/Mail/Mailer.php
typo3/sysext/core/Classes/Mail/MemorySpool.php [new file with mode: 0644]
typo3/sysext/core/Classes/Mail/TransportFactory.php [new file with mode: 0644]
typo3/sysext/core/Configuration/Commands.php [new file with mode: 0644]
typo3/sysext/core/Configuration/DefaultConfiguration.php
typo3/sysext/core/Configuration/DefaultConfigurationDescription.yaml
typo3/sysext/core/Documentation/Changelog/master/Feature-76349-IntegrateSwiftMailersSpoolTransportIntoTYPO3.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Command/SendEmailCommandTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeFileSpoolFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeInvalidSpoolFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeMemorySpoolFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeValidSpoolFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Mail/MailerTest.php
typo3/sysext/core/Tests/Unit/Mail/TransportFactoryTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Classes/Command/SendEmailCommand.php b/typo3/sysext/core/Classes/Command/SendEmailCommand.php
new file mode 100644 (file)
index 0000000..bc687ba
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+namespace TYPO3\CMS\Core\Command;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use TYPO3\CMS\Core\Mail\Mailer;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Command for sending spooled messages.
+ *
+ * Inspired and partially taken from symfony's swiftmailer package.
+ *
+ * @link https://github.com/symfony/swiftmailer-bundle/blob/master/Command/SendEmailCommand.php
+ */
+class SendEmailCommand extends Command
+{
+    protected function configure()
+    {
+        $this
+            ->setDescription('Sends emails from the spool')
+            ->addOption('message-limit', null, InputArgument::REQUIRED, 'The maximum number of messages to send.')
+            ->addOption('time-limit', null, InputArgument::REQUIRED, 'The time limit for sending messages (in seconds).')
+            ->addOption('recover-timeout', null, InputArgument::REQUIRED, 'The timeout for recovering messages that have taken too long to send (in seconds).');
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $io = new SymfonyStyle($input, $output);
+
+        $mailer = $this->getMailer();
+
+        $transport = $mailer->getTransport();
+        if ($transport instanceof \Swift_Transport_SpoolTransport) {
+            $spool = $transport->getSpool();
+            if ($spool instanceof \Swift_ConfigurableSpool) {
+                $spool->setMessageLimit((int) $input->getOption('message-limit'));
+                $spool->setTimeLimit((int) $input->getOption('time-limit'));
+            }
+            if ($spool instanceof \Swift_FileSpool) {
+                if (null !== $recoverTimeout) {
+                    $spool->recover((int) $input->getOption('recover-timeout'));
+                } else {
+                    $spool->recover();
+                }
+            }
+            $sent = $spool->flushQueue($mailer->getRealTransport());
+            $io->text(sprintf('<comment>%d</comment> emails sent', $sent));
+        } else {
+            $io->warning('Transport is not a Swift_Transport_SpoolTransport.');
+        }
+    }
+
+    /**
+     * Returns the TYPO3 mailer.
+     *
+     * @return \TYPO3\CMS\Core\Mail\Mailer
+     */
+    protected function getMailer(): Mailer
+    {
+        return GeneralUtility::makeInstance(Mailer::class);
+    }
+}
index 07b0e49..a6c83a4 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Core\Mail;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Swift_Transport;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
@@ -80,59 +81,7 @@ class Mailer extends \Swift_Mailer
      */
     private function initializeTransport()
     {
-        switch ($this->mailSettings['transport']) {
-            case 'smtp':
-                // Get settings to be used when constructing the transport object
-                list($host, $port) = preg_split('/:/', $this->mailSettings['transport_smtp_server']);
-                if ($host === '') {
-                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"', 1291068606);
-                }
-                if ($port === null || $port === '') {
-                    $port = '25';
-                }
-                $useEncryption = $this->mailSettings['transport_smtp_encrypt'] ?: null;
-                // Create our transport
-                $this->transport = \Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
-                // Need authentication?
-                $username = $this->mailSettings['transport_smtp_username'];
-                if ($username !== '') {
-                    $this->transport->setUsername($username);
-                }
-                $password = $this->mailSettings['transport_smtp_password'];
-                if ($password !== '') {
-                    $this->transport->setPassword($password);
-                }
-                break;
-            case 'sendmail':
-                $sendmailCommand = $this->mailSettings['transport_sendmail_command'];
-                if (empty($sendmailCommand)) {
-                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"', 1291068620);
-                }
-                // Create our transport
-                $this->transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
-                break;
-            case 'mbox':
-                $mboxFile = $this->mailSettings['transport_mbox_file'];
-                if ($mboxFile == '') {
-                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
-                }
-                // Create our transport
-                $this->transport = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MboxTransport::class, $mboxFile);
-                break;
-            case 'mail':
-                // Create the transport, no configuration required
-                $this->transport = \Swift_MailTransport::newInstance();
-                break;
-            default:
-                // Custom mail transport
-                $customTransport = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($this->mailSettings['transport'], $this->mailSettings);
-                if ($customTransport instanceof \Swift_Transport) {
-                    $this->transport = $customTransport;
-                } else {
-                    throw new \RuntimeException($this->mailSettings['transport'] . ' is not an implementation of \\Swift_Transport,
-                                                       but must implement that interface to be used as a mail transport.', 1323006478);
-                }
-        }
+        $this->transport = $this->getTransportFactory()->get($this->mailSettings);
     }
 
     /**
@@ -151,6 +100,27 @@ class Mailer extends \Swift_Mailer
     }
 
     /**
+     * Returns the real transport (not a spool).
+     *
+     * @return \Swift_Transport
+     * @api
+     */
+    public function getRealTransport(): Swift_Transport
+    {
+        $mailSettings = (false === empty($this->mailSettings)) ? $this->mailSettings: (array)$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
+        unset($mailSettings['transport_spool_type']);
+        return $this->getTransportFactory()->get($mailSettings);
+    }
+
+    /**
+     * @return TransportFactory
+     */
+    protected function getTransportFactory(): TransportFactory
+    {
+        return GeneralUtility::makeInstance(TransportFactory::class);
+    }
+
+    /**
      * Get the object manager
      *
      * @return \TYPO3\CMS\Extbase\Object\ObjectManager
diff --git a/typo3/sysext/core/Classes/Mail/MemorySpool.php b/typo3/sysext/core/Classes/Mail/MemorySpool.php
new file mode 100644 (file)
index 0000000..1546def
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+namespace TYPO3\CMS\Core\Mail;
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 3 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+use TYPO3\CMS\Core\Log\LogManager;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Small wrapper for \Swift_MemorySpool
+ *
+ * Because TYPO3 doesn't offer a terminate signal or hook,
+ * and taking in account the risk that extensions do some redirects or even exits,
+ * we simpley use the destructor of a singleton class which should be pretty much
+ * at the end of a request.
+ *
+ * To have only one memory spool per request seems to be more appropriate anyway.
+ *
+ * @api experimental! This class is experimental and subject to change!
+ */
+class MemorySpool extends \Swift_MemorySpool implements \TYPO3\CMS\Core\SingletonInterface
+{
+    public function __destruct()
+    {
+        $this->sendMessages();
+    }
+
+    public function sendMessages()
+    {
+        $mailer = GeneralUtility::makeInstance(Mailer::class);
+        try {
+            $this->flushQueue($mailer->getRealTransport());
+        } catch (\Swift_TransportException $exception) {
+            $this->getLogger()->error(sprintf('Exception occurred while flushing email queue: %s', $exception->getMessage()));
+        }
+    }
+
+    /**
+     * Get class logger
+     *
+     * @return TYPO3\CMS\Core\Log\Logger
+     */
+    protected function getLogger()
+    {
+        return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
+    }
+}
diff --git a/typo3/sysext/core/Classes/Mail/TransportFactory.php b/typo3/sysext/core/Classes/Mail/TransportFactory.php
new file mode 100644 (file)
index 0000000..88cb86e
--- /dev/null
@@ -0,0 +1,134 @@
+<?php
+
+namespace TYPO3\CMS\Core\Mail;
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 3 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * TransportFactory
+ */
+class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
+{
+    const SPOOL_MEMORY = 'memory';
+    const SPOOL_FILE = 'file';
+
+    /**
+     * Gets a transport from settings.
+     *
+     * @param  array   $mailSettings from $GLOBALS['TYPO3_CONF_VARS']['MAIL']
+     * @return \Swift_Transport
+     * @throws \TYPO3\CMS\Core\Exception
+     * @throws \RuntimeException
+     */
+    public function get(array $mailSettings): \Swift_Transport
+    {
+        if (!isset($mailSettings['transport'])) {
+            throw new \InvalidArgumentException('Key "transport" must be set in the mail settings', 1469363365);
+        }
+        if ($mailSettings['transport'] === 'spool') {
+            throw new \InvalidArgumentException('Mail transport can not be set to "spool"', 1469363238);
+        }
+
+        $transport = null;
+        $transportType = (isset($mailSettings['transport_spool_type']) && !empty($mailSettings['transport_spool_type'])) ? 'spool': $mailSettings['transport'];
+
+        switch ($transportType) {
+            case 'spool':
+                $transport = \Swift_SpoolTransport::newInstance($this->createSpool($mailSettings));
+                break;
+            case 'smtp':
+                // Get settings to be used when constructing the transport object
+                list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
+                if ($host === '') {
+                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"', 1291068606);
+                }
+                if ($port === null || $port === '') {
+                    $port = '25';
+                }
+                $useEncryption = $mailSettings['transport_smtp_encrypt'] ?: null;
+                // Create our transport
+                $transport = \Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
+                // Need authentication?
+                $username = $mailSettings['transport_smtp_username'];
+                if ($username !== '') {
+                    $transport->setUsername($username);
+                }
+                $password = $mailSettings['transport_smtp_password'];
+                if ($password !== '') {
+                    $transport->setPassword($password);
+                }
+                break;
+            case 'sendmail':
+                $sendmailCommand = $mailSettings['transport_sendmail_command'];
+                if (empty($sendmailCommand)) {
+                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"', 1291068620);
+                }
+                // Create our transport
+                $transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
+                break;
+            case 'mbox':
+                $mboxFile = $mailSettings['transport_mbox_file'];
+                if ($mboxFile == '') {
+                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
+                }
+                // Create our transport
+                $transport = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MboxTransport::class, $mboxFile);
+                break;
+            case 'mail':
+                // Create the transport, no configuration required
+                $transport = \Swift_MailTransport::newInstance();
+                break;
+            default:
+                // Custom mail transport
+                $customTransport = GeneralUtility::makeInstance($mailSettings['transport'], $mailSettings);
+                if ($customTransport instanceof \Swift_Transport) {
+                    $transport = $customTransport;
+                } else {
+                    throw new \RuntimeException($mailSettings['transport'] . ' is not an implementation of \\Swift_Transport,
+                            but must implement that interface to be used as a mail transport.', 1323006478);
+                }
+        }
+        return $transport;
+    }
+
+    /**
+     * Creates a spool from mail settings.
+     *
+     * @param  array  $mailSettings
+     * @return \Swift_Spool
+     */
+    protected function createSpool(array $mailSettings): \Swift_Spool
+    {
+        $spool = null;
+        switch ($mailSettings['transport_spool_type']) {
+            case self::SPOOL_FILE:
+                $path = GeneralUtility::getFileAbsFileName($mailSettings['transport_spool_filepath']);
+                $spool = GeneralUtility::makeInstance(\Swift_FileSpool::class, $path);
+                break;
+            case self::SPOOL_MEMORY:
+                $spool = GeneralUtility::makeInstance(MemorySpool::class);
+                break;
+            default:
+                $spool = GeneralUtility::makeInstance($mailSettings['transport_spool_type'], $mailSettings);
+                if (!($spool instanceof \Swift_Spool)) {
+                    throw new \RuntimeException($mailSettings['spool'] . ' is not an implementation of \\Swift_Spool,
+                            but must implement that interface to be used as a mail spool.', 1466799482);
+                }
+                break;
+        }
+        return $spool;
+    }
+}
diff --git a/typo3/sysext/core/Configuration/Commands.php b/typo3/sysext/core/Configuration/Commands.php
new file mode 100644 (file)
index 0000000..7f1d0ac
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+return [
+    'swiftmailer:spool:send' => [
+        'class' => \TYPO3\CMS\Core\Command\SendEmailCommand::class
+    ],
+];
index 2b876f3..55149a4 100644 (file)
@@ -1058,6 +1058,8 @@ return [
         'defaultMailFromName' => '',
         'defaultMailReplyToAddress' => '',
         'defaultMailReplyToName' => '',
+        'transport_spool_type' => '',
+        'transport_spool_filepath' => 'typo3temp/var/messages/',
     ],
     'HTTP' => [ // HTTP configuration to tune how TYPO3 behaves on HTTP requests made by TYPO3. Have a look at http://docs.guzzlephp.org/en/latest/request-options.html for some background information on those settings.
         'allow_redirects' => [ // Mixed, set to false if you want to allow redirects, or use it as an array to add more values,
index 51dfd07..43481e4 100644 (file)
@@ -494,6 +494,12 @@ MAIL:
         defaultMailReplyToName:
             type: text
             description: 'This default name is used when no other "reply-to" name is set for a TYPO3-generated email.'
+        transport_spool_type:
+            type: text
+            description: '<dl><dt>file</dt><dd>Messages get stored to the file system till they get sent through the command swiftmailer:spool:send.</dd><dt>memory</dt><dd>Messages get send at the end of the running process.</dd><dt>&lt;classname&gt;</dt><dd>Custom class which implements the Swift_Spool interface.</dd></dl>'
+        transport_spool_filepath:
+            type: text
+            description: '<em>only with transport_spool_type=file</em>: Path where messages get temporarily stored.'
 HTTP:
     type: container
     items:
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-76349-IntegrateSwiftMailersSpoolTransportIntoTYPO3.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-76349-IntegrateSwiftMailersSpoolTransportIntoTYPO3.rst
new file mode 100644 (file)
index 0000000..91175cc
--- /dev/null
@@ -0,0 +1,67 @@
+.. include:: ../../Includes.txt
+
+=====================================================================
+Feature: #76349 - Integrate Swift Mailer's spool transport into TYPO3
+=====================================================================
+
+See :issue:`76349`
+
+Description
+===========
+
+The default behavior of the TYPO3 mailer is to send the email messages immediately. You may, however, want to avoid the performance hit of the communication to the email server, which could cause the user to wait for the next page to load while the email is sending. This can be avoided by choosing to "spool" the emails instead of sending them directly.
+
+This makes the mailer to not attempt to send the email message but instead save it somewhere such as a file. Another process can then read from the spool and take care of sending the emails in the spool. Currently only spooling to file or memory is supported.
+
+Spool Using Memory
+==================
+
+When you use spooling to store the emails to memory, they will get sent right before the kernel terminates. This means the email only gets sent if the whole request got executed without any unhandled exception or any errors. To configure this spool, use the following configuration:
+
+.. code-block:: php
+
+   $GLOBALS['TYPO3_CONF_VARS']['MAIL']['transport_spool_type'] = 'memory';
+
+
+Spool Using Files
+=================
+
+When you use the filesystem for spooling, TYPO3 by default stores the spools files in `typo3temp/var/messages/`. This folder will contain files for each email in the spool. So make sure this directory is writable by TYPO3.
+
+In order to use the spool with files, use the following configuration:
+
+.. code-block:: php
+
+   $GLOBALS['TYPO3_CONF_VARS']['MAIL']['transport_spool_type'] = 'file';
+
+If you want to store the spool somewhere else, you can define a different directory using the following setting:
+
+.. code-block:: php
+
+   $GLOBALS['TYPO3_CONF_VARS']['MAIL']['transport_spool_filepath'] = '/folder/of/choice';
+
+
+Now, when TYPO3 is instructed to send an email, it will not actually be sent but instead added to the spool. Sending the messages from the spool is done separately. There is a console command to send the messages in the spool:
+
+.. code-block:: php
+
+   ./typo3/sysext/core/bin/typo3 swiftmailer:spool:send
+
+
+It has an option to limit the number of messages to be sent:
+
+.. code-block:: php
+
+   ./typo3/sysext/core/bin/typo3 swiftmailer:spool:send --message-limit=10
+
+
+You can also set the time limit in seconds:
+
+.. code-block:: php
+
+   ./typo3/sysext/core/bin/typo3 swiftmailer:spool:send --time-limit=10
+
+
+Of course you will not want to run this manually in reality. Instead, the console command should be triggered by a cron job or scheduled task and run at a regular interval.
+
+.. index:: PHP-API, NotScanned
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Command/SendEmailCommandTest.php b/typo3/sysext/core/Tests/Unit/Command/SendEmailCommandTest.php
new file mode 100644 (file)
index 0000000..868db8c
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Functional\Command;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use Symfony\Component\Console\Tester\CommandTester;
+use TYPO3\CMS\Core\Command\SendEmailCommand;
+
+/**
+ * Testcase for the TYPO3\CMS\Core\Command\SendEmailCommand class.
+ */
+class SendEmailCommandTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function executeWillFlushTheQueue()
+    {
+        $realTransport = $this->getMockBuilder(\Swift_Transport::class)->getMock();
+
+        $spool = $this->getMockBuilder(\Swift_Spool::class)->getMock();
+        $spool
+            ->expects($this->once())
+            ->method('flushQueue')
+            ->with($realTransport)
+            ->will($this->returnValue(5))
+        ;
+        $spoolTransport = new \Swift_Transport_SpoolTransport(new \Swift_Events_SimpleEventDispatcher(), $spool);
+
+        $mailer = $this->getMockBuilder(\TYPO3\CMS\Core\Mail\Mailer::class)
+            ->disableOriginalConstructor()
+            ->setMethods(['getTransport', 'getRealTransport'])
+            ->getMock();
+
+        $mailer
+            ->expects($this->any())
+            ->method('getTransport')
+            ->will($this->returnValue($spoolTransport));
+
+        $mailer
+            ->expects($this->any())
+            ->method('getRealTransport')
+            ->will($this->returnValue($realTransport));
+
+        $command = $this->getMockBuilder(SendEmailCommand::class)
+            ->setConstructorArgs(['swiftmailer:spool:send'])
+            ->setMethods(['getMailer'])
+            ->getMock();
+
+        $command
+            ->expects($this->any())
+            ->method('getMailer')
+            ->will($this->returnValue($mailer));
+
+        $tester = new CommandTester($command);
+        $tester->execute([], []);
+
+        $this->assertStringEndsWith("5 emails sent\n", $tester->getDisplay());
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeFileSpoolFixture.php b/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeFileSpoolFixture.php
new file mode 100644 (file)
index 0000000..99a5a21
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Fixture fake valid spool
+ */
+class FakeFileSpoolFixture extends \Swift_FileSpool
+{
+    public function __construct($path)
+    {
+        $this->_path = $path;
+    }
+
+    public function getPath(): string
+    {
+        return $this->_path;
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeInvalidSpoolFixture.php b/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeInvalidSpoolFixture.php
new file mode 100644 (file)
index 0000000..18dc455
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Fixture fake invalid spool
+ */
+class FakeInvalidSpoolFixture
+{
+    private $settings;
+
+    public function __construct(array $settings)
+    {
+        $this->settings = $settings;
+    }
+
+    public function getSettings(): array
+    {
+        return $this->settings;
+    }
+
+    public function start()
+    {
+    }
+
+    public function stop()
+    {
+    }
+
+    public function isStarted()
+    {
+    }
+
+    public function queueMessage(\Swift_Mime_Message $message)
+    {
+    }
+
+    public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null)
+    {
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeMemorySpoolFixture.php b/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeMemorySpoolFixture.php
new file mode 100644 (file)
index 0000000..8554266
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Mail\MemorySpool;
+
+/**
+ * Fixture fake valid spool
+ */
+class FakeMemorySpoolFixture extends MemorySpool
+{
+    public function __destruct()
+    {
+        // Do not execute any code on destruction during tests.
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeValidSpoolFixture.php b/typo3/sysext/core/Tests/Unit/Mail/Fixtures/FakeValidSpoolFixture.php
new file mode 100644 (file)
index 0000000..6a82287
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Fixture fake valid spool
+ */
+class FakeValidSpoolFixture implements \Swift_Spool
+{
+    private $settings;
+
+    public function __construct(array $settings)
+    {
+        $this->settings = $settings;
+    }
+
+    public function getSettings(): array
+    {
+        return $this->settings;
+    }
+
+    public function start()
+    {
+    }
+
+    public function stop()
+    {
+    }
+
+    public function isStarted()
+    {
+    }
+
+    public function queueMessage(\Swift_Mime_Message $message)
+    {
+    }
+
+    public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null)
+    {
+    }
+}
index fc78b1e..182d575 100644 (file)
@@ -128,4 +128,37 @@ class MailerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $port = $this->subject->getTransport()->getPort();
         $this->assertEquals(12345, $port);
     }
+
+    /**
+     * @test
+     * @dataProvider getRealTransportReturnsNoSpoolTransportProvider
+     */
+    public function getRealTransportReturnsNoSpoolTransport($settings)
+    {
+        $this->subject->injectMailSettings($settings);
+        // $this->subject->__construct();
+        $transport = $this->subject->getRealTransport();
+
+        $this->assertInstanceOf(\Swift_Transport::class, $transport);
+        $this->assertNotInstanceOf(\Swift_SpoolTransport::class, $transport);
+    }
+
+    /**
+     * Data provider for getRealTransportReturnsNoSpoolTransport
+     *
+     * @return array Data sets
+     */
+    public static function getRealTransportReturnsNoSpoolTransportProvider()
+    {
+        return [
+            'without spool' => [[
+                'transport' => 'mail',
+                'spool' => '',
+            ]],
+            'with spool' => [[
+                'transport' => 'mail',
+                'spool' => 'memory',
+            ]],
+        ];
+    }
 }
diff --git a/typo3/sysext/core/Tests/Unit/Mail/TransportFactoryTest.php b/typo3/sysext/core/Tests/Unit/Mail/TransportFactoryTest.php
new file mode 100644 (file)
index 0000000..f43e975
--- /dev/null
@@ -0,0 +1,171 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Mail;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeValidSpoolFixture;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Testcase for the TYPO3\CMS\Core\Mail\TransportFactory class.
+ */
+class TransportFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+    /**
+     * @var \TYPO3\CMS\Core\Mail\TransportFactory
+     */
+    protected $subject;
+
+    protected function setUp()
+    {
+        $this->subject = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\TransportFactory::class);
+    }
+
+    /**
+     * @test
+     */
+    public function getReturnsSwiftSpoolTransportUsingSwiftFileSpool()
+    {
+        $mailSettings = [
+            'transport' => 'mail',
+            'transport_smtp_server' => 'localhost:25',
+            'transport_smtp_encrypt' => '',
+            'transport_smtp_username' => '',
+            'transport_smtp_password' => '',
+            'transport_sendmail_command' => '',
+            'transport_mbox_file' => '',
+            'defaultMailFromAddress' => '',
+            'defaultMailFromName' => '',
+            'transport_spool_type' => 'file',
+            'transport_spool_filepath' => 'typo3temp/var/messages/',
+        ];
+
+        // Register fixture class
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Swift_FileSpool::class]['className'] = \TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeFileSpoolFixture::class;
+
+        $transport = $this->subject->get($mailSettings);
+        $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
+
+        $spool = $transport->getSpool();
+        $this->assertInstanceOf(\Swift_FileSpool::class, $spool);
+
+        $path = $spool->getPath();
+        $this->assertContains($mailSettings['transport_spool_filepath'], $path);
+    }
+
+    /**
+     * @test
+     */
+    public function getReturnsSwiftSpoolTransportUsingSwiftMemorySpool()
+    {
+        $mailSettings = [
+            'transport' => 'mail',
+            'transport_smtp_server' => 'localhost:25',
+            'transport_smtp_encrypt' => '',
+            'transport_smtp_username' => '',
+            'transport_smtp_password' => '',
+            'transport_sendmail_command' => '',
+            'transport_mbox_file' => '',
+            'defaultMailFromAddress' => '',
+            'defaultMailFromName' => '',
+            'transport_spool_type' => 'memory',
+            'transport_spool_filepath' => 'typo3temp/var/messages/',
+        ];
+
+        // Register fixture class
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Core\Mail\MemorySpool::class]['className'] = \TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeMemorySpoolFixture::class;
+
+        $transport = $this->subject->get($mailSettings);
+        $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
+
+        $spool = $transport->getSpool();
+        $this->assertInstanceOf(\Swift_MemorySpool::class, $spool);
+    }
+
+    /**
+     * @test
+     */
+    public function getReturnsSwiftSpoolTransportUsingCustomSpool()
+    {
+        $mailSettings = [
+            'transport' => 'mail',
+            'transport_smtp_server' => 'localhost:25',
+            'transport_smtp_encrypt' => '',
+            'transport_smtp_username' => '',
+            'transport_smtp_password' => '',
+            'transport_sendmail_command' => '',
+            'transport_mbox_file' => '',
+            'defaultMailFromAddress' => '',
+            'defaultMailFromName' => '',
+            'transport_spool_type' => 'TYPO3\\CMS\\Core\\Tests\\Unit\\Mail\\Fixtures\\FakeValidSpoolFixture',
+            'transport_spool_filepath' => 'typo3temp/var/messages/',
+        ];
+
+        $transport = $this->subject->get($mailSettings);
+        $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
+
+        $spool = $transport->getSpool();
+        $this->assertInstanceOf(FakeValidSpoolFixture::class, $spool);
+
+        $this->assertSame($mailSettings, $spool->getSettings());
+    }
+
+    /**
+     * @test
+     */
+    public function getThrowsRuntimeExceptionForInvalidCustomSpool()
+    {
+        $this->expectException(\RuntimeException::class);
+        $this->expectExceptionCode(1466799482);
+
+        $mailSettings = [
+            'transport' => 'mail',
+            'transport_smtp_server' => 'localhost:25',
+            'transport_smtp_encrypt' => '',
+            'transport_smtp_username' => '',
+            'transport_smtp_password' => '',
+            'transport_sendmail_command' => '',
+            'transport_mbox_file' => '',
+            'defaultMailFromAddress' => '',
+            'defaultMailFromName' => '',
+            'transport_spool_type' => 'TYPO3\\CMS\\Core\\Tests\\Unit\\Mail\\Fixtures\\FakeInvalidSpoolFixture',
+            'transport_spool_filepath' => 'typo3temp/var/messages/',
+        ];
+
+        $this->subject->get($mailSettings);
+    }
+
+    /**
+     * @test
+     */
+    public function getReturnsSwiftMailTransport()
+    {
+        $mailSettings = [
+            'transport' => 'mail',
+            'transport_smtp_server' => 'localhost:25',
+            'transport_smtp_encrypt' => '',
+            'transport_smtp_username' => '',
+            'transport_smtp_password' => '',
+            'transport_sendmail_command' => '',
+            'transport_mbox_file' => '',
+            'defaultMailFromAddress' => '',
+            'defaultMailFromName' => '',
+            'transport_spool_type' => '',
+            'transport_spool_filepath' => 'typo3temp/var/messages/',
+        ];
+
+        $transport = $this->subject->get($mailSettings);
+        $this->assertInstanceOf(\Swift_MailTransport::class, $transport);
+    }
+}