From eab6303698707c98674fbd345051deb980487df4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Fri, 10 Sep 2021 10:49:42 +0200 Subject: [PATCH] Implement fluidemail --- .../Classes/Controller/AjaxFormController.php | 4 +- .../Classes/Service/EmailService.php | 80 ++++++------------- 2 files changed, 28 insertions(+), 56 deletions(-) diff --git a/public/typo3conf/ext/ep_events/Classes/Controller/AjaxFormController.php b/public/typo3conf/ext/ep_events/Classes/Controller/AjaxFormController.php index 1e4f52a5..a463d62a 100644 --- a/public/typo3conf/ext/ep_events/Classes/Controller/AjaxFormController.php +++ b/public/typo3conf/ext/ep_events/Classes/Controller/AjaxFormController.php @@ -64,7 +64,7 @@ class AjaxFormController extends ActionController 'toEmail' => $this->settings['inquiryToEmail'], 'toName' => $this->settings['inquiryToName'], 'subject' => 'Anfrage E&P Events', - 'templateName' => 'Email/Inquiry', + 'templateName' => 'Inquiry', 'variables' => [ 'inquiry' => $inquiry ] ]); return json_encode($response); @@ -82,7 +82,7 @@ class AjaxFormController extends ActionController 'toEmail' => $this->settings['inquiryToEmail'], 'toName' => $this->settings['inquiryToName'], 'subject' => 'Anfrage E&P Events', - 'templateName' => 'Email/Configurator', + 'templateName' => 'Configurator', 'variables' => [ 'inquiry' => $inquiry, 'config' => $this->configurationService->getConfiguratorConfig(), diff --git a/public/typo3conf/ext/ep_events/Classes/Service/EmailService.php b/public/typo3conf/ext/ep_events/Classes/Service/EmailService.php index dd925481..3936647d 100644 --- a/public/typo3conf/ext/ep_events/Classes/Service/EmailService.php +++ b/public/typo3conf/ext/ep_events/Classes/Service/EmailService.php @@ -28,20 +28,16 @@ namespace EP\EpEvents\Service; ***************************************************************/ use EP\EpEvents\Domain\Model\Inquiry; +use Symfony\Component\Mime\Address; +use TYPO3\CMS\Core\Mail\FluidEmail; +use TYPO3\CMS\Core\Mail\Mailer; use TYPO3\CMS\Core\Mail\MailMessage; 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 */ @@ -52,9 +48,8 @@ class EmailService implements SingletonInterface */ public function __construct(ConfigurationManagerInterface $configurationManager) { - $this->configurationManager = $configurationManager; $settings = GeneralUtility::removeDotsFromTS( - $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT) + $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT) ); $this->settings = $settings['plugin']['tx_epevents']; } @@ -66,6 +61,9 @@ class EmailService implements SingletonInterface */ public function send(array $options) { + /** @var Mailer $mailer */ + $mailer = GeneralUtility::makeInstance(Mailer::class); + $fromEmail = $this->settings['settings']['inquiryToEmail']; $fromName = $this->settings['settings']['inquiryToName']; $enableAutoreply = $this->settings['settings']['enableContactFormAutoreply']; @@ -77,20 +75,17 @@ class EmailService implements SingletonInterface $templateName = $options['templateName']; $variables = $options['variables']; - /** @var MailMessage $message */ - $message = GeneralUtility::makeInstance(MailMessage::class); - $message - ->setTo([$toEmail => $toName]) - ->setFrom([$fromEmail => $fromName]) - ->setSubject($subject) + /** @var FluidEmail $email */ + $email = GeneralUtility::makeInstance(FluidEmail::class); + $email + ->to(new Address($toEmail, $toName)) + ->from(new Address($fromEmail, $fromName)) + ->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($email); if ($enableAutoreply) { /** @var Inquiry $inquiry */ @@ -99,42 +94,19 @@ class EmailService implements SingletonInterface $toName = $inquiry->getName(); $templateName = 'Email/Autoreply'; - /** @var MailMessage $message */ - $message = GeneralUtility::makeInstance(MailMessage::class); - $message - ->setTo([$toEmail => $toName]) - ->setFrom([$fromEmail => $fromName]) - ->setSubject($autoreplySubject) + /** @var FluidEmail $email */ + $email = GeneralUtility::makeInstance(MailMessage::class); + $email + ->to(new Address($toEmail, $toName)) + ->from(new Address($fromEmail, $fromName)) + ->subject($autoreplySubject) + ->setTemplate($templateName) + ->assignMultiple($variables) ; - $view = $this->getView($templateName); - $view->assignMultiple($variables); - $message->setBody($view->render(), 'text/html'); - - $autoreplySent = $message->send(); + $mailer->send($email); } - 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; - } - }