‪TYPO3CMS  10.4
TransportFactory.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\LoggerAwareInterface;
21 use Psr\Log\LoggerAwareTrait;
22 use Symfony\Component\Mailer\Transport;
23 use Symfony\Component\Mailer\Transport\NullTransport;
24 use Symfony\Component\Mailer\Transport\SendmailTransport;
25 use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
26 use Symfony\Component\Mailer\Transport\TransportInterface;
27 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
32 
36 class ‪TransportFactory implements ‪SingletonInterface, LoggerAwareInterface
37 {
38  use LoggerAwareTrait;
39 
40  public const ‪SPOOL_MEMORY = 'memory';
41  public const ‪SPOOL_FILE = 'file';
42 
46  protected ‪$dispatcher;
47 
51  protected ‪$logManager;
52 
53  public function ‪__construct(EventDispatcherInterface ‪$dispatcher, ‪LogManagerInterface ‪$logManager)
54  {
55  $this->dispatcher = ‪$dispatcher;
56  $this->logManager = ‪$logManager;
57  }
58 
67  public function get(array $mailSettings): TransportInterface
68  {
69  if (!isset($mailSettings['transport'])) {
70  throw new \InvalidArgumentException('Key "transport" must be set in the mail settings', 1469363365);
71  }
72  if ($mailSettings['transport'] === 'spool') {
73  throw new \InvalidArgumentException('Mail transport can not be set to "spool"', 1469363238);
74  }
75 
76  $transport = null;
77  $transportType = isset($mailSettings['transport_spool_type'])
78  && !empty($mailSettings['transport_spool_type'])
79  ? 'spool' : $mailSettings['transport'];
80 
81  switch ($transportType) {
82  case 'spool':
83  $transport = $this->‪createSpool($mailSettings);
84  break;
85  case 'smtp':
86  // Get settings to be used when constructing the transport object
87  if (
88  isset($mailSettings['transport_smtp_server'])
89  && strpos($mailSettings['transport_smtp_server'], ':') > 0
90  ) {
91  $parts = ‪GeneralUtility::trimExplode(':', $mailSettings['transport_smtp_server'], true);
92  $host = $parts[0];
93  $port = $parts[1] ?? null;
94  } else {
95  $host = (string)($mailSettings['transport_smtp_server'] ?? '');
96  $port = null;
97  }
98 
99  if ($host === '') {
100  throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp".', 1291068606);
101  }
102  if ($port === null || $port === '') {
103  $port = 25;
104  } else {
105  $port = (int)$port;
106  }
107  $useEncryption = (bool)($mailSettings['transport_smtp_encrypt'] ?? false) ?: null;
108  // Create transport
109  $transport = new EsmtpTransport(
110  $host,
111  $port,
112  $useEncryption,
113  $this->dispatcher,
114  $this->logManager->getLogger(EsmtpTransport::class)
115  );
116  // Need authentication?
117  $username = (string)($mailSettings['transport_smtp_username'] ?? '');
118  if ($username !== '') {
119  $transport->setUsername($username);
120  }
121  $password = (string)($mailSettings['transport_smtp_password'] ?? '');
122  if ($password !== '') {
123  $transport->setPassword($password);
124  }
125  break;
126  case 'sendmail':
127  $sendmailCommand = $mailSettings['transport_sendmail_command'] ?? @ini_get('sendmail_path');
128  if (empty($sendmailCommand)) {
129  $sendmailCommand = '/usr/sbin/sendmail -bs';
130  $this->logger->warning('Mailer transport "sendmail" was chosen without a specific command, using "' . $sendmailCommand . '"');
131  }
132  // Create transport
133  $transport = new SendmailTransport(
134  $sendmailCommand,
135  $this->dispatcher,
136  $this->logManager->getLogger(SendmailTransport::class)
137  );
138  break;
139  case 'mbox':
140  $mboxFile = $mailSettings['transport_mbox_file'];
141  if ($mboxFile == '') {
142  throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox".', 1294586645);
143  }
144  // Create our transport
145  $transport = GeneralUtility::makeInstance(
146  MboxTransport::class,
147  $mboxFile,
148  $this->dispatcher,
149  $this->logManager->getLogger(MboxTransport::class)
150  );
151  break;
152  // Used for testing purposes
153  case 'null':
154  case NullTransport::class:
155  $transport = new NullTransport(
156  $this->dispatcher,
157  $this->logManager->getLogger(NullTransport::class)
158  );
159  break;
160  // Used by Symfony's Transport Factory
161  case !empty($mailSettings['dsn']):
162  $transport = Transport::fromDsn(
163  $mailSettings['dsn'],
164  $this->dispatcher,
165  null,
166  $this->logManager->getLogger(Transport::class)
167  );
168  break;
169  default:
170  // Custom mail transport
171  $transport = GeneralUtility::makeInstance($mailSettings['transport'], $mailSettings);
172  if (!$transport instanceof TransportInterface) {
173  throw new \RuntimeException($mailSettings['transport'] . ' is not an implementation of Symfony\Mailer\TransportInterface,
174  but must implement that interface to be used as a mail transport.', 1323006478);
175  }
176  }
177  return $transport;
178  }
179 
187  protected function ‪createSpool(array $mailSettings): DelayedTransportInterface
188  {
189  $spool = null;
190  switch ($mailSettings['transport_spool_type']) {
191  case ‪self::SPOOL_FILE:
192  $path = GeneralUtility::getFileAbsFileName($mailSettings['transport_spool_filepath']);
193  if (empty($path)) {
194  throw new \RuntimeException('The Spool Type filepath must be configured for TYPO3 in order to be used. Be sure that it\'s not accessible via the web.', 1518558797);
195  }
196  $spool = GeneralUtility::makeInstance(
197  FileSpool::class,
198  $path,
199  $this->dispatcher,
200  $this->logManager->getLogger(FileSpool::class)
201  );
202  break;
204  $spool = GeneralUtility::makeInstance(
205  MemorySpool::class,
206  $this->dispatcher,
207  $this->logManager->getLogger(MemorySpool::class)
208  );
209  break;
210  default:
211  $spool = GeneralUtility::makeInstance($mailSettings['transport_spool_type'], $mailSettings);
212  if (!($spool instanceof DelayedTransportInterface)) {
213  throw new \RuntimeException(
214  $mailSettings['transport_spool_type'] . ' is not an implementation of DelayedTransportInterface, but must implement that interface to be used as a mail spool.',
215  1466799482
216  );
217  }
218  break;
219  }
220  return $spool;
221  }
222 }
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:22
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Mail\TransportFactory\SPOOL_FILE
‪const SPOOL_FILE
Definition: TransportFactory.php:41
‪TYPO3\CMS\Core\Mail\TransportFactory\SPOOL_MEMORY
‪const SPOOL_MEMORY
Definition: TransportFactory.php:40
‪TYPO3\CMS\Core\Mail\TransportFactory\$logManager
‪LogManagerInterface $logManager
Definition: TransportFactory.php:49
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Mail\TransportFactory\__construct
‪__construct(EventDispatcherInterface $dispatcher, LogManagerInterface $logManager)
Definition: TransportFactory.php:51
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Core\Mail\TransportFactory\$dispatcher
‪EventDispatcherInterface $dispatcher
Definition: TransportFactory.php:45
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Mail\TransportFactory\createSpool
‪DelayedTransportInterface createSpool(array $mailSettings)
Definition: TransportFactory.php:185
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:37