Skip to content
Snippets Groups Projects
MailTemplateService.php 4.71 KiB
Newer Older
<?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;
use TYPO3\CMS\Lang\LanguageService;
use Typoheads\Formhandler\Finisher\AbstractFinisher;
/***************************************************************
 *  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!
 ***************************************************************/
class MailTemplateService {
Torsten Oppermann's avatar
Torsten Oppermann committed
	/**
	 * register a template with sg_mail
Fabian Galinski's avatar
Fabian Galinski committed
	 *
	 * @param string $extension
	 * @param string $templateName
	 * @param string $templatePath
	 * @param string $description
Torsten Oppermann's avatar
Torsten Oppermann committed
	 */
	public static function registerTemplate($extension, $templateName, $templatePath, $description, array $marker) {

		MailTemplateService::$registerArray[$extension][$templateName] = [
			'templatePath' => $templatePath,
			'description' => $description,
			'marker' => $marker,
			'extension' => $extension,
			'templateName' => $templateName
		$defaultLangFile = $templatePath . 'en.sg_mail.locallang.html';
		if (!file_exists($defaultLangFile)) {
			$langFileContent = '';
			file_put_contents($defaultLangFile, $langFileContent);
	 * Get all registered templates
Fabian Galinski's avatar
Fabian Galinski committed
	 *
	public static function getRegisterArray() {
		return self::$registerArray;
	 * send an Email
Fabian Galinski's avatar
Fabian Galinski committed
	 *
	 * @param string $language
	 * @param string $templateKey
	 * @param string $extensionKey
	 * @param array $toAddress
	 * @param array $fromAddress
	 * @param string $subject
	 * @param array $content
	 * @param boolean $ignoreMailQueue
	 */
	public static function sendEmail(
		$language, $templateKey, $extensionKey, array $toAddress, array $fromAddress, $subject,
		$ignoreMailQueue = FALSE
		// @TODO get language dynamically
		!isset($language) ? $language = 'de' : null;

		// 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);