Skip to content
Snippets Groups Projects
FormsFinisher.php 4.27 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;

Torsten Oppermann's avatar
Torsten Oppermann committed
 * 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'
	];
Torsten Oppermann's avatar
Torsten Oppermann committed
	 * 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();
Torsten Oppermann's avatar
Torsten Oppermann committed
		$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;
			}

			$formElementProperties = $formElement->getProperties();
			if (isset($formElementProperties['markerName']) && \trim($formElementProperties['markerName']) !== '') {
				$markers[\trim($formElementProperties['markerName'])] = $value;
			} else {
				$markers[$identifier] = $value;
			}
		}
		$templateName = trim((string) $this->parseOption('template'));
		if ($templateName === '') {
			$templateName = $formDefinition->getIdentifier();
		}

		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		$mailTemplateService = $objectManager->get(
			MailTemplateService::class, $templateName, 'sg_mail', $markers
		$ignoreMailQueue = (boolean) $this->parseOption('ignoreMailQueue');
		$mailTemplateService->setIgnoreMailQueue($ignoreMailQueue);
		$mailTemplateService->setLanguage($GLOBALS['TSFE']->config['config']['language']);
		$mailToAddresses = trim((string) $this->parseOption('mailTo'));
		if ($mailToAddresses !== '') {
			$mailTemplateService->setToAddresses($mailToAddresses);
		$fromAddress = trim((string) $this->parseOption('mailFrom'));
		if ($fromAddress !== '') {
			$mailTemplateService->setFromAddress($fromAddress);
		}

		$fromName = trim((string) $this->parseOption('userName'));
		if ($fromName !== '') {
			$mailTemplateService->setFromName($fromName);
		}

		$replyTo = trim((string) $this->parseOption('replyTo'));
		if ($replyTo !== '') {
			$mailTemplateService->setReplyToAddress($replyTo);
		}

		$ccAddresses = trim((string) $this->parseOption('cc'));
		if ($ccAddresses !== '') {
			$mailTemplateService->setCcAddresses($ccAddresses);
		}

		$bccAddresses = trim((string) $this->parseOption('bcc'));
		if ($bccAddresses !== '') {
			$mailTemplateService->setBccAddresses($bccAddresses);
		}
		$mailTemplateService->sendEmail();