‪TYPO3CMS  11.5
Mailer.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Mail;
17 
18 use Psr\EventDispatcher\EventDispatcherInterface;
19 use Symfony\Component\Mailer\Envelope;
20 use Symfony\Component\Mailer\MailerInterface;
21 use Symfony\Component\Mailer\SentMessage;
22 use Symfony\Component\Mailer\Transport\TransportInterface;
23 use Symfony\Component\Mime\Address;
24 use Symfony\Component\Mime\Email;
25 use Symfony\Component\Mime\RawMessage;
30 
37 class ‪Mailer implements MailerInterface
38 {
42  protected ‪$transport;
43 
47  protected ‪$mailSettings = [];
48 
52  protected ‪$sentMessage;
53 
57  protected ‪$mailerHeader = 'TYPO3';
58 
62  protected ‪$eventDispatcher;
63 
71  public function ‪__construct(?TransportInterface ‪$transport = null, ?EventDispatcherInterface ‪$eventDispatcher = null)
72  {
73  if (‪$transport !== null) {
74  $this->transport = ‪$transport;
75  } else {
76  if (empty($this->mailSettings)) {
77  $this->‪injectMailSettings();
78  }
79  try {
80  $this->‪initializeTransport();
81  } catch (\‪Exception $e) {
82  throw new ‪CoreException($e->getMessage(), 1291068569);
83  }
84  }
85  if (‪$eventDispatcher !== null) {
86  $this->eventDispatcher = ‪$eventDispatcher;
87  $this->eventDispatcher->dispatch(new ‪AfterMailerInitializationEvent($this));
88  }
89  }
90 
94  public function ‪send(RawMessage $message, ?Envelope $envelope = null): void
95  {
96  if ($message instanceof Email) {
97  // Ensure to always have a From: header set
98  if (empty($message->getFrom())) {
100  if ($address) {
102  if ($name) {
103  $from = new Address($address, $name);
104  } else {
105  $from = new Address($address);
106  }
107  $message->from($from);
108  }
109  }
110  if (empty($message->getReplyTo())) {
111  $replyTo = MailUtility::getSystemReplyTo();
112  if (!empty($replyTo)) {
113  $address = key($replyTo);
114  if ($address === 0) {
115  $replyTo = new Address($replyTo[$address]);
116  } else {
117  $replyTo = new Address((string)$address, reset($replyTo));
118  }
119  $message->replyTo($replyTo);
120  }
121  }
122  // Only set X-Mailer header once, if message is re-used
123  if (!$message->getHeaders()->has('X-Mailer')) {
124  $message->getHeaders()->addTextHeader('X-Mailer', $this->mailerHeader);
125  }
126  }
127 
128  $this->sentMessage = $this->transport->send($message, $envelope);
129  }
130 
131  public function ‪getSentMessage(): ?SentMessage
132  {
133  return ‪$this->sentMessage;
134  }
135 
136  public function ‪getTransport(): TransportInterface
137  {
138  return ‪$this->transport;
139  }
140 
157  private function ‪initializeTransport()
158  {
159  $this->transport = $this->‪getTransportFactory()->‪get($this->mailSettings);
160  }
161 
168  public function ‪injectMailSettings(?array ‪$mailSettings = null)
169  {
170  if (is_array(‪$mailSettings)) {
171  $this->mailSettings = ‪$mailSettings;
172  } else {
173  $this->mailSettings = (array)‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
174  }
175  }
176 
182  public function ‪getRealTransport(): TransportInterface
183  {
184  ‪$mailSettings = !empty($this->mailSettings) ? $this->mailSettings : (array)‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
185  unset(‪$mailSettings['transport_spool_type']);
187  }
188 
192  protected function ‪getTransportFactory(): ‪TransportFactory
193  {
194  return GeneralUtility::makeInstance(TransportFactory::class);
195  }
196 }
‪TYPO3\CMS\Core\Mail\Mailer\$mailSettings
‪array $mailSettings
Definition: Mailer.php:45
‪TYPO3\CMS\Core\Mail\Event\AfterMailerInitializationEvent
Definition: AfterMailerInitializationEvent.php:27
‪TYPO3\CMS\Core\Mail\TransportFactory\get
‪TransportInterface get(array $mailSettings)
Definition: TransportFactory.php:66
‪TYPO3\CMS\Core\Mail\Mailer\getTransport
‪getTransport()
Definition: Mailer.php:131
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Mail\Mailer\$sentMessage
‪SentMessage null $sentMessage
Definition: Mailer.php:49
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:78
‪TYPO3\CMS\Core\Mail\Mailer\getSentMessage
‪getSentMessage()
Definition: Mailer.php:126
‪TYPO3\CMS\Core\Mail\Mailer\getTransportFactory
‪TransportFactory getTransportFactory()
Definition: Mailer.php:187
‪TYPO3\CMS\Core\Mail\Mailer\getRealTransport
‪TransportInterface getRealTransport()
Definition: Mailer.php:177
‪TYPO3\CMS\Core\Mail\Mailer\$mailerHeader
‪string $mailerHeader
Definition: Mailer.php:53
‪TYPO3\CMS\Core\Mail\Mailer\initializeTransport
‪initializeTransport()
Definition: Mailer.php:152
‪TYPO3\CMS\Core\Mail\Mailer\send
‪send(RawMessage $message, ?Envelope $envelope=null)
Definition: Mailer.php:89
‪TYPO3\CMS\Core\Mail\Mailer\injectMailSettings
‪injectMailSettings(?array $mailSettings=null)
Definition: Mailer.php:163
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:24
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Core\Mail\Mailer\__construct
‪__construct(?TransportInterface $transport=null, ?EventDispatcherInterface $eventDispatcher=null)
Definition: Mailer.php:66
‪TYPO3\CMS\Core\Mail\Mailer\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: Mailer.php:57
‪TYPO3\CMS\Core\Mail\Mailer\$transport
‪TransportInterface $transport
Definition: Mailer.php:41
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromName
‪static string null getSystemFromName()
Definition: MailUtility.php:57
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:38