‪TYPO3CMS  9.5
EmailFinisher.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
26 
54 {
55  const ‪FORMAT_PLAINTEXT = 'plaintext';
56  const ‪FORMAT_HTML = 'html';
57 
61  protected ‪$defaultOptions = [
62  'recipientName' => '',
63  'senderName' => '',
64  'format' => ‪self::FORMAT_HTML,
65  'attachUploads' => true
66  ];
67 
74  protected function ‪executeInternal()
75  {
76  $formRuntime = $this->finisherContext->getFormRuntime();
77  $standaloneView = $this->‪initializeStandaloneView($formRuntime);
78 
79  $translationService = ‪TranslationService::getInstance();
80  if (isset($this->options['translation']['language']) && !empty($this->options['translation']['language'])) {
81  $languageBackup = $translationService->getLanguage();
82  $translationService->setLanguage($this->options['translation']['language']);
83  }
84  $message = $standaloneView->render();
85  if (!empty($languageBackup)) {
86  $translationService->setLanguage($languageBackup);
87  }
88 
89  $subject = $this->‪parseOption('subject');
90  $recipientAddress = $this->‪parseOption('recipientAddress');
91  $recipientName = $this->‪parseOption('recipientName');
92  $senderAddress = $this->‪parseOption('senderAddress');
93  $senderName = $this->‪parseOption('senderName');
94  $replyToAddress = $this->‪parseOption('replyToAddress');
95  $carbonCopyAddress = $this->‪parseOption('carbonCopyAddress');
96  $blindCarbonCopyAddress = $this->‪parseOption('blindCarbonCopyAddress');
97  $format = $this->‪parseOption('format');
98  $attachUploads = $this->‪parseOption('attachUploads');
99 
100  if (empty($subject)) {
101  throw new ‪FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
102  }
103  if (empty($recipientAddress)) {
104  throw new ‪FinisherException('The option "recipientAddress" must be set for the EmailFinisher.', 1327060200);
105  }
106  if (empty($senderAddress)) {
107  throw new ‪FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210);
108  }
109 
110  $mail = $this->objectManager->get(MailMessage::class);
111 
112  $mail->setFrom([$senderAddress => $senderName])
113  ->setTo([$recipientAddress => $recipientName])
114  ->setSubject($subject);
115 
116  if (!empty($replyToAddress)) {
117  $mail->setReplyTo($replyToAddress);
118  }
119 
120  if (!empty($carbonCopyAddress)) {
121  $mail->setCc($carbonCopyAddress);
122  }
123 
124  if (!empty($blindCarbonCopyAddress)) {
125  $mail->setBcc($blindCarbonCopyAddress);
126  }
127 
128  if ($format === self::FORMAT_PLAINTEXT) {
129  $mail->setBody($message, 'text/plain');
130  } else {
131  $mail->setBody($message, 'text/html');
132  }
133 
134  $elements = $formRuntime->getFormDefinition()->getRenderablesRecursively();
135 
136  if ($attachUploads) {
137  foreach ($elements as $element) {
138  if (!$element instanceof ‪FileUpload) {
139  continue;
140  }
141  $file = $formRuntime[$element->getIdentifier()];
142  if ($file) {
143  if ($file instanceof ‪FileReference) {
144  $file = $file->getOriginalResource();
145  }
146 
147  $mail->attach(\Swift_Attachment::newInstance($file->getContents(), $file->getName(), $file->getMimeType()));
148  }
149  }
150  }
151 
152  $mail->send();
153  }
154 
160  protected function ‪initializeStandaloneView(‪FormRuntime $formRuntime): ‪StandaloneView
161  {
162  $format = ucfirst($this->‪parseOption('format'));
163  $standaloneView = $this->objectManager->get(StandaloneView::class);
164 
165  if (isset($this->options['templatePathAndFilename'])) {
166  $this->options['templatePathAndFilename'] = strtr($this->options['templatePathAndFilename'], [
167  '{@format}' => $format
168  ]);
169  $standaloneView->setTemplatePathAndFilename($this->options['templatePathAndFilename']);
170  } else {
171  if (!isset($this->options['templateName'])) {
172  throw new FinisherException('The option "templateName" must be set for the EmailFinisher.', 1327058829);
173  }
174  $this->options['templateName'] = strtr($this->options['templateName'], [
175  '{@format}' => $format
176  ]);
177  $standaloneView->setTemplate($this->options['templateName']);
178  }
179 
180  $standaloneView->assign('finisherVariableProvider', $this->finisherContext->getFinisherVariableProvider());
181 
182  if (isset($this->options['templateRootPaths']) && is_array($this->options['templateRootPaths'])) {
183  $standaloneView->setTemplateRootPaths($this->options['templateRootPaths']);
184  }
185 
186  if (isset($this->options['partialRootPaths']) && is_array($this->options['partialRootPaths'])) {
187  $standaloneView->setPartialRootPaths($this->options['partialRootPaths']);
188  }
189 
190  if (isset($this->options['layoutRootPaths']) && is_array($this->options['layoutRootPaths'])) {
191  $standaloneView->setLayoutRootPaths($this->options['layoutRootPaths']);
192  }
193 
194  if (isset($this->options['variables'])) {
195  $standaloneView->assignMultiple($this->options['variables']);
196  }
197 
198  $standaloneView->assign('form', $formRuntime);
199  $standaloneView->getRenderingContext()
200  ->getViewHelperVariableContainer()
201  ->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime);
202 
203  return $standaloneView;
204  }
205 }
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormRuntime.php:97
‪TYPO3\CMS\Form\Service\TranslationService\getInstance
‪static TranslationService getInstance()
Definition: TranslationService.php:82
‪TYPO3\CMS\Form\Service\TranslationService
Definition: TranslationService.php:43
‪TYPO3\CMS\Form\Domain\Finishers\EmailFinisher\initializeStandaloneView
‪StandaloneView initializeStandaloneView(FormRuntime $formRuntime)
Definition: EmailFinisher.php:159
‪TYPO3\CMS\Core\Mail\MailMessage
Definition: MailMessage.php:23
‪TYPO3\CMS\Form\Domain\Finishers
Definition: AbstractFinisher.php:3
‪TYPO3\CMS\Form\Domain\Finishers\EmailFinisher\FORMAT_HTML
‪const FORMAT_HTML
Definition: EmailFinisher.php:56
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher
Definition: AbstractFinisher.php:35
‪TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException
Definition: FinisherException.php:24
‪TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper
Definition: RenderRenderableViewHelper.php:37
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:25
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:3
‪TYPO3\CMS\Form\Domain\Finishers\EmailFinisher\$defaultOptions
‪array $defaultOptions
Definition: EmailFinisher.php:60
‪TYPO3\CMS\Form\Domain\Finishers\EmailFinisher
Definition: EmailFinisher.php:54
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher\parseOption
‪string array null parseOption(string $optionName)
Definition: AbstractFinisher.php:155
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:24
‪TYPO3\CMS\Form\Domain\Finishers\EmailFinisher\FORMAT_PLAINTEXT
‪const FORMAT_PLAINTEXT
Definition: EmailFinisher.php:55
‪TYPO3\CMS\Form\Domain\Finishers\EmailFinisher\executeInternal
‪executeInternal()
Definition: EmailFinisher.php:73