‪TYPO3CMS  10.4
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  return $this;
86  }
87 
88  public function ‪setTemplate(string ‪$templateName)
89  {
90  $this->templateName = ‪$templateName;
91  return $this;
92  }
93 
94  public function ‪assign($key, $value)
95  {
96  $this->view->assign($key, $value);
97  return $this;
98  }
99 
100  public function ‪assignMultiple(array $values)
101  {
102  $this->view->assignMultiple($values);
103  return $this;
104  }
105 
106  /*
107  * Shorthand setters
108  */
109  public function ‪setRequest(ServerRequestInterface $request): self
110  {
111  $this->view->assign('request', $request);
112  if ($request->getAttribute('normalizedParams') instanceof ‪NormalizedParams) {
113  $this->view->assign('normalizedParams', $request->getAttribute('normalizedParams'));
114  } else {
115  $this->view->assign('normalizedParams', ‪NormalizedParams::createFromServerParams($_SERVER));
116  }
117  return $this;
118  }
119 
120  protected function ‪getDefaultVariables(): array
121  {
122  return [
123  'typo3' => [
124  'sitename' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
125  'formats' => [
126  'date' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
127  'time' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']
128  ],
129  'systemConfiguration' => ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'],
130  'information' => GeneralUtility::makeInstance(Typo3Information::class),
131  ]
132  ];
133  }
134 
135  public function ‪ensureValidity()
136  {
137  $this->‪generateTemplatedBody();
138  parent::ensureValidity();
139  }
140 
141  public function ‪getBody(): AbstractPart
142  {
143  $this->‪generateTemplatedBody();
144  return parent::getBody();
145  }
146 
151  public function ‪getViewHelperVariableContainer(): ViewHelperVariableContainer
152  {
153  return $this->view->getRenderingContext()->getViewHelperVariableContainer();
154  }
155 
156  protected function ‪generateTemplatedBody(): void
157  {
158  if (!$this->view) {
159  $this->‪initializeView();
160  }
161  if (in_array(static::FORMAT_HTML, $this->‪format, true)) {
162  $this->html($this->‪renderContent('html'));
163  }
164  if (in_array(static::FORMAT_PLAIN, $this->‪format, true)) {
165  $this->text(trim($this->‪renderContent('txt')));
166  }
167 
168  $subjectFromTemplate = $this->view->renderSection(
169  'Subject',
170  $this->view->getRenderingContext()->getVariableProvider()->getAll(),
171  true
172  );
173  if (!empty($subjectFromTemplate)) {
174  $this->subject($subjectFromTemplate);
175  }
176  }
177 
178  protected function ‪renderContent(string ‪$format): string
179  {
180  $this->view->setFormat(‪$format);
181  $this->view->setTemplate($this->templateName);
182  return $this->view->render();
183  }
184 }
‪TYPO3\CMS\Fluid\View\TemplatePaths
Definition: TemplatePaths.php:35
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:26
‪TYPO3\CMS\Core\Mail\FluidEmail\assignMultiple
‪assignMultiple(array $values)
Definition: FluidEmail.php:97
‪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:58
‪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:91
‪TYPO3\CMS\Core\Mail\FluidEmail\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: FluidEmail.php:106
‪TYPO3\CMS\Core\Mail\FluidEmail\renderContent
‪renderContent(string $format)
Definition: FluidEmail.php:175
‪TYPO3\CMS\Core\Mail\FluidEmail\ensureValidity
‪ensureValidity()
Definition: FluidEmail.php:132
‪TYPO3\CMS\Core\Mail\FluidEmail\$format
‪string[] $format
Definition: FluidEmail.php:42
‪TYPO3\CMS\Core\Mail\FluidEmail\getViewHelperVariableContainer
‪ViewHelperVariableContainer getViewHelperVariableContainer()
Definition: FluidEmail.php:148
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Core\Mail\FluidEmail\__construct
‪__construct(TemplatePaths $templatePaths=null, Headers $headers=null, AbstractPart $body=null)
Definition: FluidEmail.php:52
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromServerParams
‪static static createFromServerParams(array $serverParams, array $systemConfiguration=null)
Definition: NormalizedParams.php:831
‪TYPO3\CMS\Core\Mail\FluidEmail\getBody
‪getBody()
Definition: FluidEmail.php:138
‪TYPO3\CMS\Core\Mail\FluidEmail\format
‪format(string $format)
Definition: FluidEmail.php:67
‪TYPO3\CMS\Core\Mail\FluidEmail\FORMAT_PLAIN
‪const FORMAT_PLAIN
Definition: FluidEmail.php:37
‪TYPO3\CMS\Core\Mail\FluidEmail\getDefaultVariables
‪getDefaultVariables()
Definition: FluidEmail.php:117
‪TYPO3\CMS\Core\Mail\FluidEmail\$templateName
‪string $templateName
Definition: FluidEmail.php:46
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Mail\FluidEmail\setTemplate
‪setTemplate(string $templateName)
Definition: FluidEmail.php:85
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Mail\FluidEmail\generateTemplatedBody
‪generateTemplatedBody()
Definition: FluidEmail.php:153
‪TYPO3\CMS\Core\Mail
Definition: DelayedTransportInterface.php:18
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35