From f0a07f83afcbc05df04bf6e9b5e1d007933dfa0a Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Mon, 3 Oct 2016 14:38:53 +0200
Subject: [PATCH] [TASK] Refactoring mail model

---
 Classes/Command/SendMailCommandController.php |  8 +++
 Classes/Domain/Model/Mail.php                 | 57 +++++++++++++++++++
 Classes/Service/MailTemplateService.php       |  3 +-
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/Classes/Command/SendMailCommandController.php b/Classes/Command/SendMailCommandController.php
index 1393ec8f..d2fcef46 100644
--- a/Classes/Command/SendMailCommandController.php
+++ b/Classes/Command/SendMailCommandController.php
@@ -28,6 +28,7 @@ namespace SGalinski\SgMail\Command;
 
 use SGalinski\SgMail\Domain\Model\Mail;
 use TYPO3\CMS\Core\Mail\MailMessage;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
 
 /**
@@ -59,7 +60,10 @@ class SendMailCommandController extends CommandController {
 
 			$fromAddress = $mailToSend->getFromAddress();
 			$toAddress = $mailToSend->getToAddress();
+			$ccAddresses = GeneralUtility::trimExplode(',', $mailToSend->getCcAddresses());
+			$bccAddresses = GeneralUtility::trimExplode(',', $mailToSend->getBccAddresses());
 			$mailSubject = $mailToSend->getMailSubject();
+			$fromName = $mailToSend->getFromName();
 			$mailBody = $mailToSend->getMailBody();
 
 			$mailToSend->setSent(TRUE);
@@ -72,7 +76,11 @@ class SendMailCommandController extends CommandController {
 			$mailMessage->setFrom($fromAddress);
 			$mailMessage->setTo($toAddress);
 			$mailMessage->setSubject($mailSubject);
+			$mailMessage->setCc($ccAddresses);
+			$mailMessage->setBcc($bccAddresses);
+			$mailMessage->setFrom($fromName);
 			$mailMessage->setBody($mailBody, 'text/html');
+
 			$mailMessage->send();
 		}
 
diff --git a/Classes/Domain/Model/Mail.php b/Classes/Domain/Model/Mail.php
index e0857ddb..fb5ff20b 100644
--- a/Classes/Domain/Model/Mail.php
+++ b/Classes/Domain/Model/Mail.php
@@ -69,6 +69,21 @@ class Mail extends AbstractEntity {
 	 */
 	protected $priority = 0;
 
+	/**
+	 * @var string
+	 */
+	protected $bccAddresses = '';
+
+	/**
+	 * @var string
+	 */
+	protected $ccAddresses = '';
+
+	/**
+	 * @var string
+	 */
+	protected $fromName = '';
+
 	/**
 	 * @return string
 	 */
@@ -158,4 +173,46 @@ class Mail extends AbstractEntity {
 	public function setPriority($priority) {
 		$this->priority = (int) $priority;
 	}
+
+	/**
+	 * @return string
+	 */
+	public function getBccAddresses() {
+		return $this->bccAddresses;
+	}
+
+	/**
+	 * @param string $bccAddresses
+	 */
+	public function setBccAddresses($bccAddresses) {
+		$this->bccAddresses = $bccAddresses;
+	}
+
+	/**
+	 * @return string
+	 */
+	public function getCcAddresses() {
+		return $this->ccAddresses;
+	}
+
+	/**
+	 * @param string $ccAddresses
+	 */
+	public function setCcAddresses($ccAddresses) {
+		$this->ccAddresses = $ccAddresses;
+	}
+
+	/**
+	 * @return string
+	 */
+	public function getFromName() {
+		return $this->fromName;
+	}
+
+	/**
+	 * @param string $fromName
+	 */
+	public function setFromName($fromName) {
+		$this->fromName = $fromName;
+	}
 }
diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php
index 725fea7e..11d61dd8 100644
--- a/Classes/Service/MailTemplateService.php
+++ b/Classes/Service/MailTemplateService.php
@@ -257,6 +257,8 @@ class MailTemplateService {
 		$mail->setMailSubject($this->subject);
 		$mail->setMailBody($emailBody);
 		$mail->setPriority($priority);
+		$mail->setBccAddresses(implode(',', $this->bccAddresses));
+		$mail->setCcAddresses(implode(',', $this->ccAddresses));
 
 		$mailRepository = $objectManager->get(MailRepository::class);
 		$mailRepository->add($mail);
@@ -384,5 +386,4 @@ class MailTemplateService {
 		return $this;
 	}
 
-
 }
-- 
GitLab