[TASK] Deprecate \TYPO3\CMS\Core\Utility\MailUtility::mail() 38/16138/8
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Sun, 7 Apr 2013 11:47:10 +0000 (13:47 +0200)
committerBenjamin Mack <benni@typo3.org>
Thu, 25 Apr 2013 07:16:03 +0000 (09:16 +0200)
Change-Id: I8375c8a3880d2de2df06c81aa21f056a0d6289a7
Releases: 6.1
Resolves: #42321
Related: #47037
Reviewed-on: https://review.typo3.org/16138
Reviewed-by: Markus Klein
Reviewed-by: Anja Leichsenring
Reviewed-by: Wouter Wolters
Reviewed-by: Stefan Neufeind
Reviewed-by: Benjamin Mack
Tested-by: Benjamin Mack
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Mail/SwiftMailerAdapter.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Classes/Utility/MailUtility.php
typo3/sysext/core/Tests/Unit/Mail/MailerTest.php
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
typo3/sysext/scheduler/Classes/Example/TestTask.php

index 2c7cae2..7ac3041 100644 (file)
@@ -164,6 +164,8 @@ class Bootstrap {
                        ->configureImageProcessingOptions()
                        ->convertPageNotFoundHandlingToBoolean()
                        ->registerGlobalDebugFunctions()
+                       // SwiftMailerAdapter is
+                       // @deprecated since 6.1, will be removed two versions later - will be removed together with \TYPO3\CMS\Core\Utility\MailUtility::mail()
                        ->registerSwiftMailer()
                        ->configureExceptionHandling()
                        ->setMemoryLimit()
@@ -497,6 +499,7 @@ class Bootstrap {
         * Mail sending via Swift Mailer
         *
         * @return \TYPO3\CMS\Core\Core\Bootstrap
+        * @deprecated since 6.1, will be removed two versions later - will be removed together with \TYPO3\CMS\Core\Utility\MailUtility::mail()
         */
        protected function registerSwiftMailer() {
                $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'][] =
index 90224df..58edc83 100644 (file)
@@ -31,6 +31,7 @@ namespace TYPO3\CMS\Core\Mail;
  * Hook subscriber for using Swift Mailer with \TYPO3\CMS\Core\Utility\MailUtility
  *
  * @author Jigal van Hemert <jigal@xs4all.nl>
+ * @deprecated since 6.1, will be removed two versions later - will be removed together with \TYPO3\CMS\Core\Utility\MailUtility::mail()
  */
 class SwiftMailerAdapter implements \TYPO3\CMS\Core\Mail\MailerAdapterInterface {
 
index 037c3bb..b6334b9 100644 (file)
@@ -4380,10 +4380,12 @@ Connection: close
         * @param string $headers Headers, separated by LF
         * @param string $encoding Encoding type: "base64", "quoted-printable", "8bit". Default value is "quoted-printable".
         * @param string $charset Charset used in encoding-headers (only if $encoding is set to a valid value which produces such a header)
-        * @param boolean $dontEncodeHeader If set, the header content will not be encoded.
+        * @param boolean $dontEncodeHeader If set, the header content will not be encoded
         * @return boolean TRUE if mail was accepted for delivery, FALSE otherwise
+        * @deprecated since 6.1, will be removed two versions later - Use \TYPO3\CMS\Core\Mail\Mailer instead
         */
        static public function plainMailEncoded($email, $subject, $message, $headers = '', $encoding = 'quoted-printable', $charset = '', $dontEncodeHeader = FALSE) {
+               GeneralUtility::logDeprecatedFunction();
                if (!$charset) {
                        $charset = 'utf-8';
                }
index c554038..44008ec 100644 (file)
@@ -26,6 +26,9 @@ namespace TYPO3\CMS\Core\Utility;
  *
  * This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Class to handle mail specific functionality
  *
@@ -46,8 +49,10 @@ class MailUtility {
         * @return boolean Indicates whether the mail has been sent or not
         * @see PHP function mail() []
         * @link http://www.php.net/manual/en/function.mail.php
+        * @deprecated since 6.1, will be removed two versions later - Use \TYPO3\CMS\Core\Mail\Mailer instead
         */
        static public function mail($to, $subject, $messageBody, $additionalHeaders = NULL, $additionalParameters = NULL) {
+               GeneralUtility::logDeprecatedFunction();
                $success = TRUE;
                // If the mail does not have a From: header, fall back to the default in TYPO3_CONF_VARS.
                if (!preg_match('/^From:/im', $additionalHeaders) && $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']) {
@@ -216,7 +221,37 @@ class MailUtility {
                return implode($newlineChar, $lines);
        }
 
+       /**
+        * Parses mailbox headers and turns them into an array.
+        *
+        * Mailbox headers are a comma separated list of 'name <email@example.org>' combinations
+        * or plain email addresses (or a mix of these).
+        * The resulting array has key-value pairs where the key is either a number
+        * (no display name in the mailbox header) and the value is the email address,
+        * or the key is the email address and the value is the display name.
+        *
+        * @param string $rawAddresses Comma separated list of email addresses (optionally with display name)
+        * @return array Parsed list of addresses.
+        */
+       static public function parseAddresses($rawAddresses) {
+               /** @var $addressParser \TYPO3\CMS\Core\Mail\Rfc822AddressesParser */
+               $addressParser = GeneralUtility::makeInstance(
+                               'TYPO3\\CMS\\Core\\Mail\\Rfc822AddressesParser',
+                               $rawAddresses
+               );
+               $addresses = $addressParser->parseAddressList();
+               $addressList = array();
+               foreach ($addresses as $address) {
+                       if ($address->personal) {
+                               // item with name found ( name <email@example.org> )
+                               $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
+                       } else {
+                               // item without name found ( email@example.org )
+                               $addressList[] = $address->mailbox . '@' . $address->host;
+                       }
+               }
+               return $addressList;
+       }
 }
 
-
 ?>
\ No newline at end of file
index 32a1092..7391773 100644 (file)
@@ -51,6 +51,9 @@ class MailerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $this->globalMailSettings;
        }
 
+       //////////////////////////
+       // Tests concerning TYPO3\CMS\Core\Mail\Mailer
+       //////////////////////////
        /**
         * @test
         */
index 13d4a7a..d90987a 100644 (file)
@@ -6585,41 +6585,63 @@ class ContentObjectRenderer {
        }
 
        /**
-        * Sending a notification email using $GLOBALS['TSFE']->plainMailEncoded()
+        * Sends a notification email
         *
-        * @param string $msg The message content. If blank, no email is sent.
+        * @param string $message The message content. If blank, no email is sent.
         * @param string $recipients Comma list of recipient email addresses
         * @param string $cc Email address of recipient of an extra mail. The same mail will be sent ONCE more; not using a CC header but sending twice.
-        * @param string $email_from "From" email address
-        * @param string $email_fromName Optional "From" name
+        * @param string $senderAddress "From" email address
+        * @param string $senderName Optional "From" name
         * @param string $replyTo Optional "Reply-To" header email address.
         * @return boolean Returns TRUE if sent
-        * @todo Define visibility
         */
-       public function sendNotifyEmail($msg, $recipients, $cc, $email_from, $email_fromName = '', $replyTo = '') {
-               // Sends order emails:
-               $headers = array();
-               if ($email_from) {
-                       $headers[] = 'From: ' . $email_fromName . ' <' . $email_from . '>';
+       public function sendNotifyEmail($message, $recipients, $cc, $senderAddress, $senderName = '', $replyTo = '') {
+               $result = FALSE;
+               /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
+               $mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
+               $senderName = trim($senderName);
+               $senderAddress = trim($senderAddress);
+               if ($senderName !== '' && $senderAddress !== '') {
+                       $sender = array($senderAddress => $senderName);
+               } elseif ($senderAddress !== '') {
+                       $sender = array($senderAddress);
+               } else {
+                       $sender = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
                }
-               if ($replyTo) {
-                       $headers[] = 'Reply-To: ' . $replyTo;
+               $mail->setFrom($sender);
+               $parsedReplyTo = \TYPO3\CMS\Core\Utility\MailUtility::parseAddresses($replyTo);
+               if (count($parsedReplyTo) > 0) {
+                       $mail->setReplyTo($parsedReplyTo);
                }
-               $recipients = implode(',', \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $recipients, 1));
-               $emailContent = trim($msg);
-               if ($emailContent) {
+               $message = trim($message);
+               if ($message !== '') {
                        // First line is subject
-                       $parts = explode(LF, $emailContent, 2);
-                       $subject = trim($parts[0]);
-                       $plain_message = trim($parts[1]);
-                       if ($recipients) {
-                               $GLOBALS['TSFE']->plainMailEncoded($recipients, $subject, $plain_message, implode(LF, $headers));
-                       }
-                       if ($cc) {
-                               $GLOBALS['TSFE']->plainMailEncoded($cc, $subject, $plain_message, implode(LF, $headers));
-                       }
-                       return TRUE;
+                       $messageParts = explode(LF, $message, 2);
+                       $subject = trim($messageParts[0]);
+                       $plainMessage = trim($messageParts[1]);
+                       $parsedRecipients = \TYPO3\CMS\Core\Utility\MailUtility::parseAddresses($recipients);
+                       if (count($parsedRecipients) > 0) {
+                               $mail->setTo($parsedRecipients)
+                                       ->setSubject($subject)
+                                       ->setBody($plainMessage);
+                               $mail->send();
+                       }
+                       $parsedCc = \TYPO3\CMS\Core\Utility\MailUtility::parseAddresses($cc);
+                       if (count($parsedCc) > 0) {
+                               /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
+                               $mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
+                               if (count($parsedReplyTo) > 0) {
+                                       $mail->setReplyTo($parsedReplyTo);
+                               }
+                               $mail->setFrom($sender)
+                                       ->setTo($parsedCc)
+                                       ->setSubject($subject)
+                                       ->setBody($plainMessage);
+                               $mail->send();
+                       }
+                       $result = TRUE;
                }
+               return $result;
        }
 
        /**
index 3095798..27211e8 100644 (file)
@@ -4499,8 +4499,10 @@ if (version == "n3") {
         * @return void
         * @see \TYPO3\CMS\Core\Utility\GeneralUtility::plainMailEncoded()
         * @todo Define visibility
+        * @deprecated since 6.1, will be removed two versions later - Use \TYPO3\CMS\Core\Mail\Mailer instead
         */
        public function plainMailEncoded($email, $subject, $message, $headers = '') {
+               \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
                // '76', 'all', ''
                $urlmode = $this->config['config']['notification_email_urlmode'];
                if ($urlmode) {
index 479291b..c4c86b5 100644 (file)
@@ -70,7 +70,7 @@ class TestTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
                        // Prepare mailer and send the mail
                        try {
                                /** @var $mailer \TYPO3\CMS\Core\Mail\MailMessage */
-                               $mailer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Mail\MailMessage');
+                               $mailer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
                                $mailer->setFrom(array($this->email => 'SCHEDULER TEST-TASK'));
                                $mailer->setReplyTo(array($this->email => 'SCHEDULER TEST-TASK'));
                                $mailer->setSubject('SCHEDULER TEST-TASK');