[BUGFIX] Cleanup Spool Mailer feature
[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 TYPO3\CMS\Core\Exception;
19 use TYPO3\CMS\Core\SingletonInterface;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * TransportFactory
24 */
25 class TransportFactory implements SingletonInterface
26 {
27 const SPOOL_MEMORY = 'memory';
28 const SPOOL_FILE = 'file';
29
30 /**
31 * Gets a transport from settings.
32 *
33 * @param array $mailSettings from $GLOBALS['TYPO3_CONF_VARS']['MAIL']
34 * @return \Swift_Transport
35 * @throws Exception
36 * @throws \RuntimeException
37 */
38 public function get(array $mailSettings): \Swift_Transport
39 {
40 if (!isset($mailSettings['transport'])) {
41 throw new \InvalidArgumentException('Key "transport" must be set in the mail settings', 1469363365);
42 }
43 if ($mailSettings['transport'] === 'spool') {
44 throw new \InvalidArgumentException('Mail transport can not be set to "spool"', 1469363238);
45 }
46
47 $transport = null;
48 $transportType = isset($mailSettings['transport_spool_type']) && !empty($mailSettings['transport_spool_type']) ? 'spool' : $mailSettings['transport'];
49
50 switch ($transportType) {
51 case 'spool':
52 $transport = \Swift_SpoolTransport::newInstance($this->createSpool($mailSettings));
53 break;
54 case 'smtp':
55 // Get settings to be used when constructing the transport object
56 list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server'] ?? '');
57 if ($host === '') {
58 throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp".', 1291068606);
59 }
60 if ($port === null || $port === '') {
61 $port = 25;
62 }
63 $useEncryption = $mailSettings['transport_smtp_encrypt'] ?: null;
64 // Create our transport
65 $transport = \Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
66 // Need authentication?
67 $username = $mailSettings['transport_smtp_username'];
68 if ($username !== '') {
69 $transport->setUsername($username);
70 }
71 $password = $mailSettings['transport_smtp_password'];
72 if ($password !== '') {
73 $transport->setPassword($password);
74 }
75 break;
76 case 'sendmail':
77 $sendmailCommand = $mailSettings['transport_sendmail_command'];
78 if (empty($sendmailCommand)) {
79 throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail".', 1291068620);
80 }
81 // Create our transport
82 $transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
83 break;
84 case 'mbox':
85 $mboxFile = $mailSettings['transport_mbox_file'];
86 if ($mboxFile == '') {
87 throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox".', 1294586645);
88 }
89 // Create our transport
90 $transport = GeneralUtility::makeInstance(MboxTransport::class, $mboxFile);
91 break;
92 case 'mail':
93 // Create the transport, no configuration required
94 $transport = \Swift_MailTransport::newInstance();
95 break;
96 default:
97 // Custom mail transport
98 $customTransport = GeneralUtility::makeInstance($mailSettings['transport'], $mailSettings);
99 if ($customTransport instanceof \Swift_Transport) {
100 $transport = $customTransport;
101 } else {
102 throw new \RuntimeException($mailSettings['transport'] . ' is not an implementation of \\Swift_Transport,
103 but must implement that interface to be used as a mail transport.', 1323006478);
104 }
105 }
106 return $transport;
107 }
108
109 /**
110 * Creates a spool from mail settings.
111 *
112 * @param array $mailSettings
113 * @return \Swift_Spool
114 */
115 protected function createSpool(array $mailSettings): \Swift_Spool
116 {
117 $spool = null;
118 switch ($mailSettings['transport_spool_type']) {
119 case self::SPOOL_FILE:
120 $path = GeneralUtility::getFileAbsFileName($mailSettings['transport_spool_filepath']);
121 if (empty($path) || !file_exists($path) || !is_writable($path)) {
122 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);
123 }
124 $spool = GeneralUtility::makeInstance(\Swift_FileSpool::class, $path);
125 break;
126 case self::SPOOL_MEMORY:
127 $spool = GeneralUtility::makeInstance(MemorySpool::class);
128 break;
129 default:
130 $spool = GeneralUtility::makeInstance($mailSettings['transport_spool_type'], $mailSettings);
131 if (!$spool instanceof \Swift_Spool) {
132 throw new \RuntimeException($mailSettings['spool'] . ' is not an implementation of \\Swift_Spool,
133 but must implement that interface to be used as a mail spool.', 1466799482);
134 }
135 break;
136 }
137 return $spool;
138 }
139 }