[BUGFIX] Sending fails on multiple email-addresses
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / PostProcess / MailPostProcessor.php
index c90c8a0..2b07604 100644 (file)
@@ -145,6 +145,36 @@ class MailPostProcessor implements \TYPO3\CMS\Form\PostProcess\PostProcessorInte
        }
 
        /**
+        * Filter input-string for valid email addresses
+        *
+        * @param string $emails If this is a string, it will be checked for one or more valid email addresses.
+        * @return array List of valid email addresses
+        */
+       protected function filterValidEmails($emails) {
+               if (!is_string($emails)) {
+                       // No valid addresses - empty list
+                       return array();
+               }
+
+               /** @var $addressParser \TYPO3\CMS\Core\Mail\Rfc822AddressesParser */
+               $addressParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\Rfc822AddressesParser', $emails);
+               $addresses = $addressParser->parseAddressList();
+
+               $validEmails = array();
+               foreach ($addresses as $address) {
+                       $fullAddress = $address->mailbox . '@' . $address->host;
+                       if (\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($fullAddress)) {
+                               if ($address->personal) {
+                                       $validEmails[$fullAddress] = $address->personal;
+                               } else {
+                                       $validEmails[] = $fullAddress;
+                               }
+                       }
+               }
+               return $validEmails;
+       }
+
+       /**
         * Adds the receiver of the mail message when configured
         *
         * Checks the address if it is a valid email address
@@ -152,8 +182,9 @@ class MailPostProcessor implements \TYPO3\CMS\Form\PostProcess\PostProcessorInte
         * @return void
         */
        protected function setTo() {
-               if ($this->typoScript['recipientEmail'] && \TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($this->typoScript['recipientEmail'])) {
-                       $this->mailMessage->setTo($this->typoScript['recipientEmail']);
+               $validEmails = $this->filterValidEmails($this->typoScript['recipientEmail']);
+               if (count($validEmails)) {
+                       $this->mailMessage->setTo($validEmails);
                }
        }
 
@@ -165,8 +196,9 @@ class MailPostProcessor implements \TYPO3\CMS\Form\PostProcess\PostProcessorInte
         * @return void
         */
        protected function setCc() {
-               if ($this->typoScript['ccEmail'] && \TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($this->typoScript['ccEmail'])) {
-                       $this->mailMessage->AddCc(trim($this->typoScript['ccEmail']));
+               $validEmails = $this->filterValidEmails($this->typoScript['ccEmail']);
+               if (count($validEmails)) {
+                       $this->mailMessage->setCc($validEmails);
                }
        }