‪TYPO3CMS  10.4
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  $this->logger->error('An Exception occurred while flushing email queue: ' . $exception->getMessage());
90  }
91  }
92 
96  public function ‪flushQueue(TransportInterface $transport): int
97  {
98  if ($this->queuedMessages === []) {
99  return 0;
100  }
101 
103  $message = null;
104  $count = 0;
105  while (‪$retries--) {
106  try {
107  while ($message = array_pop($this->queuedMessages)) {
108  $transport->send($message->getMessage(), $message->getEnvelope());
109  $count++;
110  }
111  } catch (TransportExceptionInterface $exception) {
112  if (‪$retries && $message) {
113  // re-queue the message at the end of the queue to give a chance
114  // to the other messages to be sent, in case the failure was due to
115  // this message and not just the transport failing
116  array_unshift($this->queuedMessages, $message);
117 
118  // wait half a second before we try again
119  usleep(500000);
120  } else {
121  throw $exception;
122  }
123  }
124  }
125  return $count;
126  }
127 
132  protected function ‪doSend(SentMessage $message): void
133  {
134  $this->queuedMessages[] = $message;
135  }
136 
137  public function ‪__toString(): string
138  {
139  return 'MemorySpool';
140  }
141 }
‪TYPO3\CMS\Core\Mail\MemorySpool
Definition: MemorySpool.php:41
‪TYPO3\CMS\Core\Mail\MemorySpool\__toString
‪__toString()
Definition: MemorySpool.php:134
‪TYPO3\CMS\Core\Mail\MemorySpool\$retries
‪int $retries
Definition: MemorySpool.php:58
‪TYPO3\CMS\Core\Mail\MemorySpool\doSend
‪doSend(SentMessage $message)
Definition: MemorySpool.php:129
‪TYPO3\CMS\Core\Mail\MemorySpool\$logger
‪LoggerInterface $logger
Definition: MemorySpool.php:48
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Mail\MemorySpool\flushQueue
‪flushQueue(TransportInterface $transport)
Definition: MemorySpool.php:93
‪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:23
‪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:46
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18