[CLEANUP] Alwas put null at the last position
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / Mailer.php
1 <?php
2 namespace TYPO3\CMS\Core\Mail;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Object\ObjectManager;
19 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
20
21 /**
22 * Adapter for Swift_Mailer to be used by TYPO3 extensions.
23 *
24 * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
25 * for it to work out-of-the-box.
26 */
27 class Mailer extends \Swift_Mailer
28 {
29 /**
30 * @var \Swift_Transport
31 */
32 protected $transport;
33
34 /**
35 * @var array
36 */
37 protected $mailSettings = [];
38
39 /**
40 * When constructing, also initializes the \Swift_Transport like configured
41 *
42 * @param \Swift_Transport|null $transport optionally pass a transport to the constructor.
43 * @throws \TYPO3\CMS\Core\Exception
44 */
45 public function __construct(\Swift_Transport $transport = null)
46 {
47 if ($transport !== null) {
48 $this->transport = $transport;
49 } else {
50 if (empty($this->mailSettings)) {
51 $this->injectMailSettings();
52 }
53 try {
54 $this->initializeTransport();
55 } catch (\Exception $e) {
56 throw new \TYPO3\CMS\Core\Exception($e->getMessage(), 1291068569);
57 }
58 }
59 parent::__construct($this->transport);
60
61 $this->emitPostInitializeMailerSignal();
62 }
63
64 /**
65 * Prepares a transport using the TYPO3_CONF_VARS configuration
66 *
67 * Used options:
68 * $TYPO3_CONF_VARS['MAIL']['transport'] = 'smtp' | 'sendmail' | 'mail' | 'mbox'
69 *
70 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_server'] = 'smtp.example.org';
71 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_port'] = '25';
72 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_encrypt'] = FALSE; # requires openssl in PHP
73 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_username'] = 'username';
74 * $TYPO3_CONF_VARS['MAIL']['transport_smtp_password'] = 'password';
75 *
76 * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
77 *
78 * @throws \TYPO3\CMS\Core\Exception
79 * @throws \RuntimeException
80 */
81 private function initializeTransport()
82 {
83 switch ($this->mailSettings['transport']) {
84 case 'smtp':
85 // Get settings to be used when constructing the transport object
86 list($host, $port) = preg_split('/:/', $this->mailSettings['transport_smtp_server']);
87 if ($host === '') {
88 throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"', 1291068606);
89 }
90 if ($port === null || $port === '') {
91 $port = '25';
92 }
93 $useEncryption = $this->mailSettings['transport_smtp_encrypt'] ?: null;
94 // Create our transport
95 $this->transport = \Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
96 // Need authentication?
97 $username = $this->mailSettings['transport_smtp_username'];
98 if ($username !== '') {
99 $this->transport->setUsername($username);
100 }
101 $password = $this->mailSettings['transport_smtp_password'];
102 if ($password !== '') {
103 $this->transport->setPassword($password);
104 }
105 break;
106 case 'sendmail':
107 $sendmailCommand = $this->mailSettings['transport_sendmail_command'];
108 if (empty($sendmailCommand)) {
109 throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"', 1291068620);
110 }
111 // Create our transport
112 $this->transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
113 break;
114 case 'mbox':
115 $mboxFile = $this->mailSettings['transport_mbox_file'];
116 if ($mboxFile == '') {
117 throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
118 }
119 // Create our transport
120 $this->transport = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MboxTransport::class, $mboxFile);
121 break;
122 case 'mail':
123 // Create the transport, no configuration required
124 $this->transport = \Swift_MailTransport::newInstance();
125 break;
126 default:
127 // Custom mail transport
128 $customTransport = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($this->mailSettings['transport'], $this->mailSettings);
129 if ($customTransport instanceof \Swift_Transport) {
130 $this->transport = $customTransport;
131 } else {
132 throw new \RuntimeException($this->mailSettings['transport'] . ' is not an implementation of \\Swift_Transport,
133 but must implement that interface to be used as a mail transport.', 1323006478);
134 }
135 }
136 }
137
138 /**
139 * This method is only used in unit tests
140 *
141 * @param array $mailSettings
142 * @access private
143 */
144 public function injectMailSettings(array $mailSettings = null)
145 {
146 if (is_array($mailSettings)) {
147 $this->mailSettings = $mailSettings;
148 } else {
149 $this->mailSettings = (array)$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
150 }
151 }
152
153 /**
154 * Get the object manager
155 *
156 * @return \TYPO3\CMS\Extbase\Object\ObjectManager
157 */
158 protected function getObjectManager()
159 {
160 return GeneralUtility::makeInstance(ObjectManager::class);
161 }
162
163 /**
164 * Get the SignalSlot dispatcher
165 *
166 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
167 */
168 protected function getSignalSlotDispatcher()
169 {
170 return $this->getObjectManager()->get(Dispatcher::class);
171 }
172
173 /**
174 * Emits a signal after mailer initialization
175 */
176 protected function emitPostInitializeMailerSignal()
177 {
178 $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Mail\\Mailer', 'postInitializeMailer', [$this]);
179 }
180 }