-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageService.php
More file actions
310 lines (260 loc) · 8.41 KB
/
Copy pathMessageService.php
File metadata and controls
310 lines (260 loc) · 8.41 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace Fab\Formule\Service;
/*
* This file is part of the Fab/Formule project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use Michelf\Markdown;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use Symfony\Component\Mime\Part\TextPart;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Fab\Formule\Service\LoggingService;
use Psr\Container\ContainerInterface;
/**
* MessageService
*/
class MessageService
{
protected LoggingService $loggingService;
public function __construct(array $settings, string $messageTarget, ?LoggingService $loggingService = null)
{
$this->settings = $settings;
$this->messageTarget = $messageTarget;
if ($loggingService !== null) {
$this->loggingService = $loggingService;
} else {
// Create LoggingService with its required dependencies
$connectionPool = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class);
$this->loggingService = new LoggingService($connectionPool);
}
}
const TO_ADMIN = 'Admin';
const TO_USER = 'User';
/**
* @var
*/
protected $mailMessage;
/**
* @var array
*/
protected $settings;
/**
* @var array
*/
protected $values;
/**
* @var string
*/
protected $messageTarget;
/**
* @param array $values
* @return bool
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function send(array $values): bool
{
$this->values = $values;
// Substitute markers
$subject = $this->renderWithFluid($this->getSubject(), $values);
$body = $this->renderWithFluid($this->getBody(), $values);
// Parse body through Markdown processing.
$body = Markdown::defaultTransform($body);
$this->getMailMessage()
#->setSender($this->getFrom())
->setFrom($this->getFrom())
->setTo($this->getTo())
#->setCc($this->getCc())
#->setBcc($this->getBcc())
->setSubject($subject);
// According to preference.
if ($this->isPlainTextPreferred()) {
$text = Html2Text::getInstance()->convert($body);
$this->getMailMessage()->setBody(new TextPart($text));
} else {
// Create multipart message with HTML and text versions
if ($this->hasHtml($body)) {
$text = Html2Text::getInstance()->convert($body);
$alternativePart = new AlternativePart(
new TextPart($text),
new TextPart($body, 'text/html')
);
$this->getMailMessage()->setBody($alternativePart);
} else {
$this->getMailMessage()->setBody(new TextPart($body, 'text/html'));
}
}
// Handle attachment
#foreach ($this->attachments as $attachment) {
# $this->getMailMessage()->attach($attachment);
#}
$this->getMailMessage()->send();
$isSent = $this->getMailMessage()->isSent();
$this->getLoggingService()->log($this->getMailMessage());
return $isSent;
}
/**
* Check whether a string contains HTML tags
*
* @see http://preprocess.me/how-to-check-if-a-string-contains-html-tags-in-php
* @param string $content the content to be analyzed
* @return boolean
*/
protected function hasHtml(string $content): bool
{
$result = FALSE;
//we compare the length of the string with html tags and without html tags
if (strlen($content) !== strlen(strip_tags($content))) {
$result = TRUE;
}
return $result;
}
/**
* @param $content
* @param array $values
* @return string
*/
protected function renderWithFluid($content, array $values): string
{
/** @var StandaloneView $view */
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setTemplateSource($content);
// Make it possible to keen the syntact {values.foo} in the template
$values['values'] = $values;
$view->assignMultiple($values);
return trim($view->render());
}
/**
* @return array
* @throws \Fab\Formule\Exception\InvalidEmailFormatException
*/
public function getFrom(): array
{
$emailFrom = [];
if (!empty($this->settings['emailFrom'])) {
$email = $this->settings['emailFrom'];
if (empty($this->settings['nameFrom'])) {
$name = $this->settings['emailFrom'];
} else {
$name = $this->settings['nameFrom'];
}
$emailFrom = array($email => $name);
$this->getEmailAddressService()->validate($emailFrom);
} elseif (!empty($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'])) {
$email = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
if (empty($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'])) {
$name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
} else {
$name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
}
$emailFrom = array($email => $name);
$this->getEmailAddressService()->validate($emailFrom);
}
return $emailFrom;
}
/**
* @return array
*/
protected function getTo(): array
{
$to = $this->get('to');
if (!filter_var($to, FILTER_VALIDATE_EMAIL) && !empty($this->values[$to])) {
$to = $this->values[$to];
}
return $this->getEmailAddressService()->parse($to);
}
/**
* @return array
*/
protected function getCc(): array
{
$cc = $this->get('cc');
if (!filter_var($cc, FILTER_VALIDATE_EMAIL) && !empty($this->values[$cc])) {
$cc = $this->values[$cc];
}
return $this->getEmailAddressService()->parse($cc);
}
/**
* @return array
*/
protected function getBcc(): array
{
$bcc = $this->get('bcc');
if (!filter_var($bcc, FILTER_VALIDATE_EMAIL) && !empty($this->values[$bcc])) {
$bcc = $this->values[$bcc];
}
return $this->getEmailAddressService()->parse($bcc);
}
/**
* @return string
*/
protected function getSubject(): string
{
return $this->get('subject');
}
/**
* @return bool
*/
protected function isPlainTextPreferred(): bool
{
return $this->getTemplateService($this->settings['template'])->getPreferredEmailBodyEncoding() === 'text';
}
/**
* @return string
*/
protected function getBody(): string
{
$section = $this->messageTarget === self::TO_ADMIN ? TemplateService::SECTION_EMAIL_ADMIN : TemplateService::SECTION_EMAIL_USER;
$body = $this->getTemplateService($this->settings['template'])->getSection($section);
if (empty($body)) {
$body = $this->get('body');
}
return $body;
}
/**
* @return string
*/
protected function get($key): string
{
return $this->settings['email' . $this->messageTarget . ucfirst($key)];
}
/**
* @return object|MailMessage
*/
protected function getMailMessage()
{
if (is_null($this->mailMessage)) {
$this->mailMessage = GeneralUtility::makeInstance(MailMessage::class);
}
return $this->mailMessage;
}
/**
* @return object|LoggingService
* @throws \InvalidArgumentException
*/
protected function getLoggingService()
{
return $this->loggingService;
}
/**
* @return object|EmailAddressService
* @throws \InvalidArgumentException
*/
public function getEmailAddressService()
{
return GeneralUtility::makeInstance(EmailAddressService::class);
}
/**
* @param int $templateIdentifier
* @return object|TemplateService
* @throws \InvalidArgumentException
*/
protected function getTemplateService(int $templateIdentifier)
{
return GeneralUtility::makeInstance(TemplateService::class, $templateIdentifier);
}
}