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