[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / TransportFactory.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Mail;
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 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
20 use Symfony\Component\Mailer\Transport;
21 use Symfony\Component\Mailer\Transport\NullTransport;
22 use Symfony\Component\Mailer\Transport\SendmailTransport;
23 use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
24 use Symfony\Component\Mailer\Transport\TransportInterface;
25 use TYPO3\CMS\Core\Exception;
26 use TYPO3\CMS\Core\SingletonInterface;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * TransportFactory
31 */
32 class TransportFactory implements SingletonInterface, LoggerAwareInterface
33 {
34 use LoggerAwareTrait;
35
36 const SPOOL_MEMORY = 'memory';
37 const SPOOL_FILE = 'file';
38
39 /**
40 * Gets a transport from settings.
41 *
42 * @param array $mailSettings from $GLOBALS['TYPO3_CONF_VARS']['MAIL']
43 * @return TransportInterface
44 * @throws Exception
45 * @throws \RuntimeException
46 */
47 public function get(array $mailSettings): TransportInterface
48 {
49 if (!isset($mailSettings['transport'])) {
50 throw new \InvalidArgumentException('Key "transport" must be set in the mail settings', 1469363365);
51 }
52 if ($mailSettings['transport'] === 'spool') {
53 throw new \InvalidArgumentException('Mail transport can not be set to "spool"', 1469363238);
54 }
55
56 $transport = null;
57 $transportType = isset($mailSettings['transport_spool_type']) && !empty($mailSettings['transport_spool_type']) ? 'spool' : $mailSettings['transport'];
58
59 switch ($transportType) {
60 case 'spool':
61 $transport = $this->createSpool($mailSettings);
62 break;
63 case 'smtp':
64 // Get settings to be used when constructing the transport object
65 if (isset($mailSettings['transport_smtp_server']) && strpos($mailSettings['transport_smtp_server'], ':') > 0) {
66 $parts = GeneralUtility::trimExplode(':', $mailSettings['transport_smtp_server'], true);
67 $host = $parts[0];
68 $port = $parts[1] ?? null;
69 } else {
70 $host = (string)$mailSettings['transport_smtp_server'] ?? '';
71 $port = null;
72 }
73
74 if ($host === '') {
75 throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp".', 1291068606);
76 }
77 if ($port === null || $port === '') {
78 $port = 25;
79 } else {
80 $port = (int)$port;
81 }
82 $useEncryption = ($mailSettings['transport_smtp_encrypt'] ?? '') ?: null;
83 // Create transport
84 $transport = new EsmtpTransport($host, $port, $useEncryption);
85 // Need authentication?
86 $username = (string)($mailSettings['transport_smtp_username'] ?? '');
87 if ($username !== '') {
88 $transport->setUsername($username);
89 }
90 $password = (string)($mailSettings['transport_smtp_password'] ?? '');
91 if ($password !== '') {
92 $transport->setPassword($password);
93 }
94 break;
95 case 'sendmail':
96 $sendmailCommand = $mailSettings['transport_sendmail_command'] ?? @ini_get('sendmail_path');
97 if (empty($sendmailCommand)) {
98 $sendmailCommand = '/usr/sbin/sendmail -bs';
99 $this->logger->warning('Mailer transport "sendmail" was chosen without a specific command, using "' . $sendmailCommand . '"');
100 }
101 // Create transport
102 $transport = new SendmailTransport($sendmailCommand);
103 break;
104 case 'mbox':
105 $mboxFile = $mailSettings['transport_mbox_file'];
106 if ($mboxFile == '') {
107 throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox".', 1294586645);
108 }
109 // Create our transport
110 $transport = GeneralUtility::makeInstance(MboxTransport::class, $mboxFile);
111 break;
112 // Used for testing purposes
113 case 'null':
114 case NullTransport::class:
115 $transport = new NullTransport();
116 break;
117 // Used by Symfony's Transport Factory
118 case !empty($mailSettings['dsn']):
119 $transport = Transport::fromDsn($mailSettings['dsn']);
120 break;
121 default:
122 // Custom mail transport
123 $transport = GeneralUtility::makeInstance($mailSettings['transport'], $mailSettings);
124 if (!$transport instanceof TransportInterface) {
125 throw new \RuntimeException($mailSettings['transport'] . ' is not an implementation of Symfony\Mailer\TransportInterface,
126 but must implement that interface to be used as a mail transport.', 1323006478);
127 }
128 }
129 return $transport;
130 }
131
132 /**
133 * Creates a spool from mail settings.
134 *
135 * @param array $mailSettings
136 * @return DelayedTransportInterface
137 * @throws \RuntimeException
138 */
139 protected function createSpool(array $mailSettings): DelayedTransportInterface
140 {
141 $spool = null;
142 switch ($mailSettings['transport_spool_type']) {
143 case self::SPOOL_FILE:
144 $path = GeneralUtility::getFileAbsFileName($mailSettings['transport_spool_filepath']);
145 if (empty($path) || !file_exists($path) || !is_writable($path)) {
146 throw new \RuntimeException('The Spool Type filepath must be available and writeable for TYPO3 in order to be used. Be sure that it\'s not accessible via the web.', 1518558797);
147 }
148 $spool = GeneralUtility::makeInstance(FileSpool::class, $path);
149 break;
150 case self::SPOOL_MEMORY:
151 $spool = GeneralUtility::makeInstance(MemorySpool::class);
152 break;
153 default:
154 $spool = GeneralUtility::makeInstance($mailSettings['transport_spool_type'], $mailSettings);
155 if (!($spool instanceof DelayedTransportInterface)) {
156 throw new \RuntimeException(
157 $mailSettings['transport_spool_type'] . ' is not an implementation of DelayedTransportInterface, but must implement that interface to be used as a mail spool.',
158 1466799482
159 );
160 }
161 break;
162 }
163 return $spool;
164 }
165 }