TYPO3 CMS  TYPO3_7-6
MailPostProcessor.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
23 
28 {
34  const LOCALISATION_OBJECT_NAME = 'tx_form_view_mail';
35 
39  protected $objectManager;
40 
44  protected $sessionUtility;
45 
49  protected $formUtility;
50 
54  protected $form;
55 
59  protected $typoScript;
60 
64  protected $mailMessage;
65 
69  protected $htmlMailTemplatePath = 'Html';
70 
74  protected $plaintextMailTemplatePath = 'Plain';
75 
79  protected $dirtyHeaders = [];
80 
85  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
86  {
87  $this->objectManager = $objectManager;
88  }
89 
94  public function injectSessionUtility(\TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility)
95  {
96  $this->sessionUtility = $sessionUtility;
97  }
98 
105  public function __construct(\TYPO3\CMS\Form\Domain\Model\Element $form, array $typoScript)
106  {
107  $this->form = $form;
108  $this->typoScript = $typoScript;
109  $this->mailMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
110  $this->setTemplatePaths();
111  }
112 
120  public function process()
121  {
122  $this->formUtility = FormUtility::create($this->controllerContext->getConfiguration());
123  $this->setSubject();
124  $this->setFrom();
125  $this->setTo();
126  $this->setCc();
127  $this->setReplyTo();
128  $this->setPriority();
129  $this->setOrganization();
130  $this->setHtmlContent();
131  $this->setPlainContent();
132  $this->addAttachmentsFromSession();
133  $this->send();
134  return $this->render();
135  }
136 
144  protected function setSubject()
145  {
146  if (isset($this->typoScript['subject'])) {
147  $subject = $this->formUtility->renderItem(
148  $this->typoScript['subject.'],
149  $this->typoScript['subject']
150  );
151  } elseif ($this->getTypoScriptValueFromIncomingData('subjectField') !== null) {
152  $subject = $this->getTypoScriptValueFromIncomingData('subjectField');
153  } else {
154  $subject = 'Formmail on ' . GeneralUtility::getIndpEnv('HTTP_HOST');
155  }
156 
157  $subject = $this->sanitizeHeaderString($subject);
158  $this->mailMessage->setSubject($subject);
159  }
160 
168  protected function setFrom()
169  {
170  if (isset($this->typoScript['senderEmail'])) {
171  $fromEmail = $this->formUtility->renderItem(
172  $this->typoScript['senderEmail.'],
173  $this->typoScript['senderEmail']
174  );
175  } elseif ($this->getTypoScriptValueFromIncomingData('senderEmailField') !== null) {
176  $fromEmail = $this->getTypoScriptValueFromIncomingData('senderEmailField');
177  } else {
178  $fromEmail = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
179  }
180  if (!GeneralUtility::validEmail($fromEmail)) {
181  $fromEmail = MailUtility::getSystemFromAddress();
182  }
183  if (isset($this->typoScript['senderName'])) {
184  $fromName = $this->formUtility->renderItem(
185  $this->typoScript['senderName.'],
186  $this->typoScript['senderName']
187  );
188  } elseif ($this->getTypoScriptValueFromIncomingData('senderNameField') !== null) {
189  $fromName = $this->getTypoScriptValueFromIncomingData('senderNameField');
190  } else {
191  $fromName = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
192  }
193  $fromName = $this->sanitizeHeaderString($fromName);
194  if (!empty($fromName)) {
195  $from = [$fromEmail => $fromName];
196  } else {
197  $from = $fromEmail;
198  }
199  $this->mailMessage->setFrom($from);
200  }
201 
208  protected function filterValidEmails($emails)
209  {
210  if (!is_string($emails)) {
211  // No valid addresses - empty list
212  return [];
213  }
214 
216  $addressParser = GeneralUtility::makeInstance(Rfc822AddressesParser::class, $emails);
217  $addresses = $addressParser->parseAddressList();
218 
219  $validEmails = [];
220  foreach ($addresses as $address) {
221  $fullAddress = $address->mailbox . '@' . $address->host;
222  if (GeneralUtility::validEmail($fullAddress)) {
223  if ($address->personal) {
224  $validEmails[$fullAddress] = $address->personal;
225  } else {
226  $validEmails[] = $fullAddress;
227  }
228  }
229  }
230  return $validEmails;
231  }
232 
240  protected function setTo()
241  {
242  $emails = $this->formUtility->renderItem(
243  $this->typoScript['recipientEmail.'],
244  $this->typoScript['recipientEmail']
245  );
246  $validEmails = $this->filterValidEmails($emails);
247  if (!empty($validEmails)) {
248  $this->mailMessage->setTo($validEmails);
249  }
250  }
251 
259  protected function setCc()
260  {
261  $emails = $this->formUtility->renderItem(
262  $this->typoScript['ccEmail.'],
263  $this->typoScript['ccEmail']
264  );
265  $validEmails = $this->filterValidEmails($emails);
266  if (!empty($validEmails)) {
267  $this->mailMessage->setCc($validEmails);
268  }
269  }
270 
278  protected function setReplyTo()
279  {
280  if (isset($this->typoScript['replyToEmail'])) {
281  $emails = $this->formUtility->renderItem(
282  $this->typoScript['replyToEmail.'],
283  $this->typoScript['replyToEmail']
284  );
285  } elseif ($this->getTypoScriptValueFromIncomingData('replyToEmailField') !== null) {
286  $emails = $this->getTypoScriptValueFromIncomingData('replyToEmailField');
287  }
288  $validEmails = $this->filterValidEmails($emails);
289  if (!empty($validEmails)) {
290  $this->mailMessage->setReplyTo($validEmails);
291  }
292  }
293 
302  protected function setPriority()
303  {
304  $priority = 3;
305  if (isset($this->typoScript['priority'])) {
306  $priorityFromTs = $this->formUtility->renderItem(
307  $this->typoScript['priority.'],
308  $this->typoScript['priority']
309  );
310  }
311  if (!empty($priorityFromTs)) {
312  $priority = MathUtility::forceIntegerInRange($priorityFromTs, 1, 5);
313  }
314  $this->mailMessage->setPriority($priority);
315  }
316 
324  protected function setOrganization()
325  {
326  if (isset($this->typoScript['organization'])) {
327  $organization = $this->formUtility->renderItem(
328  $this->typoScript['organization.'],
329  $this->typoScript['organization']
330  );
331  }
332  if (!empty($organization)) {
333  $organization = $this->sanitizeHeaderString($organization);
334  $this->mailMessage->getHeaders()->addTextHeader('Organization', $organization);
335  }
336  }
337 
346  protected function setCharacterSet()
347  {
348  $characterSet = null;
349  if ($GLOBALS['TSFE']->config['config']['formMailCharset']) {
350  $characterSet = $GLOBALS['TSFE']->csConvObj->parse_charset($GLOBALS['TSFE']->config['config']['formMailCharset']);
351  } elseif ($GLOBALS['TSFE']->metaCharset != $GLOBALS['TSFE']->renderCharset) {
352  $characterSet = $GLOBALS['TSFE']->metaCharset;
353  }
354  if ($characterSet) {
355  $this->mailMessage->setCharset($characterSet);
356  }
357  }
358 
366  protected function setHtmlContent()
367  {
368  $htmlContent = $this->getView($this->htmlMailTemplatePath)->render();
369  $this->mailMessage->setBody($htmlContent, 'text/html');
370  }
371 
379  protected function setPlainContent()
380  {
381  $plainContent = $this->getView($this->plaintextMailTemplatePath, 'Plain')->render();
382  $this->mailMessage->addPart($plainContent, 'text/plain');
383  }
384 
391  protected function send()
392  {
393  if ($this->mailMessage->getTo() && $this->mailMessage->getBody()) {
394  $this->mailMessage->send();
395  }
396  }
397 
403  protected function render()
404  {
405  if ($this->mailMessage->isSent()) {
406  $output = $this->renderMessage('success');
407  } else {
408  $output = $this->renderMessage('error');
409  }
410  return $output;
411  }
412 
419  protected function sanitizeHeaderString($string)
420  {
421  $pattern = '/[\\r\\n\\f\\e]/';
422  if (preg_match($pattern, $string) > 0) {
423  $this->dirtyHeaders[] = $string;
424  $string = '';
425  }
426  return $string;
427  }
428 
435  protected function addAttachmentsFromSession()
436  {
437  $sessionData = $this->sessionUtility->getSessionData();
438  if (is_array($sessionData)) {
439  foreach ($sessionData as $fieldName => $values) {
440  if (is_array($values)) {
441  foreach ($values as $file) {
442  if (isset($file['tempFilename'])) {
443  if (
444  is_file($file['tempFilename'])
445  && GeneralUtility::isAllowedAbsPath($file['tempFilename'])
446  ) {
447  $this->mailMessage->attach(\Swift_Attachment::fromPath($file['tempFilename'])->setFilename($file['name']));
448  }
449  }
450  }
451  }
452  }
453  }
454  }
455 
461  protected function setTemplatePaths()
462  {
463  if (
464  isset($this->typoScript['htmlMailTemplatePath'])
465  && $this->typoScript['htmlMailTemplatePath'] !== ''
466  ) {
467  $this->htmlMailTemplatePath = $this->typoScript['htmlMailTemplatePath'];
468  }
469 
470  if (
471  isset($this->typoScript['plaintextMailTemplatePath'])
472  && $this->typoScript['plaintextMailTemplatePath'] !== ''
473  ) {
474  $this->plaintextMailTemplatePath = $this->typoScript['plaintextMailTemplatePath'];
475  }
476  }
477 
485  protected function getView($templateName, $scope = 'Html')
486  {
487  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
488  $typoScript = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
490  $view = $this->objectManager->get(\TYPO3\CMS\Fluid\View\StandaloneView::class);
491 
492  $viewParts = [
493  'templateRootPaths' => $typoScript['view']['templateRootPaths'],
494  'partialRootPaths' => $typoScript['view']['partialRootPaths'],
495  ];
496  /* Extend all template root paths to $templateRootPaths/PostProcessor/Mail/$themeName */
497  foreach ($typoScript['view']['templateRootPaths'] as &$path) {
498  if (substr($path, -1) !== '/') {
499  $path .= '/';
500  }
501  $path .= 'PostProcessor/Mail/' . $this->controllerContext->getConfiguration()->getThemeName();
502  }
503  /* Extend all partial root paths to $partialRootPaths/$themeName/PostProcessor/Mail/$scope/ */
504  foreach ($typoScript['view']['partialRootPaths'] as &$path) {
505  if (substr($path, -1) !== '/') {
506  $path .= '/';
507  }
508  $path .= $this->controllerContext->getConfiguration()->getThemeName() . '/PostProcessor/Mail/' . $scope . '/';
509  }
510  $view->setLayoutRootPaths($typoScript['view']['layoutRootPaths']);
511  $view->setTemplateRootPaths($typoScript['view']['templateRootPaths']);
512  $view->setPartialRootPaths($typoScript['view']['partialRootPaths']);
513  $view->setTemplate($templateName);
514  $view->assignMultiple([
515  'model' => $this->form
516  ]);
517  return $view;
518  }
519 
526  protected function renderMessage($messageType)
527  {
528  return $this->formUtility->renderItem(
529  $this->typoScript['messages.'][$messageType . '.'],
530  $this->typoScript['messages.'][$messageType],
531  $this->getLocalLanguageLabel($messageType)
532  );
533  }
534 
542  protected function getLocalLanguageLabel($type = '')
543  {
544  $label = static::LOCALISATION_OBJECT_NAME . '.' . $type;
545  $message = LocalizationUtility::translate($label, 'form');
546  return $message;
547  }
548 
556  protected function getTypoScriptValueFromIncomingData($propertyName)
557  {
558  if (empty($this->typoScript[$propertyName])) {
559  return null;
560  }
561 
562  $propertyValue = $this->typoScript[$propertyName];
563  $incomingData = $this->controllerContext->getValidationElement();
564  if (!$incomingData->hasIncomingField($propertyValue)) {
565  return null;
566  }
567 
568  return $incomingData->getIncomingField($propertyValue);
569  }
570 }
getTypoScriptValueFromIncomingData($propertyName)
static translate($key, $extensionName, $arguments=null)
$htmlMailTemplatePath
setReplyTo()
setTemplatePaths()
setCc()
setPriority()
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
render()
setSubject()
__construct(\TYPO3\CMS\Form\Domain\Model\Element $form, array $typoScript)
addAttachmentsFromSession()
setOrganization()
$sessionUtility
$plaintextMailTemplatePath
$mailMessage
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
setCharacterSet()
$formUtility
setFrom()
renderMessage($messageType)
$dirtyHeaders
setTo()
getLocalLanguageLabel($type='')
setHtmlContent()
process()
const LOCALISATION_OBJECT_NAME
$form
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
injectSessionUtility(\TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility)
$typoScript
setPlainContent()
$objectManager
send()
sanitizeHeaderString($string)