TYPO3 CMS  TYPO3_6-2
MailPostProcessor.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $form;
28 
32  protected $typoScript;
33 
37  protected $mailMessage;
38 
42  protected $requestHandler;
43 
47  protected $dirtyHeaders = array();
48 
55  public function __construct(\TYPO3\CMS\Form\Domain\Model\Form $form, array $typoScript) {
56  $this->form = $form;
57  $this->typoScript = $typoScript;
58  $this->mailMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
59  $this->requestHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Request');
60  }
61 
69  public function process() {
70  $this->setSubject();
71  $this->setFrom();
72  $this->setTo();
73  $this->setCc();
74  $this->setReplyTo();
75  $this->setPriority();
76  $this->setOrganization();
77  // @todo The whole content rendering seems to be missing here!
78  $this->setHtmlContent();
79  $this->setPlainContent();
80  $this->addAttachmentsFromForm();
81  $this->send();
82  return $this->render();
83  }
84 
92  protected function setSubject() {
93  if (isset($this->typoScript['subject'])) {
94  $subject = $this->typoScript['subject'];
95  } elseif ($this->requestHandler->has($this->typoScript['subjectField'])) {
96  $subject = $this->requestHandler->get($this->typoScript['subjectField']);
97  } else {
98  $subject = 'Formmail on ' . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST');
99  }
100  $subject = $this->sanitizeHeaderString($subject);
101  $this->mailMessage->setSubject($subject);
102  }
103 
111  protected function setFrom() {
112  $fromEmail = '';
113  if ($this->typoScript['senderEmail']) {
114  $fromEmail = $this->typoScript['senderEmail'];
115  } elseif ($this->requestHandler->has($this->typoScript['senderEmailField'])) {
116  $fromEmail = $this->requestHandler->get($this->typoScript['senderEmailField']);
117  } else {
118  $fromEmail = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
119  }
120  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($fromEmail)) {
122  }
123  $fromName = '';
124  if ($this->typoScript['senderName']) {
125  $fromName = $this->typoScript['senderName'];
126  } elseif ($this->requestHandler->has($this->typoScript['senderNameField'])) {
127  $fromName = $this->requestHandler->get($this->typoScript['senderNameField']);
128  } else {
129  $fromName = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
130  }
131  $fromName = $this->sanitizeHeaderString($fromName);
132  if (!empty($fromName)) {
133  $from = array($fromEmail => $fromName);
134  } else {
135  $from = $fromEmail;
136  }
137  $this->mailMessage->setFrom($from);
138  }
139 
146  protected function filterValidEmails($emails) {
147  if (!is_string($emails)) {
148  // No valid addresses - empty list
149  return array();
150  }
151 
153  $addressParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\Rfc822AddressesParser', $emails);
154  $addresses = $addressParser->parseAddressList();
155 
156  $validEmails = array();
157  foreach ($addresses as $address) {
158  $fullAddress = $address->mailbox . '@' . $address->host;
159  if (\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($fullAddress)) {
160  if ($address->personal) {
161  $validEmails[$fullAddress] = $address->personal;
162  } else {
163  $validEmails[] = $fullAddress;
164  }
165  }
166  }
167  return $validEmails;
168  }
169 
177  protected function setTo() {
178  $validEmails = $this->filterValidEmails($this->typoScript['recipientEmail']);
179  if (count($validEmails)) {
180  $this->mailMessage->setTo($validEmails);
181  }
182  }
183 
191  protected function setCc() {
192  $validEmails = $this->filterValidEmails($this->typoScript['ccEmail']);
193  if (count($validEmails)) {
194  $this->mailMessage->setCc($validEmails);
195  }
196  }
197 
205  protected function setReplyTo() {
206  if (isset($this->typoScript['replyToEmail'])) {
207  $emails = $this->typoScript['replyToEmail'];
208  } elseif ($this->requestHandler->has($this->typoScript['replyToEmailField'])) {
209  $emails = $this->requestHandler->get($this->typoScript['replyToEmailField']);
210  }
211  $validEmails = $this->filterValidEmails($emails);
212  if (!empty($validEmails)) {
213  $this->mailMessage->setReplyTo($validEmails);
214  }
215  }
216 
225  protected function setPriority() {
226  $priority = 3;
227  if ($this->typoScript['priority']) {
228  $priority = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->typoScript['priority'], 1, 5);
229  }
230  $this->mailMessage->setPriority($priority);
231  }
232 
240  protected function setOrganization() {
241  if ($this->typoScript['organization']) {
242  $organization = $this->typoScript['organization'];
243  $organization = $this->sanitizeHeaderString($organization);
244  $this->mailMessage->getHeaders()->addTextHeader('Organization', $organization);
245  }
246  }
247 
256  protected function setCharacterSet() {
257  $characterSet = NULL;
258  if ($GLOBALS['TSFE']->config['config']['formMailCharset']) {
259  $characterSet = $GLOBALS['TSFE']->csConvObj->parse_charset($GLOBALS['TSFE']->config['config']['formMailCharset']);
260  } elseif ($GLOBALS['TSFE']->metaCharset != $GLOBALS['TSFE']->renderCharset) {
261  $characterSet = $GLOBALS['TSFE']->metaCharset;
262  }
263  if ($characterSet) {
264  $this->mailMessage->setCharset($characterSet);
265  }
266  }
267 
275  protected function setHtmlContent() {
277  $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Mail\\Html\\HtmlView', $this->form, $this->typoScript);
278  $htmlContent = $view->get();
279  $this->mailMessage->setBody($htmlContent, 'text/html');
280  }
281 
289  protected function setPlainContent() {
291  $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Mail\\Plain\\PlainView', $this->form);
292  $plainContent = $view->render();
293  $this->mailMessage->addPart($plainContent, 'text/plain');
294  }
295 
302  protected function send() {
303  if ($this->mailMessage->getTo() && $this->mailMessage->getBody()) {
304  $this->mailMessage->send();
305  }
306  }
307 
313  protected function render() {
315  $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\View\\Mail\\MailView', $this->mailMessage, $this->typoScript);
316  return $view->render();
317  }
318 
325  protected function sanitizeHeaderString($string) {
326  $pattern = '/[\\r\\n\\f\\e]/';
327  if (preg_match($pattern, $string) > 0) {
328  $this->dirtyHeaders[] = $string;
329  $string = '';
330  }
331  return $string;
332  }
333 
339  protected function addAttachmentsFromForm() {
340  $formElements = $this->form->getElements();
341  $values = $this->requestHandler->getByMethod();
342  $this->addAttachmentsFromElements($formElements, $values);
343  }
344 
353  protected function addAttachmentsFromElements($elements, $submittedValues) {
355  foreach ($elements as $element) {
356  if (is_a($element, 'TYPO3\\CMS\\Form\\Domain\\Model\\Element\\ContainerElement')) {
357  $this->addAttachmentsFromElements($element->getElements(), $submittedValues);
358  continue;
359  }
360  if (is_a($element, 'TYPO3\\CMS\\Form\\Domain\\Model\\Element\\FileuploadElement')) {
361  $elementName = $element->getName();
362  if (is_array($submittedValues[$elementName]) && isset($submittedValues[$elementName]['tempFilename'])) {
363  $filename = $submittedValues[$elementName]['tempFilename'];
364  if (is_file($filename) && \TYPO3\CMS\Core\Utility\GeneralUtility::isAllowedAbsPath($filename)) {
365  $this->mailMessage->attach(\Swift_Attachment::fromPath($filename)->setFilename($submittedValues[$elementName]['originalFilename']));
366  }
367  }
368  }
369  }
370  }
371 
372 }
$requestHandler
setReplyTo()
setCc()
setPriority()
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
setSubject()
setOrganization()
$mailMessage
setCharacterSet()
setFrom()
addAttachmentsFromForm()
$dirtyHeaders
setTo()
__construct(\TYPO3\CMS\Form\Domain\Model\Form $form, array $typoScript)
process()
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
$form
$typoScript
send()
sanitizeHeaderString($string)