[FEATURE] Integrate Swift Mailer's spool transport
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / MemorySpool.php
1 <?php
2
3 namespace TYPO3\CMS\Core\Mail;
4
5 /* *
6 * This script is part of the TYPO3 project - inspiring people to share! *
7 * *
8 * TYPO3 is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU General Public License version 3 as published by *
10 * the Free Software Foundation. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
15 * Public License for more details. *
16 * */
17
18 use TYPO3\CMS\Core\Log\LogManager;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Small wrapper for \Swift_MemorySpool
23 *
24 * Because TYPO3 doesn't offer a terminate signal or hook,
25 * and taking in account the risk that extensions do some redirects or even exits,
26 * we simpley use the destructor of a singleton class which should be pretty much
27 * at the end of a request.
28 *
29 * To have only one memory spool per request seems to be more appropriate anyway.
30 *
31 * @api experimental! This class is experimental and subject to change!
32 */
33 class MemorySpool extends \Swift_MemorySpool implements \TYPO3\CMS\Core\SingletonInterface
34 {
35 public function __destruct()
36 {
37 $this->sendMessages();
38 }
39
40 public function sendMessages()
41 {
42 $mailer = GeneralUtility::makeInstance(Mailer::class);
43 try {
44 $this->flushQueue($mailer->getRealTransport());
45 } catch (\Swift_TransportException $exception) {
46 $this->getLogger()->error(sprintf('Exception occurred while flushing email queue: %s', $exception->getMessage()));
47 }
48 }
49
50 /**
51 * Get class logger
52 *
53 * @return TYPO3\CMS\Core\Log\Logger
54 */
55 protected function getLogger()
56 {
57 return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
58 }
59 }