‪TYPO3CMS  10.4
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())) {
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  $message->getHeaders()->addTextHeader('X-Mailer', $this->mailerHeader);
123  }
124 
125  $this->sentMessage = $this->transport->send($message, $envelope);
126  }
127 
128  public function ‪getSentMessage(): ?SentMessage
129  {
130  return ‪$this->sentMessage;
131  }
132 
133  public function ‪getTransport(): TransportInterface
134  {
135  return ‪$this->transport;
136  }
137 
154  private function ‪initializeTransport()
155  {
156  $this->transport = $this->‪getTransportFactory()->‪get($this->mailSettings);
157  }
158 
165  public function ‪injectMailSettings(array ‪$mailSettings = null)
166  {
167  if (is_array(‪$mailSettings)) {
168  $this->mailSettings = ‪$mailSettings;
169  } else {
170  $this->mailSettings = (array)‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
171  }
172  }
173 
179  public function ‪getRealTransport(): TransportInterface
180  {
181  ‪$mailSettings = !empty($this->mailSettings) ? $this->mailSettings : (array)‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
182  unset(‪$mailSettings['transport_spool_type']);
184  }
185 
189  protected function ‪getTransportFactory(): ‪TransportFactory
190  {
191  return GeneralUtility::makeInstance(TransportFactory::class);
192  }
193 }
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemReplyTo
‪static array getSystemReplyTo()
Definition: MailUtility.php:95
‪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:65
‪TYPO3\CMS\Core\Mail\Mailer\getTransport
‪getTransport()
Definition: Mailer.php:128
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Mail\Mailer\$sentMessage
‪SentMessage null $sentMessage
Definition: Mailer.php:49
‪TYPO3\CMS\Core\Mail\Mailer\injectMailSettings
‪injectMailSettings(array $mailSettings=null)
Definition: Mailer.php:160
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:73
‪TYPO3\CMS\Core\Mail\Mailer\getSentMessage
‪getSentMessage()
Definition: Mailer.php:123
‪TYPO3\CMS\Core\Mail\Mailer\getTransportFactory
‪TransportFactory getTransportFactory()
Definition: Mailer.php:184
‪TYPO3\CMS\Core\Mail\Mailer\getRealTransport
‪TransportInterface getRealTransport()
Definition: Mailer.php:174
‪TYPO3\CMS\Core\Mail\Mailer\$mailerHeader
‪string $mailerHeader
Definition: Mailer.php:53
‪TYPO3\CMS\Core\Mail\Mailer\initializeTransport
‪initializeTransport()
Definition: Mailer.php:149
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:24
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Core\Mail\Mailer\send
‪send(RawMessage $message, Envelope $envelope=null)
Definition: Mailer.php:89
‪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:5
‪TYPO3\CMS\Core\Mail\Mailer\__construct
‪__construct(TransportInterface $transport=null, EventDispatcherInterface $eventDispatcher=null)
Definition: Mailer.php:66
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromName
‪static string getSystemFromName()
Definition: MailUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:37