Skip to content
Snippets Groups Projects
FormsFinisher.php 3.23 KiB
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!
 ***************************************************************/

Torsten Oppermann's avatar
Torsten Oppermann committed
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 to one recipient
 */
class FormsFinisher extends AbstractFinisher {

	/**
	 * Executes this finisher
	 *
	 * @see AbstractFinisher::execute()
	 *
	 * @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();
		$formDefinition = $this->finisherContext->getFormRuntime()->getFormDefinition();
		$markers = [];
		foreach ($formValues as $identifier => $value) {
			$formElement = $formDefinition->getElementByIdentifier($identifier);
			if (!$formElement) {
				$markers[$identifier] = $value;
				continue;
			}
			$formElemenProperties = $formElement->getProperties();
			if (isset($formElemenProperties['markerName']) && \trim($formElemenProperties['markerName']) !== '') {
				$markers[\trim($formElemenProperties['markerName'])] = $value;
			} else {
				$markers[$identifier] = $value;
			}
		}
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
		$mailTemplateService = $objectManager->get(
			MailTemplateService::class, $this->parseOption('template'), $this->parseOption('extension'), $markers
		$mailTemplateService->setIgnoreMailQueue(FALSE);
		$mailTemplateService->setLanguage($GLOBALS['TSFE']->config['config']['language']);
		$mailTemplateService->setSubject($this->parseOption('subject'));
		$mailTemplateService->setToAddresses($this->parseOption('mailTo'));
		$mailTemplateService->setFromAddress($this->parseOption('mailFrom'));
		$mailTemplateService->setFromName($this->parseOption('userName'));
		$mailTemplateService->setReplyToAddress($this->parseOption('replyTo'));
		$mailTemplateService->setCcAddresses($this->parseOption('cc'));
		$mailTemplateService->setBccAddresses($this->parseOption('bcc'));
		$mailTemplateService->sendEmail();