Skip to content
Snippets Groups Projects
FormsFinisher.php 4.78 KiB
Newer Older
<?php
declare(strict_types=1);

namespace SGalinski\SgMail\Finisher\Forms;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use SGalinski\SgMail\Service\MailTemplateService;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
use TYPO3\CMS\Form\Service\TranslationService;
use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;

/***************************************************************
 *  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!
 ***************************************************************/

/**
 * This finisher sends an email to one recipient
 *
 * Options:
 *
 * - templatePathAndFilename (mandatory): Template path and filename for the mail body
 * - layoutRootPath: root path for the layouts
 * - partialRootPath: root path for the partials
 * - variables: associative array of variables which are available inside the Fluid template
 *
 * The following options control the mail sending. In all of them, placeholders in the form
 * of {...} are replaced with the corresponding form value; i.e. {email} as recipientAddress
 * makes the recipient address configurable.
 *
 * - subject (mandatory): Subject of the email
 * - recipientAddress (mandatory): Email address of the recipient
 * - recipientName: Human-readable name of the recipient
 * - senderAddress (mandatory): Email address of the sender
 * - senderName: Human-readable name of the sender
 * - replyToAddress: Email address of to be used as reply-to email (use multiple addresses with an array)
 * - carbonCopyAddress: Email address of the copy recipient (use multiple addresses with an array)
 * - blindCarbonCopyAddress: Email address of the blind copy recipient (use multiple addresses with an array)
 * - format: format of the email (one of the FORMAT_* constants). By default mails are sent as HTML
 *
 * Scope: frontend
 */
class FormsFinisher extends AbstractFinisher {
	const FORMAT_PLAINTEXT = 'plaintext';
	const FORMAT_HTML = 'html';

	/**
	 * @var array
	 */
	protected $defaultOptions = [
		'recipientName' => '',
		'senderName' => '',
		'format' => self::FORMAT_HTML,
		'attachUploads' => TRUE
	];

	/**
	 * Executes this finisher
	 *
	 * @see AbstractFinisher::execute()
	 *
	 * @throws \InvalidArgumentException
	 * @throws FinisherException
	 */
	protected function executeInternal() {

		$formValues = $this->finisherContext->getFormValues();
		$formRuntime = $this->finisherContext->getFormRuntime();
		debug($formRuntime);
		debug($formValues);

		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
		$mailTemplateService = $objectManager->get(MailTemplateService::class);

		// parse options
		$extensionKey = $this->parseOption('extensionKey');
		$templateName = $this->parseOption('templateName');
		$recipientAddress = $this->parseOption('recipientAddress');
		$senderAddress = $this->parseOption('senderAddress');
		$senderName = $this->parseOption('senderName');

		$mailTemplateService->setExtensionKey($extensionKey);
		$mailTemplateService->setTemplateName($templateName);
		$mailTemplateService->setToAddresses($recipientAddress);
		$mailTemplateService->setFromAddress($senderAddress);
		$mailTemplateService->setFromName($senderName);
		$mailTemplateService->setLanguage($GLOBALS['TSFE']->config['config']['language']);
	}
}