Commit fefdc1a1 authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] New MailTemplateService API

parent 78f816d7
......@@ -8,8 +8,6 @@ use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Lang\LanguageService;
use Typoheads\Formhandler\Finisher\AbstractFinisher;
/***************************************************************
* Copyright notice
......@@ -45,6 +43,69 @@ class MailTemplateService {
*/
private static $registerArray = [];
/**
* @var array to
*/
private $to = [];
/**
* @var string from
*/
private $from;
/**
* @var string $cc
*/
private $cc;
/**
* @var string replyTo
*/
private $replyTo;
/**
* @var string language
*/
private $language;
/**
* @var boolean ignoreMailQueue
*/
private $ignoreMailQueue = TRUE;
/**
* @var \TYPO3\CMS\Core\Mail\MailMessage $mailMessage
*/
private $mailMessage;
/**
* @var string templateName
*/
private $templateName;
/**
* @var string extensionKey
*/
private $extensionKey;
/**
* @var array content
*/
private $content = [];
/**
* @var string subject
*/
private $subject;
/**
* MailTemplateService constructor.
*/
public function __construct() {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$this->mailMessage = $objectManager->get(MailMessage::class);
}
/**
* register a template with sg_mail
*
......@@ -82,61 +143,36 @@ class MailTemplateService {
}
/**
* send an Email
*
* @param string $language
* @param string $templateKey
* @param string $extensionKey
* @param array $toAddress
* @param array $fromAddress
* @param string $subject
* @param array $content
* @param boolean $ignoreMailQueue
* send the Email
*/
public static function sendEmail(
$language, $templateKey, $extensionKey, array $toAddress, array $fromAddress, $subject,
array $content,
$ignoreMailQueue = FALSE
) {
// @TODO get language dynamically
!isset($language) ? $language = 'de' : null;
// Set Email Content
public function sendEmail() {
$registerArray = MailTemplateService::getRegisterArray();
$templateEntry = $registerArray[$extensionKey][$templateKey];
$templateEntry = $registerArray[$this->extensionKey][$this->templateName];
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$emailView = $objectManager->get(StandaloneView::class);
$emailView->setTemplatePathAndFilename($templateEntry['templatePath'] . $language . '.sg_mail.locallang.html');
$emailView->assignMultiple($content);
$emailView->setTemplatePathAndFilename(
$templateEntry['templatePath'] . $this->language . '.sg_mail.locallang.html'
);
$emailView->assignMultiple($this->content);
$emailBody = $emailView->render();
if ($ignoreMailQueue) {
$mailMessage = $objectManager->get(MailMessage::class);
$mailMessage->setFrom($fromAddress);
$mailMessage->setTo($toAddress);
$mailMessage->setSubject($subject);
$mailMessage->setBody($emailBody, 'text/html');
$mailMessage->send();
if ($this->ignoreMailQueue) {
$this->mailMessage->setSubject($this->subject);
$this->mailMessage->setBody($emailBody, 'text/html');
$this->mailMessage->send();
} else {
// @todo evtl. sollten hier noch die Parameter $priority und $pid mit gegeben werden.
self::addMailToMailQueue($fromAddress, $toAddress, $subject, $emailBody);
$this->addMailToMailQueue($emailBody);
}
}
/**
* Adds a new mail to the mail queue. This will have the given parameters.
* Adds a new mail to the mail queue.
*
* @param string $fromAddress
* @param string $toAddress
* @param string $mailSubject
* @param string $mailBody
* @param int $priority
* @param int $pid
*/
public static function addMailToMailQueue(
$fromAddress, $toAddress, $mailSubject, $mailBody = '', $priority = Mail::PRIORITY_LOWEST, $pid = 0
) {
* @param string $emailBody
*/
private function addMailToMailQueue($emailBody, $priority = Mail::PRIORITY_LOWEST, $pid = 0) {
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
......@@ -146,13 +182,107 @@ class MailTemplateService {
$mail = $objectManager->get(Mail::class);
$mail->setPid($pid);
$mail->setFromAddress($fromAddress);
$mail->setToAddress($toAddress);
$mail->setMailSubject($mailSubject);
$mail->setMailBody($mailBody);
$mail->setFromAddress($this->from);
$mail->setToAddress($this->to);
$mail->setMailSubject($this->subject);
$mail->setMailBody($emailBody);
$mail->setPriority($priority);
$mailRepository = $objectManager->get(MailRepository::class);
$mailRepository->add($mail);
}
/**
* @param array $registerArray
* @return MailTemplateService
*/
public static function setRegisterArray($registerArray) {
self::$registerArray = $registerArray;
}
/**
* @param array | string $to
* @return MailTemplateService
*/
public function setTo($to) {
$this->to = $to;
$this->mailMessage->setTo($to);
return $this;
}
/**
* @param array | string $from
* @return MailTemplateService
*/
public function setFrom($from) {
$this->from = $from;
$this->mailMessage->setFrom($from);
return $this;
}
/**
* @param array | string $cc
* @return MailTemplateService
*/
public function setCc($cc) {
$this->cc = $cc;
$this->mailMessage->setCc($cc);
return $this;
}
/**
* @param array | string $replyTo
* @return MailTemplateService
*/
public function setReplyTo($replyTo) {
$this->replyTo = $replyTo;
$this->mailMessage->setReplyTo($replyTo);
return $this;
}
/**
* @param string $language
* @return MailTemplateService
*/
public function setLanguage($language) {
$this->language = $language;
return $this;
}
/**
* @param boolean $ignoreMailQueue
* @return MailTemplateService
*/
public function setIgnoreMailQueue($ignoreMailQueue) {
$this->ignoreMailQueue = $ignoreMailQueue;
return $this;
}
/**
* @param string $templateName
* @return MailTemplateService
*/
public function setTemplateName($templateName) {
$this->templateName = $templateName;
return $this;
}
/**
* @param string $extensionKey
* @return MailTemplateService
*/
public function setExtensionKey($extensionKey) {
$this->extensionKey = $extensionKey;
return $this;
}
/**
* @param string $subject
* @return MailTemplateService
*/
public function setSubject($subject) {
$this->subject = $subject;
$this->mailMessage->setSubject($subject);
return $this;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment