|
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; |
|
24
|
|
|
use TYPO3\CMS\Core\Http\NormalizedParams; |
|
25
|
|
|
use TYPO3\CMS\Core\Information\Typo3Information; |
|
26
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
27
|
|
|
use TYPO3\CMS\Fluid\View\StandaloneView; |
|
28
|
|
|
use TYPO3\CMS\Fluid\View\TemplatePaths; |
|
29
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Send out templated HTML/plain text emails with Fluid. |
|
33
|
|
|
*/ |
|
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
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string[] |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $format = ['html', 'plain']; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $templateName = 'Default'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var StandaloneView |
|
52
|
|
|
*/ |
|
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) { |
|
73
|
|
|
case self::FORMAT_BOTH: |
|
74
|
|
|
$this->format = [self::FORMAT_HTML, self::FORMAT_PLAIN]; |
|
75
|
|
|
break; |
|
76
|
|
|
case self::FORMAT_HTML: |
|
77
|
|
|
$this->format = [self::FORMAT_HTML]; |
|
78
|
|
|
break; |
|
79
|
|
|
case self::FORMAT_PLAIN: |
|
80
|
|
|
$this->format = [self::FORMAT_PLAIN]; |
|
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
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return ViewHelperVariableContainer |
|
149
|
|
|
* @internal Only used for ext:form, not part of TYPO3 Core API. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getViewHelperVariableContainer(): ViewHelperVariableContainer |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->view->getRenderingContext()->getViewHelperVariableContainer(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
protected function generateTemplatedBody(): void |
|
157
|
|
|
{ |
|
158
|
|
|
if (in_array(static::FORMAT_HTML, $this->format, true)) { |
|
159
|
|
|
$this->html($this->renderContent('html')); |
|
160
|
|
|
} |
|
161
|
|
|
if (in_array(static::FORMAT_PLAIN, $this->format, true)) { |
|
162
|
|
|
$this->text(trim($this->renderContent('txt'))); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$subjectFromTemplate = $this->view->renderSection( |
|
166
|
|
|
'Subject', |
|
167
|
|
|
$this->view->getRenderingContext()->getVariableProvider()->getAll(), |
|
|
|
|
|
|
168
|
|
|
true |
|
169
|
|
|
); |
|
170
|
|
|
if (!empty($subjectFromTemplate)) { |
|
171
|
|
|
$this->subject($subjectFromTemplate); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
protected function renderContent(string $format): string |
|
176
|
|
|
{ |
|
177
|
|
|
$this->view->setFormat($format); |
|
178
|
|
|
$this->view->setTemplate($this->templateName); |
|
179
|
|
|
return $this->view->render(); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|