[FEATURE] Allow setting a default replyTo-email-address 63/50263/16
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Tue, 18 Oct 2016 13:25:03 +0000 (15:25 +0200)
committerBenni Mack <benni@typo3.org>
Sat, 10 Feb 2018 23:04:54 +0000 (00:04 +0100)
If no other email-address is set as reply-to use this address
as default. This is similar to the existing defaultMailFromAddress.

Change-Id: Iabdf282203c1989036867a6c97b9cbe329e61f60
Resolves: #78332
Releases: master
Reviewed-on: https://review.typo3.org/50263
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Classes/Mail/MailMessage.php
typo3/sysext/core/Classes/Utility/MailUtility.php
typo3/sysext/core/Configuration/DefaultConfiguration.php
typo3/sysext/core/Configuration/DefaultConfigurationDescription.yaml
typo3/sysext/core/Documentation/Changelog/master/Feature-78332-AllowSettingADefaultReplyTo-email-addressForNotification-mails.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/MailUtilityTest.php

index f4e85d8..83a42f5 100644 (file)
@@ -63,6 +63,12 @@ class MailMessage extends \Swift_Message
         if (empty($this->getFrom())) {
             $this->setFrom(MailUtility::getSystemFrom());
         }
+        if (empty($this->getReplyTo())) {
+            $replyTo = MailUtility::getSystemReplyTo();
+            if (!empty($replyTo)) {
+                $this->setReplyTo($replyTo);
+            }
+        }
         $this->initializeMailer();
         $this->sent = true;
         $this->getHeaders()->addTextHeader('X-Mailer', $this->mailerHeader);
index 5d47524..2a0ab2e 100644 (file)
@@ -115,6 +115,30 @@ class MailUtility
     }
 
     /**
+     * Gets a default "reply-to" for mail messages (email and name).
+     *
+     * Ready to be passed to $mail->setReplyTo()
+     *
+     * @return array List of email-addresses. Specifying a realname can be done in the form of "replyToName <replyTo@example.com>".
+     */
+    public static function getSystemReplyTo(): array
+    {
+        $mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
+        $replyToAddress = $mailConfiguration['defaultMailReplyToAddress'];
+        if (empty($replyToAddress) || !GeneralUtility::validEmail($replyToAddress)) {
+            return [];
+        }
+
+        if (!empty($mailConfiguration['defaultMailReplyToName'])) {
+            $replyTo = [$replyToAddress => $mailConfiguration['defaultMailReplyToName']];
+        } else {
+            $replyTo = [$replyToAddress];
+        }
+
+        return $replyTo;
+    }
+
+    /**
      * Breaks up a single line of text for emails
      * Words - longer than $lineWidth - will not be split into parts
      *
index 415224b..2b876f3 100644 (file)
@@ -1055,7 +1055,9 @@ return [
         'transport_sendmail_command' => '',
         'transport_mbox_file' => '',
         'defaultMailFromAddress' => '',
-        'defaultMailFromName' => ''
+        'defaultMailFromName' => '',
+        'defaultMailReplyToAddress' => '',
+        'defaultMailReplyToName' => '',
     ],
     'HTTP' => [ // HTTP configuration to tune how TYPO3 behaves on HTTP requests made by TYPO3. Have a look at http://docs.guzzlephp.org/en/latest/request-options.html for some background information on those settings.
         'allow_redirects' => [ // Mixed, set to false if you want to allow redirects, or use it as an array to add more values,
index bfb9e0c..51dfd07 100644 (file)
@@ -484,10 +484,16 @@ MAIL:
             description: '<em>only with transport=mbox</em>: The file where to write the mails into. This file will be conforming the mbox format described in RFC 4155. It is a simple text file with a concatenation of all mails. Path must be absolute.'
         defaultMailFromAddress:
             type: text
-            description: 'This default email address is used when no other "from" address is set for a TYPO3-generated email. You can specify an email address only (ex. info@example.org).'
+            description: 'This default email address is used when no other "from" address is set for a TYPO3-generated email. You can specify an email address only (eg. info@example.org).'
         defaultMailFromName:
             type: text
             description: 'This default name is used when no other "from" name is set for a TYPO3-generated email.'
+        defaultMailReplyToAddress:
+            type: text
+            description: 'This default email address is used when no other "reply-to" address is set for a TYPO3-generated email. You can specify an email address only (eg. info@example.org).'
+        defaultMailReplyToName:
+            type: text
+            description: 'This default name is used when no other "reply-to" name is set for a TYPO3-generated email.'
 HTTP:
     type: container
     items:
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-78332-AllowSettingADefaultReplyTo-email-addressForNotification-mails.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-78332-AllowSettingADefaultReplyTo-email-addressForNotification-mails.rst
new file mode 100644 (file)
index 0000000..62e88e1
--- /dev/null
@@ -0,0 +1,36 @@
+.. include:: ../../Includes.txt
+
+======================================================================================
+Feature: #78332 - Allow setting a default replyTo-email-address for notification-mails
+======================================================================================
+
+See :issue:`78332`
+
+Description
+===========
+
+Two new LocalConfiguration settings are introduced:
+
+.. code-block:: php
+
+       $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToAddress']
+       $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToName']
+
+Also a new function to build a mail address for SwiftMailer from these settings is introduced:
+
+.. code-block:: php
+
+       MailUtility::getSystemReplyTo()
+
+If no default reply-to address is set this function will return an empty array.
+
+This function is used in :php:`ContentObjectRenderer::sendNotifyEmail()` to set a ReplyTo address in case no address was supplied in the function parameters.
+In other places where notifications are sent for e.g. (failed) login attempts, reports and where the notification uses the system from address this function is also used.
+
+
+Impact
+======
+
+It's now possible to set a reply-to address for notification mails from TYPO3. Extensions can also use this system reply-to address by calling :php:`MailUtility::getSystemReplyTo()`.
+
+.. index:: LocalConfiguration, NotScanned
\ No newline at end of file
index 36fbb47..c273e74 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Core\Tests\Unit\Utility;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\MailUtility;
+
 /**
  * Testcase for the \TYPO3\CMS\Core\Utility\MailUtility class.
  */
