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 extends AbstractFinisher {

Torsten Oppermann
committed
private static $registerArray = [];
* Hook into Formhandler, sets config and form input
* gets called in the finisher of the typoscript
* redirects to process() function
* @param $tsConfig
*/
public function init($formInput, $tsConfig) {
$this->gp = $formInput;
foreach ($formInput as &$userInput) {
GeneralUtility::removeXSS($userInput);
}
$this->tsConfig = $tsConfig;
}
/**
* Redirect Formhandler input to send Email Function
* Needed to hook into Formhandler
*/
public function process() {
/** @var $translator LanguageService */
$translator = (TYPO3_MODE === 'FE' ? $GLOBALS['TSFE'] : $GLOBALS['LANG']);
$subject = $translator->sL($this->tsConfig['subject'], TRUE);
$toAddress = $this->tsConfig['to_address'];
$toAddress = $this->gp['email'];
}
self::sendEmail(
'en', $this->tsConfig['template_key'], $this->tsConfig['extension_key'], $toAddress,
$this->tsConfig['from_address'], $subject, $this->gp, $this->tsConfig['ignore_mail_queue']
return $this->gp;
}
/**
* Needed to hook into Formhandler
* simply returns true
*/
public function validateConfig() {
parent::validateConfig();
* @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);
}
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}
/**
* 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);