‪TYPO3CMS  ‪main
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 
47  protected array ‪$queuedMessages = [];
48 
52  protected int ‪$retries = 3;
53 
57  public function ‪__construct(
58  ?EventDispatcherInterface $dispatcher = null,
59  protected readonly ?LoggerInterface $logger = null
60  ) {
61  parent::__construct($dispatcher, $logger);
62 
63  $this->setMaxPerSecond(0);
64  }
65 
69  public function ‪__destruct()
70  {
71  // TODO: DI should be used to inject the MailerInterface
72  $mailer = GeneralUtility::makeInstance(MailerInterface::class);
73  try {
74  $this->‪flushQueue($mailer->getRealTransport());
75  } catch (TransportExceptionInterface $exception) {
76  if ($this->logger instanceof LoggerInterface) {
77  $this->logger->error('An Exception occurred while flushing email queue: {message}', ['exception' => $exception, 'message' => $exception->getMessage()]);
78  }
79  }
80  }
81 
82  public function ‪flushQueue(TransportInterface $transport): int
83  {
84  if ($this->queuedMessages === []) {
85  return 0;
86  }
87 
89  $message = null;
90  $count = 0;
91  while (‪$retries--) {
92  try {
93  while ($message = array_pop($this->queuedMessages)) {
94  $transport->send($message->getMessage(), $message->getEnvelope());
95  $count++;
96  }
97  } catch (TransportExceptionInterface $exception) {
98  if (‪$retries) {
99  // re-queue the message at the end of the queue to give a chance
100  // to the other messages to be sent, in case the failure was due to
101  // this message and not just the transport failing
102  array_unshift($this->queuedMessages, $message);
103 
104  // wait half a second before we try again
105  usleep(500000);
106  } else {
107  throw $exception;
108  }
109  }
110  }
111  return $count;
112  }
113 
117  protected function ‪doSend(SentMessage $message): void
118  {
119  $this->queuedMessages[] = $message;
120  }
121 
122  public function ‪__toString(): string
123  {
124  return 'MemorySpool';
125  }
126 }
‪TYPO3\CMS\Core\Mail\MemorySpool
Definition: MemorySpool.php:41
‪TYPO3\CMS\Core\Mail\MemorySpool\__toString
‪__toString()
Definition: MemorySpool.php:122
‪TYPO3\CMS\Core\Mail\MemorySpool\$retries
‪int $retries
Definition: MemorySpool.php:52
‪TYPO3\CMS\Core\Mail\MemorySpool\doSend
‪doSend(SentMessage $message)
Definition: MemorySpool.php:117
‪TYPO3\CMS\Core\Mail\MemorySpool\$queuedMessages
‪array $queuedMessages
Definition: MemorySpool.php:47
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Mail\MemorySpool\flushQueue
‪flushQueue(TransportInterface $transport)
Definition: MemorySpool.php:82
‪TYPO3\CMS\Core\Mail\MemorySpool\__construct
‪__construct(?EventDispatcherInterface $dispatcher=null, protected readonly ?LoggerInterface $logger=null)
Definition: MemorySpool.php:57
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Mail\MemorySpool\__destruct
‪__destruct()
Definition: MemorySpool.php:69
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18