@@ -40,7 +42,7 @@ class MailUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
      */
     public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven()
     {
-        $this->assertEmpty(\TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail(''));
+        $this->assertEmpty(MailUtility::breakLinesForEmail(''));
     }
 
     /**
@@ -51,7 +53,7 @@ class MailUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $newlineChar = LF;
         $lineWidth = 76;
         $str = 'This text is not longer than 76 chars and therefore will not be broken.';
-        $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
+        $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
         $this->assertEquals(1, count(explode($newlineChar, $returnString)));
     }
 
@@ -63,7 +65,7 @@ class MailUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $newlineChar = LF;
         $lineWidth = 50;
         $str = 'This text is longer than 50 chars and therefore will be broken.';
-        $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
+        $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
         $this->assertEquals(2, count(explode($newlineChar, $returnString)));
     }
 
@@ -76,7 +78,7 @@ class MailUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $lineWidth = 10;
         // first space after 20 chars (more than $lineWidth)
         $str = 'abcdefghijklmnopqrst uvwxyz 123456';
-        $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
+        $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
         $this->assertEquals($returnString, 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456');
     }
 
@@ -86,7 +88,7 @@ class MailUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
     public function breakLinesForEmailBreaksTextIfLineIsLongerThanTheLineWidth()
     {
         $str = 'Mein Link auf eine News (Link: http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)';
-        $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str);
+        $returnString = MailUtility::breakLinesForEmail($str);
         $this->assertEquals($returnString, 'Mein Link auf eine News (Link:' . LF . 'http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)');
     }
 
@@ -123,7 +125,45 @@ class MailUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
      */
     public function parseAddressesTest($source, $addressList)
     {
-        $returnArray = \TYPO3\CMS\Core\Utility\MailUtility::parseAddresses($source);
+        $returnArray = MailUtility::parseAddresses($source);
         $this->assertEquals($addressList, $returnArray);
     }
+
+    /**
+     * @return array
+     */
+    public function replyToProvider(): array
+    {
+        return [
+            'only address' => [
+                ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => ''],
+                ['noreply@example.org'],
+            ],
+            'name and address' => [
+                ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => 'John Doe'],
+                ['noreply@example.org' => 'John Doe'],
+            ],
+            'no address' => [
+                ['defaultMailReplyToAddress' => '', 'defaultMailReplyToName' => ''],
+                [],
+            ],
+            'invalid address' => [
+                ['defaultMailReplyToAddress' => 'foo', 'defaultMailReplyToName' => ''],
+                [],
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider replyToProvider
+     * @param array $configuration
+     * @param array $expectedReplyTo
+     */
+    public function getSystemReplyToTest(array $configuration, array $expectedReplyTo)
+    {
+        $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $configuration;
+        $returnArray = MailUtility::getSystemReplyTo();
+        $this->assertSame($expectedReplyTo, $returnArray);
+    }
 }