‪TYPO3CMS  9.5
TransportFactory.php
Go to the documentation of this file.
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 
21 
26 {
27  const ‪SPOOL_MEMORY = 'memory';
28  const ‪SPOOL_FILE = 'file';
29 
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  if (isset($mailSettings['transport_smtp_server']) && strpos($mailSettings['transport_smtp_server'], ':') > 0) {
57  $parts = GeneralUtility::trimExplode(':', $mailSettings['transport_smtp_server'], true);
58  $host = $parts[0];
59  $port = $parts[1] ?? null;
60  } else {
61  $host = (string)($mailSettings['transport_smtp_server'] ?? '');
62  $port = null;
63  }
64 
65  if ($host === '') {
66  throw new ‪Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp".', 1291068606);
67  }
68  if ($port === null || $port === '') {
69  $port = 25;
70  }
71  $useEncryption = $mailSettings['transport_smtp_encrypt'] ?? null;
72  // Create our transport
73  $transport = \Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
74  // Need authentication?
75  $username = (string)($mailSettings['transport_smtp_username'] ?? '');
76  if ($username !== '') {
77  $transport->setUsername($username);
78  }
79  $password = (string)($mailSettings['transport_smtp_password'] ?? '');
80  if ($password !== '') {
81  $transport->setPassword($password);
82  }
83  break;
84  case 'sendmail':
85  $sendmailCommand = $mailSettings['transport_sendmail_command'];
86  if (empty($sendmailCommand)) {
87  throw new ‪Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail".', 1291068620);
88  }
89  // Create our transport
90  $transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
91  break;
92  case 'mbox':
93  $mboxFile = $mailSettings['transport_mbox_file'];
94  if ($mboxFile == '') {
95  throw new ‪Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox".', 1294586645);
96  }
97  // Create our transport
98  $transport = GeneralUtility::makeInstance(MboxTransport::class, $mboxFile);
99  break;
100  case 'mail':
101  // Create the transport, no configuration required
102  $transport = \Swift_MailTransport::newInstance();
103  break;
104  default:
105  // Custom mail transport
106  $customTransport = GeneralUtility::makeInstance($mailSettings['transport'], $mailSettings);
107  if ($customTransport instanceof \Swift_Transport) {
108  $transport = $customTransport;
109  } else {
110  throw new \RuntimeException($mailSettings['transport'] . ' is not an implementation of \\Swift_Transport,
111  but must implement that interface to be used as a mail transport.', 1323006478);
112  }
113  }
114  return $transport;
115  }
116 
124  protected function ‪createSpool(array $mailSettings): \Swift_Spool
125  {
126  $spool = null;
127  switch ($mailSettings['transport_spool_type']) {
128  case ‪self::SPOOL_FILE:
129  $path = GeneralUtility::getFileAbsFileName($mailSettings['transport_spool_filepath']);
130  if (empty($path) || !file_exists($path) || !is_writable($path)) {
131  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);
132  }
133  $spool = GeneralUtility::makeInstance(\Swift_FileSpool::class, $path);
134  break;
136  $spool = GeneralUtility::makeInstance(MemorySpool::class);
137  break;
138  default:
139  $spool = GeneralUtility::makeInstance($mailSettings['transport_spool_type'], $mailSettings);
140  if (!$spool instanceof \Swift_Spool) {
141  throw new \RuntimeException(
142  $mailSettings['transport_spool_type'] . ' is not an implementation of \\Swift_Spool,
143  but must implement that interface to be used as a mail spool.',
144  1466799482
145  );
146  }
147  break;
148  }
149  return $spool;
150  }
151 }
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Mail\TransportFactory\SPOOL_FILE
‪const SPOOL_FILE
Definition: TransportFactory.php:28
‪TYPO3\CMS\Core\Mail\TransportFactory\createSpool
‪Swift_Spool createSpool(array $mailSettings)
Definition: TransportFactory.php:124
‪TYPO3\CMS\Core\Mail\TransportFactory\SPOOL_MEMORY
‪const SPOOL_MEMORY
Definition: TransportFactory.php:27
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Mail
Definition: Mailer.php:2
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:26