[TASK] Deprecate sending mails in ContentObjectRenderer 84/61384/9
authorGeorg Ringer <georg.ringer@gmail.com>
Mon, 29 Jul 2019 07:10:26 +0000 (09:10 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 2 Aug 2019 12:39:02 +0000 (14:39 +0200)
The ContentObjectRenderer should not care about sending mails, therefore it has
been marked as deprecated.

Resolves: #88850
Releases: master
Change-Id: Ibbeefe313dd0b306a69d3ff77703fae49f02ae1d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61384
Tested-by: Josef Glatz <josefglatz@gmail.com>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Josef Glatz <josefglatz@gmail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Documentation/Changelog/master/Deprecation-88850-ContentObjectRendererSendNotifyEmail.rst [new file with mode: 0644]
typo3/sysext/felogin/Classes/Controller/FrontendLoginController.php
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-88850-ContentObjectRendererSendNotifyEmail.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-88850-ContentObjectRendererSendNotifyEmail.rst
new file mode 100644 (file)
index 0000000..4f5c140
--- /dev/null
@@ -0,0 +1,42 @@
+.. include:: ../../Includes.txt
+
+============================================================
+Deprecation: #88850 - ContentObjectRenderer::sendNotifyEmail
+============================================================
+
+See :issue:`88850`
+
+Description
+===========
+
+The method :php:`ContentObjectRenderer::sendNotifyEmail` which has been used to send mails has been marked as deprecated.
+
+
+Impact
+======
+
+Using this method will trigger a PHP :php:`E_USER_DEPRECATED` error.
+
+
+Affected Installations
+======================
+
+Any 3rd party extension calling :php:`ContentObjectRenderer::sendNotifyEmail` is affected.
+
+
+Migration
+=========
+
+To send a mail, use the :php:`MailMessage`-API
+
+.. code-block:: php
+
+    $email = GeneralUtility::makeInstance(MailMessage::class)
+         ->to(new Address('john@domain.tld'), new NamedAddress('john@domain.tld', 'John Doe'))
+         ->subject('This is an example email')
+         ->text('This is the plain-text variant')
+         ->html('<h4>Hello John.</h4><p>Enjoy a HTML-readable email. <marquee>We love TYPO3</marquee>.</p>');
+
+    $email->send();
+
+.. index:: PHP-API, FullyScanned, ext:frontend
index bd3a8f1..3e6f192 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Felogin\Controller;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Symfony\Component\Mime\NamedAddress;
 use TYPO3\CMS\Core\Authentication\LoginType;
 use TYPO3\CMS\Core\Context\Context;
 use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
@@ -21,10 +22,12 @@ use TYPO3\CMS\Core\Crypto\Random;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
+use TYPO3\CMS\Core\Mail\MailMessage;
 use TYPO3\CMS\Core\Session\SessionManager;
 use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\HttpUtility;
+use TYPO3\CMS\Core\Utility\MailUtility;
 use TYPO3\CMS\Felogin\Validation\RedirectUrlValidator;
 use TYPO3\CMS\Frontend\Plugin\AbstractPlugin;
 
@@ -513,7 +516,7 @@ class FrontendLoginController extends AbstractPlugin
             }
         }
         if ($user['email']) {
-            $this->cObj->sendNotifyEmail($msg, $user['email'], '', $this->conf['email_from'], $this->conf['email_fromName'], $this->conf['replyTo']);
+            $this->sendMail($msg, $user['email'], '', $this->conf['email_from'] ?? '', $this->conf['email_fromName'] ?? '', $this->conf['replyTo'] ?? '');
         }
 
         return '';
@@ -1034,4 +1037,61 @@ class FrontendLoginController extends AbstractPlugin
         $sessionBackend = $sessionManager->getSessionBackend('FE');
         $sessionManager->invalidateAllSessionsByUserId($sessionBackend, $userId, $this->frontendController->fe_user);
     }
+
+    /**
+     * Sends "forgot password" mail
+     *
+     * @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 $senderAddress "From" email address
+     * @param string $senderName Optional "From" name
+     * @param string $replyTo Optional "Reply-To" header email address.
+     * @return bool Returns TRUE if sent
+     */
+    protected function sendMail(string $message, string $recipients, string $cc, string $senderAddress, string $senderName = '', string $replyTo = ''): bool
+    {
+        if (trim($message) === '') {
+            return false;
+        }
+
+        $mail = GeneralUtility::makeInstance(MailMessage::class);
+        $senderName = trim($senderName);
+        $senderAddress = trim($senderAddress);
+        if ($senderName !== '' && $senderAddress !== '') {
+            $mail->from(new NamedAddress($senderAddress, $senderName));
+        } elseif ($senderAddress !== '') {
+            $mail->from($senderAddress);
+        }
+        $parsedReplyTo = MailUtility::parseAddresses($replyTo);
+        if (!empty($parsedReplyTo)) {
+            $mail->replyTo($parsedReplyTo);
+        }
+        // First line is subject
+        $messageParts = explode(LF, trim($message), 2);
+        $subject = trim($messageParts[0]);
+        $plainMessage = trim($messageParts[1]);
+        $parsedRecipients = MailUtility::parseAddresses($recipients);
+        if (!empty($parsedRecipients)) {
+            $mail->to(...$parsedRecipients)
+                ->subject($subject)
+                ->text($plainMessage);
+            $mail->send();
+        }
+        $parsedCc = MailUtility::parseAddresses($cc);
+        if (!empty($parsedCc)) {
+            $from = $mail->getFrom();
+            /** @var MailMessage $mail */
+            $mail = GeneralUtility::makeInstance(MailMessage::class);
+            if (!empty($parsedReplyTo)) {
+                $mail->replyTo($parsedReplyTo);
+            }
+            $mail->from($from)
+                ->to(...$parsedCc)
+                ->subject($subject)
+                ->text($plainMessage);
+            $mail->send();
+        }
+        return true;
+    }
 }
index 45b1ab4..d2f0ffb 100644 (file)
@@ -5753,9 +5753,11 @@ class ContentObjectRenderer implements LoggerAwareInterface
      * @param string $senderName Optional "From" name
      * @param string $replyTo Optional "Reply-To" header email address.
      * @return bool Returns TRUE if sent
+     * @deprecated ContentObjectRenderer::sendNotifyEmail is deprecated and will be removed in TYPO3 v11. Consider using the mail API directly
      */
     public function sendNotifyEmail($message, $recipients, $cc, $senderAddress, $senderName = '', $replyTo = '')
     {
+        trigger_error('ContentObjectRenderer::sendNotifyEmail is deprecated and will be removed in TYPO3 v11. Consider using the mail API directly.', E_USER_DEPRECATED);
         /** @var MailMessage $mail */
         $mail = GeneralUtility::makeInstance(MailMessage::class);
         $senderName = trim($senderName);
index ac68997..13da876 100644 (file)
@@ -4224,4 +4224,11 @@ return [
             'Breaking-88779-RecordListRemoveUnusedCode.rst'
         ],
     ],
+    '\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::sendNotifyEmail' => [
+        'numberOfMandatoryArguments' => 4,
+        'maximumNumberOfArguments' => 6,
+        'restFiles' => [
+            'Deprecation-88850-ContentObjectRendererSendNotifyEmail.rst'
+        ],
+    ],
 ];