‪TYPO3CMS  11.5
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 ‪$format = ['html', 'plain'];
44 
48  protected ‪$templateName = 'Default';
49 
53  protected ‪$view;
54 
55  public function ‪__construct(?‪TemplatePaths $templatePaths = null, ?Headers $headers = null, ?AbstractPart $body = null)
56  {
57  parent::__construct($headers, $body);
58  $this->‪initializeView($templatePaths);
59  }
60 
61  protected function ‪initializeView(?‪TemplatePaths $templatePaths = null): void
62  {
63  $templatePaths = $templatePaths ?? new ‪TemplatePaths(‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']);
64  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
65  $this->view->getRenderingContext()->setTemplatePaths($templatePaths);
66  $this->view->assignMultiple($this->‪getDefaultVariables());
67  $this->‪format(‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['format'] ?? self::FORMAT_BOTH);
68  }
69 
70  public function ‪format(string ‪$format)
71  {
72  switch (‪$format) {
75  break;
78  break;
81  break;
82  default:
83  throw new \InvalidArgumentException('Setting FluidEmail->format() must be either "html", "plain" or "both", no other formats are currently supported', 1580743847);
84  }
85  $this->‪resetBody();
86  return $this;
87  }
88 
89  public function ‪setTemplate(string ‪$templateName)
90  {
91  $this->templateName = ‪$templateName;
92  $this->‪resetBody();
93  return $this;
94  }
95 
96  public function ‪assign($key, $value)
97  {
98  $this->view->assign($key, $value);
99  $this->‪resetBody();
100  return $this;
101  }
102 
103  public function ‪assignMultiple(array $values)
104  {
105  $this->view->assignMultiple($values);
106  $this->‪resetBody();
107  return $this;
108  }
109 
110  /*
111  * Shorthand setters
112  */
113  public function ‪setRequest(ServerRequestInterface $request)
114  {
115  $this->view->assign('request', $request);
116  if ($request->getAttribute('normalizedParams') instanceof ‪NormalizedParams) {
117  $this->view->assign('normalizedParams', $request->getAttribute('normalizedParams'));
118  } else {
119  $this->view->assign('normalizedParams', ‪NormalizedParams::createFromServerParams($_SERVER));
120  }
121  $this->‪resetBody();
122  return $this;
123  }
124 
125  protected function ‪getDefaultVariables(): array
126  {
127  return [
128  'typo3' => [
129  'sitename' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
130  'formats' => [
131  'date' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
132  'time' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
133  ],
134  'systemConfiguration' => ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'],
135  'information' => GeneralUtility::makeInstance(Typo3Information::class),
136  ],
137  ];
138  }
139 
140  public function ‪ensureValidity()
141  {
142  $this->‪generateTemplatedBody();
143  parent::ensureValidity();
144  }
145 
146  public function ‪getBody(): AbstractPart
147  {
148  $this->‪generateTemplatedBody();
149  return parent::getBody();
150  }
151 
155  public function ‪getHtmlBody(bool $forceBodyGeneration = false)
156  {
157  if ($forceBodyGeneration) {
158  $this->‪generateTemplatedBody('html');
159  } elseif (parent::getHtmlBody() === null) {
160  $this->‪generateTemplatedBody();
161  }
162  return parent::getHtmlBody();
163  }
164 
168  public function ‪getTextBody(bool $forceBodyGeneration = false)
169  {
170  if ($forceBodyGeneration) {
171  $this->‪generateTemplatedBody('plain');
172  } elseif (parent::getTextBody() === null) {
173  $this->‪generateTemplatedBody();
174  }
175  return parent::getTextBody();
176  }
177 
182  public function ‪getViewHelperVariableContainer(): ViewHelperVariableContainer
183  {
184  // the variables are possibly modified in ext:form, so content must be rendered
185  $this->‪resetBody();
186  return $this->view->getRenderingContext()->getViewHelperVariableContainer();
187  }
188 
189  protected function ‪generateTemplatedBody(string $forceFormat = ''): void
190  {
191  // Use a local variable to allow forcing a specific format
192  ‪$format = $forceFormat ? [$forceFormat] : ‪$this->format;
193 
194  $tryToRenderSubjectSection = false;
195  if (in_array(static::FORMAT_HTML, ‪$format, true) && ($forceFormat || parent::getHtmlBody() === null)) {
196  $this->html($this->‪renderContent('html'));
197  $tryToRenderSubjectSection = true;
198  }
199  if (in_array(static::FORMAT_PLAIN, ‪$format, true) && ($forceFormat || parent::getTextBody() === null)) {
200  $this->text(trim($this->‪renderContent('txt')));
201  $tryToRenderSubjectSection = true;
202  }
203 
204  if ($tryToRenderSubjectSection) {
205  $subjectFromTemplate = $this->view->renderSection(
206  'Subject',
207  $this->view->getRenderingContext()->getVariableProvider()->getAll(),
208  true
209  );
210  if (!empty($subjectFromTemplate)) {
211  $this->subject($subjectFromTemplate);
212  }
213  }
214  }
215 
216  protected function ‪renderContent(string ‪$format): string
217  {
218  $this->view->setFormat(‪$format);
219  $this->view->setTemplate($this->templateName);
220  return $this->view->render();
221  }
222 
223  protected function ‪resetBody(): void
224  {
225  $this->html(null);
226  $this->text(null);
227  }
228 }
‪TYPO3\CMS\Fluid\View\TemplatePaths
Definition: TemplatePaths.php:37
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:28
‪TYPO3\CMS\Core\Mail\FluidEmail\initializeView
‪initializeView(?TemplatePaths $templatePaths=null)
Definition: FluidEmail.php:58
‪TYPO3\CMS\Core\Mail\FluidEmail\__construct
‪__construct(?TemplatePaths $templatePaths=null, ?Headers $headers=null, ?AbstractPart $body=null)
Definition: FluidEmail.php:52
‪TYPO3\CMS\Core\Mail\FluidEmail\assignMultiple
‪assignMultiple(array $values)
Definition: FluidEmail.php:100
‪TYPO3\CMS\Core\Mail\FluidEmail\getHtmlBody
‪resource string null getHtmlBody(bool $forceBodyGeneration=false)
Definition: FluidEmail.php:152
‪TYPO3\CMS\Core\Mail\FluidEmail\FORMAT_BOTH
‪const FORMAT_BOTH
Definition: FluidEmail.php:38
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromServerParams
‪static static createFromServerParams(array $serverParams, ?array $systemConfiguration=null)
Definition: NormalizedParams.php:826
‪TYPO3\CMS\Core\Mail\FluidEmail\resetBody
‪resetBody()
Definition: FluidEmail.php:220
‪TYPO3\CMS\Core\Mail\FluidEmail\$view
‪StandaloneView $view
Definition: FluidEmail.php:50
‪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:93
‪TYPO3\CMS\Core\Mail\FluidEmail\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: FluidEmail.php:110
‪TYPO3\CMS\Core\Mail\FluidEmail\renderContent
‪renderContent(string $format)
Definition: FluidEmail.php:213
‪TYPO3\CMS\Core\Mail\FluidEmail\ensureValidity
‪ensureValidity()
Definition: FluidEmail.php:137
‪TYPO3\CMS\Core\Mail\FluidEmail\$format
‪string[] $format
Definition: FluidEmail.php:42
‪TYPO3\CMS\Core\Mail\FluidEmail\getViewHelperVariableContainer
‪ViewHelperVariableContainer getViewHelperVariableContainer()
Definition: FluidEmail.php:179
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Core\Mail\FluidEmail\generateTemplatedBody
‪generateTemplatedBody(string $forceFormat='')
Definition: FluidEmail.php:186
‪TYPO3\CMS\Core\Mail\FluidEmail\getBody
‪getBody()
Definition: FluidEmail.php:143
‪TYPO3\CMS\Core\Mail\FluidEmail\format
‪format(string $format)
Definition: FluidEmail.php:67
‪TYPO3\CMS\Core\Mail\FluidEmail\getTextBody
‪resource string null getTextBody(bool $forceBodyGeneration=false)
Definition: FluidEmail.php:165
‪TYPO3\CMS\Core\Mail\FluidEmail\FORMAT_PLAIN
‪const FORMAT_PLAIN
Definition: FluidEmail.php:37
‪TYPO3\CMS\Core\Mail\FluidEmail\getDefaultVariables
‪getDefaultVariables()
Definition: FluidEmail.php:122
‪TYPO3\CMS\Core\Mail\FluidEmail\$templateName
‪string $templateName
Definition: FluidEmail.php:46
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$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:86
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35