From a2b5fb6b3f19c1a27770ba9c182dae6797008626 Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Fri, 30 Sep 2016 10:41:29 +0200
Subject: [PATCH] [TASK] Refactoring Test Mail & Controller for DB
 implementation. Fixed some bugs

---
 Classes/Controller/MailController.php   |  2 ++
 Classes/Service/MailTemplateService.php | 46 ++++++++++++++++++++-----
 Configuration/TypoScript/setup.ts       |  2 +-
 3 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/Classes/Controller/MailController.php b/Classes/Controller/MailController.php
index 4f514568..836850ec 100644
--- a/Classes/Controller/MailController.php
+++ b/Classes/Controller/MailController.php
@@ -98,6 +98,8 @@ class MailController extends ActionController {
 				);
 			}
 
+			$this->view->assign('selectedLanguageLeft', $this->settings['templateDefaultLanguage']);
+			$this->view->assign('selectedLanguageRight', $this->settings['templateDefaultLanguage']);
 			$this->view->assign('selectedTemplateKey', $secondLevelFirstEntry);
 			$this->view->assign('selectedExtensionKey', $firstEntry);
 		}
diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php
index d9a2fdc9..2929d941 100644
--- a/Classes/Service/MailTemplateService.php
+++ b/Classes/Service/MailTemplateService.php
@@ -3,7 +3,9 @@
 namespace SGalinski\SgMail\Service;
 
 use SGalinski\SgMail\Domain\Model\Mail;
+use SGalinski\SgMail\Domain\Model\Template;
 use SGalinski\SgMail\Domain\Repository\MailRepository;
+use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Mail\MailMessage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Object\ObjectManager;
@@ -110,6 +112,12 @@ class MailTemplateService {
 	 */
 	private $bccAddresses = [];
 
+	/**
+	 * @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
+	 * @inject
+	 */
+	protected $templateRepository = NULL;
+
 	/**
 	 * MailTemplateService constructor.
 	 */
@@ -125,9 +133,27 @@ class MailTemplateService {
 		$this->mailMessage->setFrom($this->fromAddress);
 
 		$this->bccAddresses = GeneralUtility::trimExplode(',', $this->tsSettings['mail']['default']['bcc']);
-		$this->bccAddresses = GeneralUtility::trimExplode(',', $this->tsSettings['mail']['default']['cc']);
-		$this->mailMessage->setBcc($this->bccAddresses);
-		$this->mailMessage->setBcc($this->ccAddresses);
+		$this->ccAddresses = GeneralUtility::trimExplode(',', $this->tsSettings['mail']['default']['cc']);
+
+		foreach ($this->bccAddresses as $index => $email) {
+			if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
+				unset($this->bccAddresses[$index]);
+			}
+		}
+
+		foreach ($this->ccAddresses as $index => $email) {
+			if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
+				unset($this->ccAddresses[$index]);
+			}
+		}
+
+		if (sizeof($this->bccAddresses) > 0) {
+			$this->mailMessage->setBcc($this->bccAddresses);
+		}
+
+		if (sizeof($this->ccAddresses) > 0) {
+			$this->mailMessage->setCc($this->ccAddresses);
+		}
 	}
 
 	/**
@@ -164,13 +190,16 @@ class MailTemplateService {
 	 * Send the Email
 	 */
 	public function sendEmail() {
-		$templateEntry = self::$registerArray[$this->extensionKey][$this->templateName];
 
 		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
+		/** @var StandaloneView $emailView */
 		$emailView = $objectManager->get(StandaloneView::class);
-		$emailView->setTemplatePathAndFilename(
-			$templateEntry['templatePath'] . $this->language . '.sg_mail.locallang.html'
-		);
+
+		/** @var Template $template */
+		$template = $this->templateRepository->findTemplate(
+			$this->extensionKey, $this->templateName, $this->language
+		)->getFirst();
+		$emailView->setTemplateSource($template->getContent());
 		$emailView->assignMultiple($this->markers);
 		$emailBody = $emailView->render();
 
@@ -179,7 +208,7 @@ class MailTemplateService {
 			$emailBody = nl2br($emailBody);
 			$emailBody = preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $emailBody);
 
-			$this->mailMessage->setSubject($this->subject);
+			$this->mailMessage->setSubject($template->getSubject());
 			$this->mailMessage->setBody($emailBody, 'text/html');
 			$this->mailMessage->send();
 		} else {
@@ -327,5 +356,4 @@ class MailTemplateService {
 		return $this;
 	}
 
-
 }
diff --git a/Configuration/TypoScript/setup.ts b/Configuration/TypoScript/setup.ts
index 1ecdd616..5434e6a5 100644
--- a/Configuration/TypoScript/setup.ts
+++ b/Configuration/TypoScript/setup.ts
@@ -10,7 +10,7 @@ module.tx_sgmail {
 			test.from = info@test.de
 			test.subject = This is just a Test E-Mail
 
-			default.from =
+			default.from = torsten@sgalinski.de
 			default.bcc =
 			default.cc =
 		}
-- 
GitLab