Implement fluidemail

This commit is contained in:
Björn Fromme
2021-09-10 10:38:49 +02:00
parent 481f1d132b
commit 27c070aed1
7 changed files with 49 additions and 110 deletions
@@ -26,33 +26,24 @@ namespace EP\EpTheme\Service;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Mail\MailMessage;
use Symfony\Component\Mime\Address;
use TYPO3\CMS\Core\Mail\FluidEmail;
use TYPO3\CMS\Core\Mail\Mailer;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Fluid\View\StandaloneView;
class EmailService implements SingletonInterface
{
/**
* @var ConfigurationManagerInterface
*/
protected $configurationManager;
/**
* @var array
*/
protected $settings;
/**
* @param ConfigurationManagerInterface $manager
*/
public function injectConfigurationManager(ConfigurationManagerInterface $manager)
public function __construct(ConfigurationManagerInterface $manager)
{
$this->configurationManager = $manager;
$settings = GeneralUtility::removeDotsFromTS(
$this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
$manager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
);
$this->settings = $settings['plugin']['tx_eptheme'];
}
@@ -63,72 +54,49 @@ class EmailService implements SingletonInterface
* @param string $subject
* @param string $templateName
* @param array $variables
* @return bool
*/
public function send($toEmail, $toName, $subject, $templateName, array $variables = [])
{
/** @var Mailer $mailer */
$mailer = GeneralUtility::makeInstance(Mailer::class);
$fromEmail = $this->settings['settings']['contactFormToEmail'];
$fromName = $this->settings['settings']['contactFormToName'];
$enableAutoreply = $this->settings['settings']['enableContactFormAutoreply'];
$enableAutoreply = (bool)$this->settings['settings']['enableContactFormAutoreply'];
$autoreplySubject = $this->settings['settings']['contactFormAutoreplySubject'];
/** @var MailMessage $message */
$message = GeneralUtility::makeInstance(MailMessage::class);
/** @var FluidEmail $message */
$message = GeneralUtility::makeInstance(FluidEmail::class);
$message
->setTo([$toEmail => $toName])
->setFrom([$fromEmail => $fromName])
->setSubject($subject)
->to(new Address($toEmail, $toName))
->from(new Address($fromEmail, $fromName))
->format('html')
->subject($subject)
->setTemplate($templateName)
->assignMultiple($variables)
;
$view = $this->getView($templateName);
$view->assignMultiple($variables);
$message->setBody($view->render(), 'text/html');
$messageSent = $message->send();
$autoreplySent = false;
$mailer->send($message);
if ($enableAutoreply) {
$contactForm = $variables['contactForm'];
$toEmail = $contactForm->getEmail();
$toName = $contactForm->getName();
$templateName = 'Email/Autoreply';
$templateName = 'Autoreply';
/** @var MailMessage $message */
$message = GeneralUtility::makeInstance(MailMessage::class);
/** @var FluidEmail $message */
$message = GeneralUtility::makeInstance(FluidEmail::class);
$message
->setTo([$toEmail => $toName])
->setFrom([$fromEmail => $fromName])
->setSubject($autoreplySubject)
->to(new Address($toEmail, $toName))
->from(new Address($fromEmail, $fromName))
->format('html')
->subject($autoreplySubject)
->setTemplate($templateName)
->assignMultiple($variables)
;
$view = $this->getView($templateName);
$view->assignMultiple($variables);
$message->setBody($view->render(), 'text/html');
$autoreplySent = $message->send();
$mailer->send($message);
}
return $messageSent && $autoreplySent;
return true;
}
/**
* @param string $templateName
* @param string $format
* @return StandaloneView
*/
protected function getView($templateName, $format = 'html')
{
/** @var StandaloneView $view */
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setFormat($format);
$view->getRequest()->setControllerExtensionName('ep_events');
$view->setTemplateRootPaths($this->settings['view']['templateRootPaths']);
$view->setLayoutRootPaths($this->settings['view']['layoutRootPaths']);
$view->setPartialRootPaths($this->settings['view']['partialRootPaths']);
$view->setTemplate($templateName);
return $view;
}
}