diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php index 08938f5d1f5e0b7695c0622e60d07459c2474a9f..f6965ef4b3eb76826b0701d39aaa544b5aaded99 100644 --- a/Classes/Service/MailTemplateService.php +++ b/Classes/Service/MailTemplateService.php @@ -184,12 +184,12 @@ class MailTemplateService { $this->resourceFactory = $this->objectManager->get(ResourceFactory::class); // use defaultMailFromAddress if it is provided in LocalConfiguration.php; use the sg_mail TS setting as fallback - if (filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) { + if (\filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) { $this->fromAddress = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']; } else { $this->fromAddress = $tsSettings['mail']['default']['from']; - if (!filter_var($tsSettings['mail']['default']['from'], FILTER_VALIDATE_EMAIL)) { + if (!\filter_var($tsSettings['mail']['default']['from'], FILTER_VALIDATE_EMAIL)) { $this->fromAddress = 'noreply@example.org'; } else { $this->fromAddress = $tsSettings['mail']['default']['from']; @@ -201,12 +201,11 @@ class MailTemplateService { } $this->mailMessage->setFrom($this->fromAddress, $this->fromName); - $this->bccAddresses = GeneralUtility::trimExplode(',', $tsSettings['mail']['default']['bcc']); $this->ccAddresses = GeneralUtility::trimExplode(',', $tsSettings['mail']['default']['cc']); foreach ($this->bccAddresses as $index => $email) { - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + if (!\filter_var($email, FILTER_VALIDATE_EMAIL)) { unset($this->bccAddresses[$index]); } } @@ -355,7 +354,7 @@ class MailTemplateService { */ public function setMarkers(array $markers): MailTemplateService { $this->markers = $markers; - foreach($markers as $key => $currentMarker) { + foreach ($markers as $key => $currentMarker) { if (!\is_array($currentMarker) || !isset($currentMarker['markerLabel'])) { continue; } @@ -497,9 +496,8 @@ class MailTemplateService { */ public static function getDefaultTemplateMarker($translationKey, array $marker, $extensionKey = 'sg_mail'): array { $languagePath = 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/locallang.xlf:' . $translationKey; - // Need the key for translations - if (trim($extensionKey) === '') { + if (\trim($extensionKey) === '') { return []; } @@ -568,9 +566,8 @@ class MailTemplateService { $isTemplateBlacklisted = self::isTemplateBlacklisted($this->extensionKey, $this->templateName, $siteRootId); if ($isTemplateBlacklisted) { - // @TODO: This needs to be changed, because if the template is blacklisted, the email can not be sent - $success = TRUE; - return $success; + // @TODO throw error or log ? + return FALSE; } /** @var Template $template */ @@ -602,8 +599,8 @@ class MailTemplateService { // only standard template file is considered since version 4.1 $defaultTemplateFile = $templatePath . 'template.html'; - if (file_exists($defaultTemplateFile)) { - $defaultTemplateContent = file_get_contents($defaultTemplateFile); + if (\file_exists($defaultTemplateFile)) { + $defaultTemplateContent = \file_get_contents($defaultTemplateFile); } else { // use configured default html template /** @var TypoScriptSettingsService $typoScriptSettingsService */ @@ -613,16 +610,15 @@ class MailTemplateService { $tsSettings['mail']['defaultHtmlTemplate'] ); - if (file_exists($defaultTemplateFile)) { - $defaultTemplateContent = file_get_contents($defaultTemplateFile); + if (\file_exists($defaultTemplateFile)) { + $defaultTemplateContent = \file_get_contents($defaultTemplateFile); } else { - return $success; + return FALSE; } } } - - } elseif (filter_var($template->getToAddress(), FILTER_VALIDATE_EMAIL)) { - $this->setToAddresses(trim($template->getToAddress())); + } elseif (\filter_var($template->getToAddress(), FILTER_VALIDATE_EMAIL)) { + $this->setToAddresses(\trim($template->getToAddress())); } if ($isPreview) { @@ -633,7 +629,7 @@ class MailTemplateService { foreach ($markerArray as $marker) { $markerPath = GeneralUtility::trimExplode('.', $marker['marker']); $temporaryMarkerArray = []; - foreach (array_reverse($markerPath) as $index => $markerPathSegment) { + foreach (\array_reverse($markerPath) as $index => $markerPathSegment) { if ($index === 0) { if ($marker['markerLabel']) { $markerPathSegment = $marker['markerLabel']; @@ -651,7 +647,7 @@ class MailTemplateService { } } /** @noinspection SlowArrayOperationsInLoopInspection */ - $previewMarker = array_merge_recursive($previewMarker, $temporaryMarkerArray); + $previewMarker = \array_merge_recursive($previewMarker, $temporaryMarkerArray); } $this->setIgnoreMailQueue(TRUE); $this->setMarkers($previewMarker); @@ -693,8 +689,8 @@ class MailTemplateService { // insert <br> tags, but replace every instance of three or more successive breaks with just two. $emailBody = $emailView->render(); - $emailBody = nl2br($emailBody); - $emailBody = preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br><br>', $emailBody); + $emailBody = \nl2br($emailBody); + $emailBody = \preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br><br>', $emailBody); $mail = $this->addMailToMailQueue( $this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority, @@ -779,59 +775,58 @@ class MailTemplateService { $mailRepository = $this->objectManager->get(MailRepository::class); /** @var Mail $mailToSend */ $mailToSend = $mailRepository->findOneByUid($uid); + if (!$mailToSend || $mailToSend->getBlacklisted()) { + return FALSE; + } - if ($mailToSend && !$mailToSend->getBlacklisted()) { - $this->mailMessage->setBody($mailToSend->getMailBody(), 'text/html'); - $plaintextService = GeneralUtility::makeInstance(PlaintextService::class); - $plaintextBody = $plaintextService->makePlain($mailToSend->getMailBody()); - $this->mailMessage->addPart($plaintextBody, 'text/plain'); - $toAddresses = trim($mailToSend->getToAddress()); - $addressesArray = GeneralUtility::trimExplode(',', $toAddresses, TRUE); - if (\count($addressesArray) > 1) { - $toAddresses = $addressesArray; - } - $this->mailMessage->setTo($toAddresses); - $this->mailMessage->setFrom($mailToSend->getFromAddress(), $mailToSend->getFromName()); - $this->mailMessage->setSubject($mailToSend->getMailSubject()); + $this->mailMessage->setBody($mailToSend->getMailBody(), 'text/html'); + $plaintextService = GeneralUtility::makeInstance(PlaintextService::class); + $plaintextBody = $plaintextService->makePlain($mailToSend->getMailBody()); + $this->mailMessage->addPart($plaintextBody, 'text/plain'); + $toAddresses = \trim($mailToSend->getToAddress()); + $addressesArray = GeneralUtility::trimExplode(',', $toAddresses, TRUE); + if (\count($addressesArray) > 1) { + $toAddresses = $addressesArray; + } + $this->mailMessage->setTo($toAddresses); + $this->mailMessage->setFrom($mailToSend->getFromAddress(), $mailToSend->getFromName()); + $this->mailMessage->setSubject($mailToSend->getMailSubject()); - if ($mailToSend->getBccAddresses()) { - $this->mailMessage->setBcc(GeneralUtility::trimExplode(',', $mailToSend->getBccAddresses())); - } + if ($mailToSend->getBccAddresses()) { + $this->mailMessage->setBcc(GeneralUtility::trimExplode(',', $mailToSend->getBccAddresses())); + } - if ($mailToSend->getCcAddresses()) { - $this->mailMessage->setCc(GeneralUtility::trimExplode(',', $mailToSend->getCcAddresses())); - } + if ($mailToSend->getCcAddresses()) { + $this->mailMessage->setCc(GeneralUtility::trimExplode(',', $mailToSend->getCcAddresses())); + } - if ($mailToSend->getReplyTo()) { - $this->mailMessage->setReplyTo($mailToSend->getReplyTo()); - } - $attachments = $mailToSend->getAttachments(); - if ($attachments->count() > 0) { - foreach ($attachments as $attachment) { - /** - * @var FileReference $attachment - */ - $file = $attachment->getOriginalResource()->getOriginalFile(); - $this->mailMessage->attach( - \Swift_Attachment::newInstance($file->getContents(), $file->getName(), $file->getMimeType()) - ); - } - } - $dateTime = new DateTime(); - if ((int) $mailToSend->getSendingTime() === 0) { - $mailToSend->setSendingTime($dateTime->getTimestamp()); - } - $mailToSend->setLastSendingTime($dateTime->getTimestamp()); - $success = $this->mailMessage->send(); - if ($success) { - $mailRepository->update($mailToSend); - } else { - $this->mailMessage->getFailedRecipients(); + if ($mailToSend->getReplyTo()) { + $this->mailMessage->setReplyTo($mailToSend->getReplyTo()); + } + $attachments = $mailToSend->getAttachments(); + if ($attachments->count() > 0) { + foreach ($attachments as $attachment) { + /** + * @var FileReference $attachment + */ + $file = $attachment->getOriginalResource()->getOriginalFile(); + $this->mailMessage->attach( + \Swift_Attachment::newInstance($file->getContents(), $file->getName(), $file->getMimeType()) + ); } - return $success; } - - return NULL; + $dateTime = new DateTime(); + if ((int) $mailToSend->getSendingTime() === 0) { + $mailToSend->setSendingTime($dateTime->getTimestamp()); + } + $mailToSend->setLastSendingTime($dateTime->getTimestamp()); + $success = $this->mailMessage->send(); + if ($success) { + $mailRepository->update($mailToSend); + } else { + $this->mailMessage->getFailedRecipients(); + } + return $success; } /** @@ -848,23 +843,34 @@ class MailTemplateService { $fromName = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']; } - $fromMail = \trim($template->getFromMail()); - if (!filter_var($fromMail, FILTER_VALIDATE_EMAIL)) { + $fromMail = $this->getValidFromMail(\trim($template->getFromMail())); + $this->setFromAddress($fromMail, $fromName); + $this->setCcAddresses($template->getCc()); + $this->setBccAddresses($template->getBcc()); + $this->setReplyToAddress($template->getReplyTo()); + $this->setFromName($fromName); + } + + /** + * Sets the fromMail property of the mailTemplateService. + * Checks validity and uses all available fallbacks + * + * @param string $fromMail + * @return string + */ + private function getValidFromMail($fromMail): string { + $fromMail = \trim($fromMail); + if (!\filter_var($fromMail, FILTER_VALIDATE_EMAIL)) { $fromMail = $this->fromAddress; } - if (!filter_var($fromMail, FILTER_VALIDATE_EMAIL)) { + if (!\filter_var($fromMail, FILTER_VALIDATE_EMAIL)) { $fromMail = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']; - if (!filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) { + if (!\filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) { $fromMail = 'noreply@example.com'; } } - $this->setFromAddress($fromMail, $fromName); - $this->setCcAddresses($template->getCc()); - $this->setBccAddresses($template->getBcc()); - $this->setReplyToAddress($template->getReplyTo()); - $this->setFromName($fromName); - $this->setReplyToAddress($template->getReplyTo()); + return $fromMail; } /** @@ -891,7 +897,7 @@ class MailTemplateService { $valueAsString = $value ? 'true' : 'false'; $allMarker .= $key . ': ' . $valueAsString . PHP_EOL; } elseif (\is_object($value)) { - if (method_exists($value, '__toString')) { + if (\method_exists($value, '__toString')) { $allMarker .= $key . ': ' . $value->__toString() . PHP_EOL; } } diff --git a/Classes/Service/PlaintextService.php b/Classes/Service/PlaintextService.php index 196186ad3580a61deccea2e439db85bf14e8ab5b..57b963cb985b63f687dc0945ce64fc4405ecc370 100644 --- a/Classes/Service/PlaintextService.php +++ b/Classes/Service/PlaintextService.php @@ -99,7 +99,7 @@ class PlaintextService { * @param string $content * @return array */ - protected function addLineBreaks($content) { + protected function addLineBreaks($content): array { $tags2LineBreaks = [ '</p>', '</tr>', diff --git a/Classes/Service/RegisterService.php b/Classes/Service/RegisterService.php index 99695873844c8550319a9f51a9440fa77cf24042..0bb10eb4d521147d78ffe0c7cb361278d433d88c 100644 --- a/Classes/Service/RegisterService.php +++ b/Classes/Service/RegisterService.php @@ -130,9 +130,9 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { $pathAsArray = GeneralUtility::trimExplode('/', $pathToRegistrationFile); $filename = $pathAsArray[\count($pathAsArray) - 1]; $filenameWithoutHash = GeneralUtility::trimExplode('_', $filename, FALSE, 2)[1]; - $hash = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $filenameWithoutHash); + $hash = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $filenameWithoutHash); // if the filename doesn't start with the hash value, ignore it - if (strpos($filename, $hash) !== 0) { + if (\strpos($filename, $hash) !== 0) { continue; } @@ -240,7 +240,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { $registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation); GeneralUtility::mkdir_deep($registerFolder); - $hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php'); + $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php'); $registerFile = GeneralUtility::getFileAbsFileName( $registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php' ); @@ -268,7 +268,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ]; } - file_put_contents($registerFile, '<?php return ' . var_export($newRegisterArray, TRUE) . ';'); + \file_put_contents($registerFile, '<?php return ' . \var_export($newRegisterArray, TRUE) . ';'); return $registerFile; } @@ -283,11 +283,11 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { $registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation); GeneralUtility::mkdir_deep($registerFolder); - $hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php'); + $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php'); $registerFile = GeneralUtility::getFileAbsFileName( $registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php' ); - if (file_exists($registerFile)) { + if (\file_exists($registerFile)) { \unlink($registerFile); } } @@ -340,13 +340,13 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { GeneralUtility::mkdir_deep($registerFolder); if ($filePath === '') { - $hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php'); + $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php'); $filePath = GeneralUtility::getFileAbsFileName( $registerFolder . '/' . $hashPrefix . '_' . $templateName . '.php' ); } - if (file_exists($filePath)) { + if (\file_exists($filePath)) { $success = \unlink($filePath); } else { return FALSE; @@ -383,7 +383,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { $registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation); GeneralUtility::mkdir_deep($registerFolder); - $hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php'); + $hashPrefix = \md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateName . '.php'); $filePath = GeneralUtility::getFileAbsFileName( $registerFolder . '/' . $hashPrefix . '_' . $templateName . '.php' );