Newer
Older
<?php
namespace SGalinski\SgMail\Service;
use SGalinski\SgMail\Domain\Model\Mail;
use SGalinski\SgMail\Domain\Repository\MailRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Lang\LanguageService;
use Typoheads\Formhandler\Finisher\AbstractFinisher;

Torsten Oppermann
committed
/***************************************************************
* 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 {

Torsten Oppermann
committed
private static $registerArray = [];
* @param string $extension
* @param string $templateName
* @param string $templatePath
* @param string $description

Torsten Oppermann
committed
* @param array $marker
* @return string
public static function registerTemplate($extension, $templateName, $templatePath, $description, array $marker) {
MailTemplateService::$registerArray[$extension][$templateName] = [
'templatePath' => $templatePath,

Torsten Oppermann
committed
'description' => $description,
'marker' => $marker,

Torsten Oppermann
committed
'extension' => $extension,
'templateName' => $templateName

Torsten Oppermann
committed
];
$defaultLangFile = $templatePath . 'en.sg_mail.locallang.html';
if (!file_exists($defaultLangFile)) {

Torsten Oppermann
committed
file_put_contents($defaultLangFile, $langFileContent);

Torsten Oppermann
committed
}

Torsten Oppermann
committed
* @return array

Torsten Oppermann
committed
public static function getRegisterArray() {
return self::$registerArray;
* @param string $language
* @param string $templateKey
* @param string $extensionKey
* @param string $toAddress
* @param string $fromAddress
* @param string $subject
*/
public static function sendEmail(
$language, $templateKey, $extensionKey, $toAddress, $fromAddress, $subject,
) {
// 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);
}
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
}
/**
* 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);