‪TYPO3CMS  ‪main
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\SentMessage;
21 use Symfony\Component\Mailer\Transport\TransportInterface;
22 use Symfony\Component\Mime\Address;
23 use Symfony\Component\Mime\Email;
24 use Symfony\Component\Mime\RawMessage;
31 
38 class ‪Mailer implements ‪MailerInterface
39 {
40  protected array ‪$mailSettings = [];
41 
42  protected ?SentMessage ‪$sentMessage;
43 
47  protected string ‪$mailerHeader = 'TYPO3';
48 
55  public function ‪__construct(
56  protected ?TransportInterface $transport = null,
57  protected readonly ?EventDispatcherInterface $eventDispatcher = null,
58  ) {
59  if (empty($this->mailSettings)) {
60  $this->‪injectMailSettings();
61  }
62 
63  try {
64  $this->‪initializeTransport();
65  } catch (\‪Exception $e) {
66  throw new ‪CoreException($e->getMessage(), 1291068569);
67  }
68 
69  $this->eventDispatcher?->dispatch(new ‪AfterMailerInitializationEvent($this));
70  }
71 
72  public function ‪send(RawMessage $message, Envelope $envelope = null): void
73  {
74  if ($message instanceof Email) {
75  // Ensure to always have a From: header set
76  if (empty($message->getFrom())) {
78  if ($address) {
80  if ($name) {
81  $from = new Address($address, $name);
82  } else {
83  $from = new Address($address);
84  }
85  $message->from($from);
86  }
87  }
88  if (empty($message->getReplyTo())) {
89  $replyTo = MailUtility::getSystemReplyTo();
90  if (!empty($replyTo)) {
91  $address = key($replyTo);
92  if ($address === 0) {
93  $replyTo = new Address($replyTo[$address]);
94  } else {
95  $replyTo = new Address((string)$address, reset($replyTo));
96  }
97  $message->replyTo($replyTo);
98  }
99  }
100  // Only set X-Mailer header once, if message is re-used
101  if (!$message->getHeaders()->has('X-Mailer')) {
102  $message->getHeaders()->addTextHeader('X-Mailer', $this->mailerHeader);
103  }
104  }
105 
106  // After static enrichment took place, allow listeners to further manipulate message and envelope
107  $event = new ‪BeforeMailerSentMessageEvent($this, $message, $envelope);
108  $this->eventDispatcher?->dispatch($event);
109 
110  // Send message using the defined transport, with message and envelope from the event
111  $this->sentMessage = $this->transport->send($event->getMessage(), $event->getEnvelope());
112 
113  // Finally, allow further processing by listeners after the message has been sent
114  $this->eventDispatcher?->dispatch(new ‪AfterMailerSentMessageEvent($this));
115  }
116 
117  public function ‪getSentMessage(): ?SentMessage
118  {
119  return ‪$this->sentMessage;
120  }
121 
122  public function ‪getTransport(): TransportInterface
123  {
124  return $this->transport;
125  }
126 
143  private function ‪initializeTransport()
144  {
145  $this->transport ??= $this->‪getTransportFactory()->get($this->mailSettings);
146  }
147 
153  public function ‪injectMailSettings(array ‪$mailSettings = null)
154  {
155  $this->mailSettings = ‪$mailSettings ?? (array)‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
156  }
157 
161  public function ‪getRealTransport(): TransportInterface
162  {
163  ‪$mailSettings = !empty($this->mailSettings) ? $this->mailSettings : (array)‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
164  unset(‪$mailSettings['transport_spool_type']);
165  return $this->‪getTransportFactory()->get($mailSettings);
166  }
167 
169  {
170  return GeneralUtility::makeInstance(TransportFactory::class);
171  }
172 }
‪TYPO3\CMS\Core\Mail\Mailer\getTransportFactory
‪getTransportFactory()
Definition: Mailer.php:168
‪TYPO3\CMS\Core\Mail\Mailer\$mailSettings
‪array $mailSettings
Definition: Mailer.php:40
‪TYPO3\CMS\Core\Mail\Event\AfterMailerInitializationEvent
Definition: AfterMailerInitializationEvent.php:27
‪TYPO3\CMS\Core\Mail\Mailer\getTransport
‪getTransport()
Definition: Mailer.php:122
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Mail\Mailer\injectMailSettings
‪injectMailSettings(array $mailSettings=null)
Definition: Mailer.php:153
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:79
‪TYPO3\CMS\Core\Mail\MailerInterface
Definition: MailerInterface.php:28
‪TYPO3\CMS\Core\Mail\Mailer\getSentMessage
‪getSentMessage()
Definition: Mailer.php:117
‪TYPO3\CMS\Core\Mail\Mailer\$mailerHeader
‪string $mailerHeader
Definition: Mailer.php:47
‪TYPO3\CMS\Core\Mail\Mailer\initializeTransport
‪initializeTransport()
Definition: Mailer.php:143
‪TYPO3\CMS\Core\Mail\Event\AfterMailerSentMessageEvent
Definition: AfterMailerSentMessageEvent.php:31
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:26
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:39
‪TYPO3\CMS\Core\Mail\Mailer\send
‪send(RawMessage $message, Envelope $envelope=null)
Definition: Mailer.php:72
‪TYPO3\CMS\Core\Mail\Mailer\$sentMessage
‪SentMessage $sentMessage
Definition: Mailer.php:42
‪TYPO3\CMS\Core\Mail\Mailer\getRealTransport
‪getRealTransport()
Definition: Mailer.php:161
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Mail\Mailer\__construct
‪__construct(protected ?TransportInterface $transport=null, protected readonly ?EventDispatcherInterface $eventDispatcher=null,)
Definition: Mailer.php:55
‪TYPO3\CMS\Core\Mail\Event\BeforeMailerSentMessageEvent
Definition: BeforeMailerSentMessageEvent.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromName
‪static string null getSystemFromName()
Definition: MailUtility.php:59
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:38