<?php namespace SGalinski\SgMail\Service; use SGalinski\SgMail\Domain\Model\Mail; use SGalinski\SgMail\Domain\Repository\MailRepository; use TYPO3\CMS\Core\Mail\MailMessage; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Object\ObjectManager; use TYPO3\CMS\Fluid\View\StandaloneView; /*************************************************************** * Copyright notice * * (c) sgalinski Internet Services (https://www.sgalinski.de) * * All rights reserved * * This script is part of the TYPO3 project. The TYPO3 project is * free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * The GNU General Public License can be found at * http://www.gnu.org/copyleft/gpl.html. * * This script is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ /** * MailTemplateService */ class MailTemplateService { /** * @var array */ private static $registerArray = []; /** * Hook into Formhandler * gets called in the finisher of the typoscript * * @param $gp * @param $tsConfig */ public function init($gp, $tsConfig) { self::sendEmail( NULL, $tsConfig['template_key'], $tsConfig['extension_key'], $tsConfig['to_address'], $tsConfig['from_address'], $tsConfig['subject'], $gp, $tsConfig['ignore_mail_queue'] ); } /** * register a template with sg_mail * * @param string $extension * @param string $templateName * @param string $templatePath * @param string $description * @param array $marker * @return string */ public static function registerTemplate($extension, $templateName, $templatePath, $description, array $marker) { MailTemplateService::$registerArray[$extension][$extension . '_' . $templateName] = [ 'templatePath' => $templatePath, 'description' => $description, 'marker' => $marker, 'extension' => $extension, 'templateName' => $templateName ]; $defaultLangFile = $templatePath . 'en.sg_mail.locallang.html'; if (!file_exists($defaultLangFile)) { $langFileContent = 'TEST'; file_put_contents($defaultLangFile, $langFileContent); } } /** * Get all registered templates * * @return array */ public static function getRegisterArray() { return self::$registerArray; } /** * send an Email * * @param string $language * @param string $templateKey * @param string $extensionKey * @param string $toAddress * @param string $fromAddress * @param string $subject * @param array $content * @param boolean $ignoreMailQueue */ public static function sendEmail( $language = 'en', $templateKey, $extensionKey, $toAddress, $fromAddress, $subject, array $content = [], $ignoreMailQueue = FALSE ) { // Set Email Content $registerArray = MailTemplateService::getRegisterArray(); $templateEntry = $registerArray[$extensionKey][$templateKey]; $objectManager = GeneralUtility::makeInstance(ObjectManager::class); $emailView = $objectManager->get(StandaloneView::class); $emailView->setTemplatePathAndFilename($templateEntry['templatePath'] . $language . '.sg_mail.locallang.html'); $emailView->assignMultiple($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(); } else { // @todo evtl. sollten hier noch die Parameter $priority und $pid mit gegeben werden. self::addMailToMailQueue($fromAddress, $toAddress, $subject, $emailBody); } } /** * Adds a new mail to the mail queue. This will have the given parameters. * * @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 ) { /** @var ObjectManager $objectManager */ $objectManager = GeneralUtility::makeInstance(ObjectManager::class); if ($pid <= 0 && isset($GLOBALS['TSFE'])) { $pid = (int) $GLOBALS['TSFE']->id; } $mail = $objectManager->get(Mail::class); $mail->setPid($pid); $mail->setFromAddress($fromAddress); $mail->setToAddress($toAddress); $mail->setMailSubject($mailSubject); $mail->setMailBody($mailBody); $mail->setPriority($priority); $mailRepository = $objectManager->get(MailRepository::class); $mailRepository->add($mail); } }