‪TYPO3CMS  11.5
MemorySpool.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 
18 namespace ‪TYPO3\CMS\Core\Mail;
19 
20 use Psr\Log\LoggerInterface;
21 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
22 use Symfony\Component\Mailer\SentMessage;
23 use Symfony\Component\Mailer\Transport\AbstractTransport;
24 use Symfony\Component\Mailer\Transport\TransportInterface;
25 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
29 
40 class ‪MemorySpool extends AbstractTransport implements ‪SingletonInterface, ‪DelayedTransportInterface
41 {
43 
49  protected ‪$logger;
50 
54  protected ‪$queuedMessages = [];
55 
61  protected ‪$retries = 3;
62 
69  public function ‪__construct(
70  EventDispatcherInterface $dispatcher = null,
71  LoggerInterface ‪$logger = null
72  ) {
73  parent::__construct($dispatcher, ‪$logger);
74 
75  $this->logger = ‪$logger;
76 
77  $this->setMaxPerSecond(0);
78  }
79 
83  public function ‪__destruct()
84  {
85  $mailer = GeneralUtility::makeInstance(Mailer::class);
86  try {
87  $this->‪flushQueue($mailer->getRealTransport());
88  } catch (TransportExceptionInterface $exception) {
89  if ($this->logger instanceof LoggerInterface) {
90  $this->logger->error('An Exception occurred while flushing email queue: {message}', ['exception' => $exception, 'message' => $exception->getMessage()]);
91  }
92  }
93  }
94 
98  public function ‪flushQueue(TransportInterface $transport): int
99  {
100  if ($this->queuedMessages === []) {
101  return 0;
102  }
103 
105  $message = null;
106  $count = 0;
107  while (‪$retries--) {
108  try {
109  while ($message = array_pop($this->queuedMessages)) {
110  $transport->send($message->getMessage(), $message->getEnvelope());
111  $count++;
112  }
113  } catch (TransportExceptionInterface $exception) {
114  if (‪$retries) {
115  // re-queue the message at the end of the queue to give a chance
116  // to the other messages to be sent, in case the failure was due to
117  // this message and not just the transport failing
118  array_unshift($this->queuedMessages, $message);
119 
120  // wait half a second before we try again
121  usleep(500000);
122  } else {
123  throw $exception;
124  }
125  }
126  }
127  return $count;
128  }
129 
134  protected function ‪doSend(SentMessage $message): void
135  {
136  $this->queuedMessages[] = $message;
137  }
138 
139  public function ‪__toString(): string
140  {
141  return 'MemorySpool';
142  }
143 }
‪TYPO3\CMS\Core\Mail\MemorySpool
Definition: MemorySpool.php:41
‪TYPO3\CMS\Core\Mail\MemorySpool\__toString
‪__toString()
Definition: MemorySpool.php:136
‪TYPO3\CMS\Core\Mail\MemorySpool\$retries
‪int $retries
Definition: MemorySpool.php:58
‪TYPO3\CMS\Core\Mail\MemorySpool\doSend
‪doSend(SentMessage $message)
Definition: MemorySpool.php:131
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Mail\MemorySpool\flushQueue
‪flushQueue(TransportInterface $transport)
Definition: MemorySpool.php:95
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪TYPO3\CMS\Core\Mail\MemorySpool\__construct
‪__construct(EventDispatcherInterface $dispatcher=null, LoggerInterface $logger=null)
Definition: MemorySpool.php:66
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Mail\MemorySpool\$logger
‪LoggerInterface null $logger
Definition: MemorySpool.php:48
‪TYPO3\CMS\Core\Mail\MemorySpool\$queuedMessages
‪SentMessage[] $queuedMessages
Definition: MemorySpool.php:52
‪TYPO3\CMS\Core\Mail\MemorySpool\__destruct
‪__destruct()
Definition: MemorySpool.php:80
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18