TYPO3 CMS  TYPO3_7-6
Mailer.php
Go to the documentation of this file.
1 <?php
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 
20 
27 class Mailer extends \Swift_Mailer
28 {
32  protected $transport;
33 
37  protected $mailSettings = [];
38 
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 
62  }
63 
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 
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 
158  protected function getObjectManager()
159  {
160  return GeneralUtility::makeInstance(ObjectManager::class);
161  }
162 
168  protected function getSignalSlotDispatcher()
169  {
170  return $this->getObjectManager()->get(Dispatcher::class);
171  }
172 
178  protected function emitPostInitializeMailerSignal()
179  {
180  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Mail\\Mailer', 'postInitializeMailer', [$this]);
181  }
182 }
injectMailSettings(array $mailSettings=null)
Definition: Mailer.php:144
__construct(\Swift_Transport $transport=null)
Definition: Mailer.php:45
$host
Definition: server.php:37
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']