[FEATURE] Integrate Swift Mailer's spool transport
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / TransportFactory.php
1 <?php
2
3 namespace TYPO3\CMS\Core\Mail;
4
5 /* *
6 * This script is part of the TYPO3 project - inspiring people to share! *
7 * *
8 * TYPO3 is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU General Public License version 3 as published by *
10 * the Free Software Foundation. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
15 * Public License for more details. *
16 * */
17
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * TransportFactory
22 */
23 class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
24 {
25 const SPOOL_MEMORY = 'memory';
26 const SPOOL_FILE = 'file';
27
28 /**
29 * Gets a transport from settings.
30 *
31 * @param array $mailSettings from $GLOBALS['TYPO3_CONF_VARS']['MAIL']
32 * @return \Swift_Transport
33 * @throws \TYPO3\CMS\Core\Exception
34 * @throws \RuntimeException
35 */
36 public function get(array $mailSettings): \Swift_Transport
37 {
38 if (!isset($mailSettings['transport'])) {
39 throw new \InvalidArgumentException('Key "transport" must be set in the mail settings', 1469363365);
40 }
41 if ($mailSettings['transport'] === 'spool') {
42 throw new \InvalidArgumentException('Mail transport can not be set to "spool"', 1469363238);
43 }
44
45 $transport = null;
46 $transportType = (isset($mailSettings['transport_spool_type']) && !empty($mailSettings['transport_spool_type'])) ? 'spool': $mailSettings['transport'];
47
48 switch ($transportType) {
49 case 'spool':
50 $transport = \Swift_SpoolTransport::newInstance($this->createSpool($mailSettings));
51 break;
52 case 'smtp':
53 // Get settings to be used when constructing the transport object
54 list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
55 if ($host === '') {
56 throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"', 1291068606);
57 }
58 if ($port === null || $port === '') {
59 $port = '25';
60 }
61 $useEncryption = $mailSettings['transport_smtp_encrypt'] ?: null;
62 // Create our transport
63 $transport = \Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
64 // Need authentication?
65 $username = $mailSettings['transport_smtp_username'];
66 if ($username !== '') {
67 $transport->setUsername($username);
68 }
69 $password = $mailSettings['transport_smtp_password'];
70 if ($password !== '') {
71 $transport->setPassword($password);
72 }
73 break;
74 case 'sendmail':
75 $sendmailCommand = $mailSettings['transport_sendmail_command'];
76 if (empty($sendmailCommand)) {
77 throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"', 1291068620);
78 }
79 // Create our transport
80 $transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
81 break;
82 case 'mbox':
83 $mboxFile = $mailSettings['transport_mbox_file'];
84 if ($mboxFile == '') {
85 throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
86 }
87 // Create our transport
88 $transport = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MboxTransport::class, $mboxFile);
89 break;
90 case 'mail':
91 // Create the transport, no configuration required
92 $transport = \Swift_MailTransport::newInstance();
93 break;
94 default:
95 // Custom mail transport
96 $customTransport = GeneralUtility::makeInstance($mailSettings['transport'], $mailSettings);
97 if ($customTransport instanceof \Swift_Transport) {
98 $transport = $customTransport;
99 } else {
100 throw new \RuntimeException($mailSettings['transport'] . ' is not an implementation of \\Swift_Transport,
101 but must implement that interface to be used as a mail transport.', 1323006478);
102 }
103 }
104 return $transport;
105 }
106
107 /**
108 * Creates a spool from mail settings.
109 *
110 * @param array $mailSettings
111 * @return \Swift_Spool
112 */
113 protected function createSpool(array $mailSettings): \Swift_Spool
114 {
115 $spool = null;
116 switch ($mailSettings['transport_spool_type']) {
117 case self::SPOOL_FILE:
118 $path = GeneralUtility::getFileAbsFileName($mailSettings['transport_spool_filepath']);
119 $spool = GeneralUtility::makeInstance(\Swift_FileSpool::class, $path);
120 break;
121 case self::SPOOL_MEMORY:
122 $spool = GeneralUtility::makeInstance(MemorySpool::class);
123 break;
124 default:
125 $spool = GeneralUtility::makeInstance($mailSettings['transport_spool_type'], $mailSettings);
126 if (!($spool instanceof \Swift_Spool)) {
127 throw new \RuntimeException($mailSettings['spool'] . ' is not an implementation of \\Swift_Spool,
128 but must implement that interface to be used as a mail spool.', 1466799482);
129 }
130 break;
131 }
132 return $spool;
133 }
134 }