-
Notifications
You must be signed in to change notification settings - Fork 641
/
EmailFinisher.php
259 lines (227 loc) · 9.67 KB
/
EmailFinisher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Form\Domain\Finishers;
use Symfony\Component\Mime\Address;
use TYPO3\CMS\Core\Mail\FluidEmail;
use TYPO3\CMS\Core\Mail\MailerInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Fluid\View\TemplatePaths;
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
use TYPO3\CMS\Form\Service\TranslationService;
use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;
/**
* This finisher sends an email to one recipient
*
* Options:
*
* - templateName (mandatory): Template name for the mail body
* - templateRootPaths: root paths for the templates
* - layoutRootPaths: root paths for the layouts
* - partialRootPaths: root paths for the partials
* - variables: associative array of variables which are available inside the Fluid template
*
* The following options control the mail sending. In all of them, placeholders in the form
* of {...} are replaced with the corresponding form value; i.e. {email} as senderAddress
* makes the recipient address configurable.
*
* - subject (mandatory): Subject of the email
* - recipients (mandatory): Email addresses and human-readable names of the recipients
* - senderAddress (mandatory): Email address of the sender
* - senderName: Human-readable name of the sender
* - replyToRecipients: Email addresses and human-readable names of the reply-to recipients
* - carbonCopyRecipients: Email addresses and human-readable names of the copy recipients
* - blindCarbonCopyRecipients: Email addresses and human-readable names of the blind copy recipients
* - title: The title of the email - If not set "subject" is used by default
*
* Scope: frontend
*/
class EmailFinisher extends AbstractFinisher
{
/**
* @var array
*/
protected $defaultOptions = [
'recipientName' => '',
'senderName' => '',
'addHtmlPart' => true,
'attachUploads' => true,
];
/**
* Executes this finisher
* @see AbstractFinisher::execute()
*
* @throws FinisherException
*/
protected function executeInternal()
{
$languageBackup = null;
// Flexform overrides write strings instead of integers so
// we need to cast the string '0' to false.
if (
isset($this->options['addHtmlPart'])
&& $this->options['addHtmlPart'] === '0'
) {
$this->options['addHtmlPart'] = false;
}
$subject = (string)$this->parseOption('subject');
$recipients = $this->getRecipients('recipients');
$senderAddress = $this->parseOption('senderAddress');
$senderAddress = is_string($senderAddress) ? $senderAddress : '';
$senderName = $this->parseOption('senderName');
$senderName = is_string($senderName) ? $senderName : '';
$replyToRecipients = $this->getRecipients('replyToRecipients');
$carbonCopyRecipients = $this->getRecipients('carbonCopyRecipients');
$blindCarbonCopyRecipients = $this->getRecipients('blindCarbonCopyRecipients');
$addHtmlPart = $this->parseOption('addHtmlPart') ? true : false;
$attachUploads = $this->parseOption('attachUploads');
$title = (string)$this->parseOption('title') ?: $subject;
if ($subject === '') {
throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
}
if (empty($recipients)) {
throw new FinisherException('The option "recipients" must be set for the EmailFinisher.', 1327060200);
}
if (empty($senderAddress)) {
throw new FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210);
}
$formRuntime = $this->finisherContext->getFormRuntime();
$translationService = GeneralUtility::makeInstance(TranslationService::class);
if (is_string($this->options['translation']['language'] ?? null) && $this->options['translation']['language'] !== '') {
$languageBackup = $translationService->getLanguage();
$translationService->setLanguage($this->options['translation']['language']);
}
$mail = $this
->initializeFluidEmail($formRuntime)
->from(new Address($senderAddress, $senderName))
->to(...$recipients)
->subject($subject)
->format($addHtmlPart ? FluidEmail::FORMAT_BOTH : FluidEmail::FORMAT_PLAIN)
->assign('title', $title);
if (!empty($replyToRecipients)) {
$mail->replyTo(...$replyToRecipients);
}
if (!empty($carbonCopyRecipients)) {
$mail->cc(...$carbonCopyRecipients);
}
if (!empty($blindCarbonCopyRecipients)) {
$mail->bcc(...$blindCarbonCopyRecipients);
}
if (!empty($languageBackup)) {
$translationService->setLanguage($languageBackup);
}
if ($attachUploads) {
foreach ($formRuntime->getFormDefinition()->getRenderablesRecursively() as $element) {
if (!$element instanceof FileUpload) {
continue;
}
$file = $formRuntime[$element->getIdentifier()];
if ($file) {
if ($file instanceof FileReference) {
$file = $file->getOriginalResource();
}
$mail->attach($file->getContents(), $file->getName(), $file->getMimeType());
}
}
}
// TODO: DI should be used to inject the MailerInterface
GeneralUtility::makeInstance(MailerInterface::class)->send($mail);
}
protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail
{
$templateConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
if (is_array($this->options['templateRootPaths'] ?? null)) {
$templateConfiguration['templateRootPaths'] = array_replace_recursive(
$templateConfiguration['templateRootPaths'],
$this->options['templateRootPaths']
);
ksort($templateConfiguration['templateRootPaths']);
}
if (is_array($this->options['partialRootPaths'] ?? null)) {
$templateConfiguration['partialRootPaths'] = array_replace_recursive(
$templateConfiguration['partialRootPaths'],
$this->options['partialRootPaths']
);
ksort($templateConfiguration['partialRootPaths']);
}
if (is_array($this->options['layoutRootPaths'] ?? null)) {
$templateConfiguration['layoutRootPaths'] = array_replace_recursive(
$templateConfiguration['layoutRootPaths'],
$this->options['layoutRootPaths']
);
ksort($templateConfiguration['layoutRootPaths']);
}
$fluidEmail = GeneralUtility::makeInstance(
FluidEmail::class,
GeneralUtility::makeInstance(TemplatePaths::class, $templateConfiguration)
);
if (!isset($this->options['templateName']) || $this->options['templateName'] === '') {
throw new FinisherException('The option "templateName" must be set to use FluidEmail.', 1599834020);
}
// Migrate old template name to default FluidEmail name
if ($this->options['templateName'] === '{@format}.html') {
$this->options['templateName'] = 'Default';
}
$fluidEmail
->setRequest($this->finisherContext->getRequest())
->setTemplate($this->options['templateName'])
->assignMultiple([
'finisherVariableProvider' => $this->finisherContext->getFinisherVariableProvider(),
'form' => $formRuntime,
]);
if (is_array($this->options['variables'] ?? null)) {
$fluidEmail->assignMultiple($this->options['variables']);
}
$fluidEmail
->getViewHelperVariableContainer()
->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime);
return $fluidEmail;
}
/**
* Get mail recipients
*
* @param string $listOption List option name
*/
protected function getRecipients(string $listOption): array
{
$recipients = $this->parseOption($listOption) ?? [];
if (!is_array($recipients) || $recipients === []) {
return [];
}
$addresses = [];
foreach ($recipients as $address => $name) {
// The if is needed to set address and name with TypoScript
if (MathUtility::canBeInterpretedAsInteger($address)) {
if (is_array($name)) {
$address = $name[0] ?? '';
$name = $name[1] ?? '';
} else {
$address = $name;
$name = '';
}
}
$address = trim((string)$address);
if (!GeneralUtility::validEmail($address)) {
// Drop entries without valid address
continue;
}
$addresses[] = new Address($address, $name);
}
return $addresses;
}
}