‪TYPO3CMS  ‪main
FluidEmail.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Mail;
19 
20 use Psr\Http\Message\ServerRequestInterface;
21 use Symfony\Component\Mime\Email;
22 use Symfony\Component\Mime\Header\Headers;
23 use Symfony\Component\Mime\Part\AbstractPart;
29 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
30 
34 class ‪FluidEmail extends Email
35 {
36  public const ‪FORMAT_HTML = 'html';
37  public const ‪FORMAT_PLAIN = 'plain';
38  public const ‪FORMAT_BOTH = 'both';
39 
43  protected array ‪$format = ['html', 'plain'];
44 
45  protected string ‪$templateName = 'Default';
46 
48  public function ‪__construct(‪TemplatePaths $templatePaths = null, Headers $headers = null, AbstractPart $body = null)
49  {
50  parent::__construct($headers, $body);
51  $this->‪initializeView($templatePaths);
52  }
53 
54  protected function ‪initializeView(‪TemplatePaths $templatePaths = null): void
55  {
56  $templatePaths = $templatePaths ?? new ‪TemplatePaths(‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']);
57  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
58  $this->view->getRenderingContext()->setTemplatePaths($templatePaths);
59  $this->view->assignMultiple($this->‪getDefaultVariables());
60  $this->‪format(‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['format'] ?? self::FORMAT_BOTH);
61  }
62 
63  public function ‪format(string ‪$format): static
64  {
65  $this->‪format = match (‪$format) {
66  self::FORMAT_BOTH => [‪self::FORMAT_HTML, ‪self::FORMAT_PLAIN],
67  self::FORMAT_HTML => [‪self::FORMAT_HTML],
68  self::FORMAT_PLAIN => [‪self::FORMAT_PLAIN],
69  default => throw new \InvalidArgumentException('Setting FluidEmail->format() must be either "html", "plain" or "both", no other formats are currently supported', 1580743847),
70  };
71  $this->‪resetBody();
72  return $this;
73  }
74 
75  public function ‪setTemplate(string ‪$templateName): static
76  {
77  $this->templateName = ‪$templateName;
78  $this->‪resetBody();
79  return $this;
80  }
81 
82  public function ‪assign($key, $value): static
83  {
84  $this->view->assign($key, $value);
85  $this->‪resetBody();
86  return $this;
87  }
88 
89  public function ‪assignMultiple(array $values): static
90  {
91  $this->view->assignMultiple($values);
92  $this->‪resetBody();
93  return $this;
94  }
95 
96  /*
97  * Shorthand setters
98  */
99  public function ‪setRequest(ServerRequestInterface $request): static
100  {
101  $this->view->setRequest($request);
102  $this->view->assign('request', $request);
103  if ($request->getAttribute('normalizedParams') instanceof ‪NormalizedParams) {
104  $this->view->assign('normalizedParams', $request->getAttribute('normalizedParams'));
105  } else {
106  $this->view->assign('normalizedParams', ‪NormalizedParams::createFromServerParams(‪$_SERVER));
107  }
108  $this->‪resetBody();
109  return $this;
110  }
111 
112  protected function ‪getDefaultVariables(): array
113  {
114  return [
115  'typo3' => [
116  'sitename' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
117  'formats' => [
118  'date' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
119  'time' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
120  ],
121  'systemConfiguration' => ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'],
122  'information' => GeneralUtility::makeInstance(Typo3Information::class),
123  ],
124  ];
125  }
126 
127  public function ‪ensureValidity(): void
128  {
129  $this->‪generateTemplatedBody();
130  parent::ensureValidity();
131  }
132 
133  public function ‪getBody(): AbstractPart
134  {
135  $this->‪generateTemplatedBody();
136  return parent::getBody();
137  }
138 
142  public function ‪getHtmlBody(bool $forceBodyGeneration = false)
143  {
144  if ($forceBodyGeneration) {
145  $this->‪generateTemplatedBody('html');
146  } elseif (parent::getHtmlBody() === null) {
147  $this->‪generateTemplatedBody();
148  }
149  return parent::getHtmlBody();
150  }
151 
155  public function ‪getTextBody(bool $forceBodyGeneration = false)
156  {
157  if ($forceBodyGeneration) {
158  $this->‪generateTemplatedBody('plain');
159  } elseif (parent::getTextBody() === null) {
160  $this->‪generateTemplatedBody();
161  }
162  return parent::getTextBody();
163  }
164 
168  public function ‪getViewHelperVariableContainer(): ViewHelperVariableContainer
169  {
170  // the variables are possibly modified in ext:form, so content must be rendered
171  $this->‪resetBody();
172  return $this->view->getRenderingContext()->getViewHelperVariableContainer();
173  }
174 
175  protected function ‪generateTemplatedBody(string $forceFormat = ''): void
176  {
177  // Use a local variable to allow forcing a specific format
178  ‪$format = $forceFormat ? [$forceFormat] : ‪$this->format;
179 
180  $tryToRenderSubjectSection = false;
181  if (in_array(static::FORMAT_HTML, ‪$format, true) && ($forceFormat || parent::getHtmlBody() === null)) {
182  $this->html($this->‪renderContent('html'));
183  $tryToRenderSubjectSection = true;
184  }
185  if (in_array(static::FORMAT_PLAIN, ‪$format, true) && ($forceFormat || parent::getTextBody() === null)) {
186  $this->text(trim($this->‪renderContent('txt')));
187  $tryToRenderSubjectSection = true;
188  }
189 
190  if ($tryToRenderSubjectSection) {
191  $subjectFromTemplate = $this->view->renderSection(
192  'Subject',
193  $this->view->getRenderingContext()->getVariableProvider()->getAll(),
194  true
195  );
196  if (!empty($subjectFromTemplate)) {
197  $this->subject($subjectFromTemplate);
198  }
199  }
200  }
201 
202  protected function ‪renderContent(string ‪$format): string
203  {
204  $this->view->setFormat(‪$format);
205  $this->view->setTemplate($this->templateName);
206  return $this->view->render();
207  }
208 
209  protected function ‪resetBody(): void
210  {
211  $this->html(null);
212  $this->text(null);
213  }
214 }
‪TYPO3\CMS\Fluid\View\TemplatePaths
Definition: TemplatePaths.php:39
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:28
‪TYPO3\CMS\Core\Mail\FluidEmail\assignMultiple
‪assignMultiple(array $values)
Definition: FluidEmail.php:89
‪TYPO3\CMS\Core\Mail\FluidEmail\getHtmlBody
‪resource string null getHtmlBody(bool $forceBodyGeneration=false)
Definition: FluidEmail.php:142
‪TYPO3\CMS\Core\Mail\FluidEmail\FORMAT_BOTH
‪const FORMAT_BOTH
Definition: FluidEmail.php:38
‪TYPO3\CMS\Core\Mail\FluidEmail\initializeView
‪initializeView(TemplatePaths $templatePaths=null)
Definition: FluidEmail.php:54
‪TYPO3\CMS\Core\Mail\FluidEmail\resetBody
‪resetBody()
Definition: FluidEmail.php:209
‪TYPO3\CMS\Core\Mail\FluidEmail\$view
‪StandaloneView $view
Definition: FluidEmail.php:47
‪TYPO3\CMS\Core\Mail\FluidEmail\FORMAT_HTML
‪const FORMAT_HTML
Definition: FluidEmail.php:36
‪TYPO3\CMS\Core\Mail\FluidEmail\assign
‪assign($key, $value)
Definition: FluidEmail.php:82
‪TYPO3\CMS\Core\Mail\FluidEmail\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: FluidEmail.php:99
‪TYPO3\CMS\Core\Mail\FluidEmail\renderContent
‪renderContent(string $format)
Definition: FluidEmail.php:202
‪TYPO3\CMS\Core\Mail\FluidEmail\ensureValidity
‪ensureValidity()
Definition: FluidEmail.php:127
‪TYPO3\CMS\Core\Mail\FluidEmail\getViewHelperVariableContainer
‪getViewHelperVariableContainer()
Definition: FluidEmail.php:168
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Core\Mail\FluidEmail\generateTemplatedBody
‪generateTemplatedBody(string $forceFormat='')
Definition: FluidEmail.php:175
‪TYPO3\CMS\Core\Mail\FluidEmail\__construct
‪__construct(TemplatePaths $templatePaths=null, Headers $headers=null, AbstractPart $body=null)
Definition: FluidEmail.php:48
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromServerParams
‪static static createFromServerParams(array $serverParams, array $systemConfiguration=null)
Definition: NormalizedParams.php:824
‪TYPO3\CMS\Core\Mail\FluidEmail\getBody
‪getBody()
Definition: FluidEmail.php:133
‪TYPO3\CMS\Core\Mail\FluidEmail\format
‪format(string $format)
Definition: FluidEmail.php:63
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Core\Mail\FluidEmail\getTextBody
‪resource string null getTextBody(bool $forceBodyGeneration=false)
Definition: FluidEmail.php:155
‪TYPO3\CMS\Core\Mail\FluidEmail\FORMAT_PLAIN
‪const FORMAT_PLAIN
Definition: FluidEmail.php:37
‪TYPO3\CMS\Core\Mail\FluidEmail\getDefaultVariables
‪getDefaultVariables()
Definition: FluidEmail.php:112
‪TYPO3\CMS\Core\Mail\FluidEmail\$format
‪array $format
Definition: FluidEmail.php:43
‪TYPO3\CMS\Core\Mail\FluidEmail\$templateName
‪string $templateName
Definition: FluidEmail.php:45
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Mail\FluidEmail\setTemplate
‪setTemplate(string $templateName)
Definition: FluidEmail.php:75
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38