From d707f363145b017fadd2b9a7c97fe929f2e2663a Mon Sep 17 00:00:00 2001
From: Georgi Mateev <gmateev@exactag.com>
Date: Mon, 2 Mar 2020 10:19:04 +0200
Subject: [PATCH] [BUGFIX] 1584 fix contact form placeholders

---
 Classes/Controller/NewsletterController.php |  5 +-
 Classes/Service/MailTemplateService.php     | 70 ++++++++++++++++-----
 2 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/Classes/Controller/NewsletterController.php b/Classes/Controller/NewsletterController.php
index b62ef538..adfe4a0c 100644
--- a/Classes/Controller/NewsletterController.php
+++ b/Classes/Controller/NewsletterController.php
@@ -376,6 +376,7 @@ class NewsletterController extends ActionController {
 	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
 	 * @throws \BadFunctionCallException
 	 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
+	 * @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
 	 */
 	public function sendTestMailAction(array $parameters = []) {
 		$arguments = [];
@@ -458,7 +459,7 @@ class NewsletterController extends ActionController {
 						'www' => '	www.example.com',
 					]
 				]);
-				$mailIsSend = $mailTemplateService->sendEmail(TRUE);
+				$mailIsSend = $mailTemplateService->sendEmail(TRUE, TRUE);
 			}
 
 		} else {
@@ -489,7 +490,7 @@ class NewsletterController extends ActionController {
 						$mailTemplateService->setMarkers(['user' => $recipient]);
 						// no real error handling here, one must check the MailQueue
 
-						$mailTemplateService->sendEmail(FALSE);
+						$mailTemplateService->sendEmail(FALSE, TRUE);
 					} catch (\Exception $e) {
 						// Invalid email address could not be loaded to queue
 						$errorRecipients[] = $recipient['uid'] . ' - '
diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php
index 57e1f152..4f7ab502 100644
--- a/Classes/Service/MailTemplateService.php
+++ b/Classes/Service/MailTemplateService.php
@@ -936,9 +936,11 @@ class MailTemplateService {
 	 * @param RegisterService $registerService
 	 * @param string $defaultTemplateContent
 	 * @param int $siteRootId
+	 * @param boolean $isNewsletter
 	 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
 	 */
-	protected function extractValuesForMail($template, RegisterService $registerService, $defaultTemplateContent, $siteRootId): void {
+	protected function extractValuesForMail($template, RegisterService $registerService, $defaultTemplateContent,
+		$siteRootId, $isNewsletter): void {
 		/** @var StandaloneView $emailView */
 		$emailView = $this->objectManager->get(StandaloneView::class);
 		$emailView->assignMultiple($this->markers);
@@ -981,35 +983,54 @@ class MailTemplateService {
 		//TODO: is this object even in use somewhere?
 		$this->mailMessage->setSubject($subject);
 
-		if ($this->fromAddress === $this->defaultFromAddress) {
+		$fromMail = '';
+		if (!$isNewsletter && $template !== NULL) {
+			// if it's a non-newsletter email with a template:
+			// get the values explicitly from the template and parse the markers
+			$fromMail = $this->parseMarkers($template->getFromMail(), $emailView);
+		} elseif ($this->fromAddress === $this->defaultFromAddress) {
+			// in other cases (newsletter or no template) - check for overwritten values
 			$fromMail = $this->parseMarkers(
 				empty($this->overwrittenFromMail) && $template ? $template->getFromMail() : $this->overwrittenFromMail,
 				$emailView
 			);
-			if ($fromMail) { // we don't want to override the default in that case
-				$this->setFromAddress($fromMail);
-			}
+		}
+		if ($fromMail) { // we don't want to override the default in that case
+			$this->setFromAddress($fromMail);
 		}
 
-		if ($this->fromName === $this->defaultFromName) {
+		$fromName = '';
+		if (!$isNewsletter && $template !== NULL) {
+			$fromName = $this->parseMarkers($template->getFromName(), $emailView);
+		} else if ($this->fromName === $this->defaultFromName) {
 			$fromName = $this->parseMarkers(
-				(empty($this->overwrittenFromName) && $template ? $template->getFromName() : $this->overwrittenFromName),
+				(empty($this->overwrittenFromName) && $template ? $template->getFromName(
+				) : $this->overwrittenFromName),
 				$emailView
 			);
-			if ($fromName) { // we don't want to override the default if this value is empty here
-				$this->setFromName($fromName);
-			}
+		}
+		if ($fromName) { // we don't want to override the default if this value is empty here
+			$this->setFromName($fromName);
 		}
 
-		if ($this->replyToAddress === '') {
+		if (!$isNewsletter && $template !== NULL) {
+			$replyTo = $this->parseMarkers($template->getReplyTo(), $emailView);
+			$this->setReplyToAddress($replyTo);
+		} else if ($this->replyToAddress === '') {
 			$replyTo = $this->parseMarkers(
-				(empty($this->overwrittenReplyTo) && $template ? $template->getReplyTo() : $this->overwrittenReplyTo),
+				(empty($this->overwrittenReplyTo) && $template ? $template->getReplyTo(
+				) : $this->overwrittenReplyTo),
 				$emailView
 			);
 			$this->setReplyToAddress($replyTo);
 		}
 
-		if (empty($this->ccAddresses)) {
+		if (!$isNewsletter && $template !== NULL) {
+			$cc = $this->parseMarkers($template->getCc(), $emailView);
+			if ($cc) {
+				$this->setCcAddresses($cc);
+			}
+		} else if (empty($this->ccAddresses)) {
 			$cc = $this->parseMarkers(
 				(empty($this->overwrittenCc) && $template ? $template->getCc() : $this->overwrittenCc),
 				$emailView
@@ -1017,7 +1038,12 @@ class MailTemplateService {
 			$this->setCcAddresses($cc);
 		}
 
-		if (empty($this->bccAddresses)) {
+		if (!$isNewsletter && $template !== NULL) {
+			$bcc = $this->parseMarkers($template->getBcc(), $emailView);
+			if ($bcc) {
+				$this->setBccAddresses($bcc);
+			}
+		} else if (empty($this->bccAddresses)) {
 			$bcc = $this->parseMarkers(
 				(empty($this->overwrittenBcc) && $template ? $template->getBcc() : $this->overwrittenBcc),
 				$emailView
@@ -1025,7 +1051,12 @@ class MailTemplateService {
 			$this->setBccAddresses($bcc);
 		}
 
-		if ($this->toAddresses === '') {
+		if (!$isNewsletter && $template !== NULL) {
+			$toAddress = $this->parseMarkers($template->getToAddress(), $emailView);
+			if ($toAddress) {
+				$this->setToAddresses($toAddress);
+			}
+		} else if ($this->toAddresses === '') {
 			$toAddress = $this->parseMarkers(
 				(empty($this->overwrittenToAddresses) && $template ? $template->getToAddress() : $this->overwrittenToAddresses),
 				$emailView
@@ -1068,7 +1099,7 @@ class MailTemplateService {
 	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
 	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
 	 */
-	public function sendEmail($isPreview = FALSE): bool {
+	public function sendEmail($isPreview = FALSE, $isNewsletter = FALSE): bool {
 		if ($isPreview) { //TODO: remove this from here
 			$this->setIgnoreMailQueue(TRUE);
 		}
@@ -1094,7 +1125,12 @@ class MailTemplateService {
 			$this->setToAddresses(\trim($template->getToAddress()));
 		}
 
-		$this->extractValuesForMail($template, $registerService, $defaultTemplateContent, $siteRootId);
+		$this->extractValuesForMail($template, $registerService, $defaultTemplateContent, $siteRootId, $isNewsletter);
+
+		// Fallback empty recipient address to the default sender address
+		if (!$this->mailMessage->getTo()) {
+			$this->mailMessage->setTo($this->defaultFromAddress);
+		}
 
 		$mail = $this->addMailToMailQueue(
 			$this->extensionKey, $this->templateName, $this->getSubjectToSend(), $this->getMailBodyToSend(),
-- 
GitLab