Browse Source

Allow the Reply-to header of e-mail contain more than one e-mail

Ricardo Turella 5 years ago
parent
commit
3d7dbfc703

+ 1 - 0
src/Mailer/Mailer.php

@@ -95,6 +95,7 @@ use InvalidArgumentException;
  * @method array getSender() Gets "sender" address. {@see \Cake\Mailer\Message::getSender()}
  * @method $this setReplyTo($email, $name = null) Sets "Reply-To" address. {@see \Cake\Mailer\Message::setReplyTo()}
  * @method array getReplyTo() Gets "Reply-To" address. {@see \Cake\Mailer\Message::getReplyTo()}
+ * @method $this addReplyTo($email, $name = null) Add "Reply-To" address. {@see \Cake\Mailer\Message::addReplyTo()}
  * @method $this setReadReceipt($email, $name = null) Sets Read Receipt (Disposition-Notification-To header).
  *   {@see \Cake\Mailer\Message::setReadReceipt()}
  * @method array getReadReceipt() Gets Read Receipt (Disposition-Notification-To header).

+ 24 - 9
src/Mailer/Message.php

@@ -101,7 +101,7 @@ class Message implements JsonSerializable, Serializable
     protected $sender = [];
 
     /**
-     * The email the recipient will reply to
+     * List of email's that the recipient will reply to
      *
      * @var array
      */
@@ -381,7 +381,7 @@ class Message implements JsonSerializable, Serializable
      */
     public function setReplyTo($email, ?string $name = null)
     {
-        return $this->setEmailSingle('replyTo', $email, $name, 'Reply-To requires only 1 email address.');
+        return $this->setEmail('replyTo', $email, $name);
     }
 
     /**
@@ -395,6 +395,19 @@ class Message implements JsonSerializable, Serializable
     }
 
     /**
+     * Add "Reply-To" address.
+     *
+     * @param string|array $email Null to get, String with email,
+     *   Array with email as key, name as value or email as value (without name)
+     * @param string|null $name Name
+     * @return $this
+     */
+    public function addReplyTo($email, ?string $name = null)
+    {
+        return $this->addEmail('replyTo', $email, $name);
+    }
+
+    /**
      * Sets Read Receipt (Disposition-Notification-To header).
      *
      * @param string|array $email String with email,
@@ -878,10 +891,18 @@ class Message implements JsonSerializable, Serializable
             'replyTo' => 'Reply-To',
             'readReceipt' => 'Disposition-Notification-To',
             'returnPath' => 'Return-Path',
+            'to' => 'To',
+            'cc' => 'Cc',
+            'bcc' => 'Bcc',
         ];
+        $headersMultipleEmails = ['to', 'cc', 'bcc', 'replyTo'];
         foreach ($relation as $var => $header) {
             if ($include[$var]) {
-                $headers[$header] = (string)current($this->formatAddress($this->{$var}));
+                if (in_array($var, $headersMultipleEmails)) {
+                    $headers[$header] = implode(', ', $this->formatAddress($this->{$var}));
+                } else {
+                    $headers[$header] = (string)current($this->formatAddress($this->{$var}));
+                }
             }
         }
         if ($include['sender']) {
@@ -892,12 +913,6 @@ class Message implements JsonSerializable, Serializable
             }
         }
 
-        foreach (['to', 'cc', 'bcc'] as $var) {
-            if ($include[$var]) {
-                $headers[ucfirst($var)] = implode(', ', $this->formatAddress($this->{$var}));
-            }
-        }
-
         $headers += $this->headers;
         if (!isset($headers['Date'])) {
             $headers['Date'] = date(DATE_RFC2822);

+ 3 - 2
tests/TestCase/Mailer/EmailTest.php

@@ -457,12 +457,13 @@ class EmailTest extends TestCase
         $this->Email->setTo('to@cakephp.org', 'To, CakePHP');
         $this->Email->setCc('cc@cakephp.org', 'Cc CakePHP');
         $this->Email->setBcc('bcc@cakephp.org', 'Bcc CakePHP');
+        $this->Email->addReplyTo('replyto2@cakephp.org', 'ReplyTo2 CakePHP');
         $this->Email->addTo('to2@cakephp.org', 'To2 CakePHP');
         $this->Email->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
         $this->Email->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
 
         $this->assertSame($this->Email->getFrom(), ['cake@cakephp.org' => 'CakePHP']);
-        $this->assertSame($this->Email->getReplyTo(), ['replyto@cakephp.org' => 'ReplyTo CakePHP']);
+        $this->assertSame($this->Email->getReplyTo(), ['replyto@cakephp.org' => 'ReplyTo CakePHP', 'replyto2@cakephp.org' => 'ReplyTo2 CakePHP']);
         $this->assertSame($this->Email->getReadReceipt(), ['readreceipt@cakephp.org' => 'ReadReceipt CakePHP']);
         $this->assertSame($this->Email->getReturnPath(), ['returnpath@cakephp.org' => 'ReturnPath CakePHP']);
         $this->assertSame($this->Email->getTo(), ['to@cakephp.org' => 'To, CakePHP', 'to2@cakephp.org' => 'To2 CakePHP']);
@@ -471,7 +472,7 @@ class EmailTest extends TestCase
 
         $headers = $this->Email->getHeaders(array_fill_keys(['from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'], true));
         $this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
-        $this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
+        $this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>, ReplyTo2 CakePHP <replyto2@cakephp.org>');
         $this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
         $this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
         $this->assertSame($headers['To'], '"To, CakePHP" <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');

+ 3 - 2
tests/TestCase/Mailer/MessageTest.php

@@ -708,12 +708,13 @@ HTML;
         $this->message->setTo('to@cakephp.org', 'To, CakePHP');
         $this->message->setCc('cc@cakephp.org', 'Cc CakePHP');
         $this->message->setBcc('bcc@cakephp.org', 'Bcc CakePHP');
+        $this->message->addReplyTo('replyto2@cakephp.org', 'ReplyTo2 CakePHP');
         $this->message->addTo('to2@cakephp.org', 'To2 CakePHP');
         $this->message->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
         $this->message->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
 
         $this->assertSame($this->message->getFrom(), ['cake@cakephp.org' => 'CakePHP']);
-        $this->assertSame($this->message->getReplyTo(), ['replyto@cakephp.org' => 'ReplyTo CakePHP']);
+        $this->assertSame($this->message->getReplyTo(), ['replyto@cakephp.org' => 'ReplyTo CakePHP', 'replyto2@cakephp.org' => 'ReplyTo2 CakePHP']);
         $this->assertSame($this->message->getReadReceipt(), ['readreceipt@cakephp.org' => 'ReadReceipt CakePHP']);
         $this->assertSame($this->message->getReturnPath(), ['returnpath@cakephp.org' => 'ReturnPath CakePHP']);
         $this->assertSame($this->message->getTo(), ['to@cakephp.org' => 'To, CakePHP', 'to2@cakephp.org' => 'To2 CakePHP']);
@@ -722,7 +723,7 @@ HTML;
 
         $headers = $this->message->getHeaders(array_fill_keys(['from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'], true));
         $this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
-        $this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
+        $this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>, ReplyTo2 CakePHP <replyto2@cakephp.org>');
         $this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
         $this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
         $this->assertSame($headers['To'], '"To, CakePHP" <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');