Newer
Older
<?php
declare(strict_types=1);
namespace SGalinski\SgMail\Finisher\Forms;
/***************************************************************
* 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!
***************************************************************/
use SGalinski\SgMail\Service\MailTemplateService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
* This finisher sends an email via sg_mail after form submission and enables customization of mail markers
*/
class FormsFinisher extends AbstractFinisher {
/**
* This contains type names of fields we dont care about in sgmail
*
* @var array
*/
const IGNORE_FIELDS = [
'Honeypot', 'Hidden'
];
* Send email with the sgmail api to one or more recipients
* overwrites the mail markers with custom identifiers if provided
*
* @throws \InvalidArgumentException
* @throws \BadFunctionCallException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
*/
protected function executeInternal() {
$formValues = $this->finisherContext->getFormValues();
$formRuntime = $this->finisherContext->getFormRuntime();
// check if all the necessary objects exist
if ($formRuntime !== NULL) {
$formDefinition = $formRuntime->getFormDefinition();
} else {
return;
}
if ($formRuntime === NULL) {
return;
}
$markers = [];
foreach ($formValues as $identifier => $value) {
$formElement = $formDefinition->getElementByIdentifier($identifier);
if (!$formElement) {
continue;
}
$type = $formElement->getType();
if (\in_array($type, self::IGNORE_FIELDS, TRUE)) {
continue;
}
Stefan Galinski
committed
$formElementProperties = $formElement->getProperties();
if (isset($formElementProperties['markerName']) && \trim($formElementProperties['markerName']) !== '') {
$markers[\trim($formElementProperties['markerName'])] = $value;
} else {
$markers[$identifier] = $value;
}
}

Torsten Oppermann
committed
Stefan Galinski
committed
$templateName = trim((string) $this->parseOption('template'));
if ($templateName === '') {

Torsten Oppermann
committed
$templateName = $formDefinition->getIdentifier();
}
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$mailTemplateService = $objectManager->get(
MailTemplateService::class, $templateName, 'sg_mail', $markers
Stefan Galinski
committed
$ignoreMailQueue = (boolean) $this->parseOption('ignoreMailQueue');
$mailTemplateService->setIgnoreMailQueue($ignoreMailQueue);
$mailTemplateService->setLanguage($GLOBALS['TSFE']->config['config']['language']);
Stefan Galinski
committed
$mailToAddresses = trim((string) $this->parseOption('mailTo'));
if ($mailToAddresses !== '') {
$mailTemplateService->setToAddresses($mailToAddresses);
}
Stefan Galinski
committed
$fromAddress = trim((string) $this->parseOption('mailFrom'));
if ($fromAddress !== '') {
$mailTemplateService->setFromAddress($fromAddress);
}
Stefan Galinski
committed
$fromName = trim((string) $this->parseOption('userName'));
if ($fromName !== '') {
$mailTemplateService->setFromName($fromName);
}
Stefan Galinski
committed
$replyTo = trim((string) $this->parseOption('replyTo'));
if ($replyTo !== '') {
$mailTemplateService->setReplyToAddress($replyTo);
}
Stefan Galinski
committed
$ccAddresses = trim((string) $this->parseOption('cc'));
if ($ccAddresses !== '') {
$mailTemplateService->setCcAddresses($ccAddresses);
}
Stefan Galinski
committed
$bccAddresses = trim((string) $this->parseOption('bcc'));
if ($bccAddresses !== '') {
$mailTemplateService->setBccAddresses($bccAddresses);
}
$mailTemplateService->sendEmail();