Browse Source

Merge pull request #15370 from cakephp/backport-15018

Backport: Allow multiple replyTo addresses.
othercorey 5 years ago
parent
commit
00bedb4023

+ 14 - 12
src/Mailer/Email.php

@@ -109,7 +109,7 @@ class Email implements JsonSerializable, Serializable
     protected $_sender = [];
 
     /**
-     * The email the recipient will reply to
+     * List of email(s) that the recipient will reply to.
      *
      * @var array
      */
@@ -478,7 +478,7 @@ class Email implements JsonSerializable, Serializable
      */
     public function setReplyTo($email, $name = null)
     {
-        return $this->_setEmailSingle('_replyTo', $email, $name, 'Reply-To requires only 1 email address.');
+        return $this->_setEmail('_replyTo', $email, $name);
     }
 
     /**
@@ -1168,10 +1168,19 @@ class Email implements JsonSerializable, Serializable
             'replyTo' => 'Reply-To',
             'readReceipt' => 'Disposition-Notification-To',
             'returnPath' => 'Return-Path',
+            'to' => 'To',
+            'cc' => 'Cc',
+            'bcc' => 'Bcc',
         ];
-        foreach ($relation as $var => $header) {
-            if ($include[$var]) {
-                $var = '_' . $var;
+        $headerMultipleEmails = ['to', 'cc', 'bcc', 'replyTo'];
+        foreach ($relation as $key => $header) {
+            $var = '_' . $key;
+            if (!$include[$key]) {
+                continue;
+            }
+            if (in_array($key, $headerMultipleEmails, true)) {
+                $headers[$header] = implode(', ', $this->_formatAddress($this->{$var}));
+            } else {
                 $headers[$header] = current($this->_formatAddress($this->{$var}));
             }
         }
@@ -1183,13 +1192,6 @@ class Email implements JsonSerializable, Serializable
             }
         }
 
-        foreach (['to', 'cc', 'bcc'] as $var) {
-            if ($include[$var]) {
-                $classVar = '_' . $var;
-                $headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar}));
-            }
-        }
-
         $headers += $this->_headers;
         if (!isset($headers['Date'])) {
             $headers['Date'] = date(DATE_RFC2822);

+ 5 - 0
tests/TestCase/Mailer/EmailTest.php

@@ -546,6 +546,11 @@ class EmailTest extends TestCase
         $this->assertSame($headers['To'], '"To, CakePHP" <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');
         $this->assertSame($headers['Cc'], 'Cc CakePHP <cc@cakephp.org>, Cc2 CakePHP <cc2@cakephp.org>');
         $this->assertSame($headers['Bcc'], 'Bcc CakePHP <bcc@cakephp.org>, Bcc2 CakePHP <bcc2@cakephp.org>');
+
+        $this->Email->setReplyTo(['replyto@cakephp.org' => 'Reply to me', 'replyto2@cakephp.org' => 'Reply to me too']);
+        $this->assertCount(2, $this->Email->getReplyTo());
+        $headers = $this->Email->getHeaders(array_fill_keys(['replyTo'], true));
+        $this->assertSame($headers['Reply-To'], 'Reply to me <replyto@cakephp.org>, Reply to me too <replyto2@cakephp.org>');
     }
 
     /**

+ 2 - 0
tests/TestCase/Mailer/Transport/MailTransportTest.php

@@ -52,6 +52,7 @@ class MailTransportTest extends TestCase
         $email->setTo('cake@cakephp.org', 'CakePHP');
         $email->setCc(['mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso']);
         $email->setBcc('phpnut@cakephp.org');
+        $email->setReplyTo(['rep@cakephp.org' => 'reply', 'rep2@cakephp.org' => 'reply 2']);
         $email->setMessageId('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
         $longNonAscii = 'Foø Bår Béz Foø Bår Béz Foø Bår Béz Foø Bår Béz';
         $email->setSubject($longNonAscii);
@@ -68,6 +69,7 @@ class MailTransportTest extends TestCase
         $encoded .= ' =?UTF-8?B?Rm/DuCBCw6VyIELDqXo=?=';
 
         $data = 'From: CakePHP Test <noreply@cakephp.org>' . PHP_EOL;
+        $data .= 'Reply-To: reply <rep@cakephp.org>, reply 2 <rep2@cakephp.org>' . PHP_EOL;
         $data .= 'Return-Path: CakePHP Return <pleasereply@cakephp.org>' . PHP_EOL;
         $data .= 'Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>' . PHP_EOL;
         $data .= 'Bcc: phpnut@cakephp.org' . PHP_EOL;

+ 2 - 0
tests/TestCase/Mailer/Transport/SmtpTransportTest.php

@@ -437,6 +437,7 @@ class SmtpTransportTest extends TestCase
         $email->setCc(['mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso']);
         $email->setBcc('phpnut@cakephp.org');
         $email->setMessageId('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
+        $email->setReplyTo(['replyto@cakephp.org' => 'ReplyTo CakePHP', 'reply2@cakephp.org' => 'Reply 2']);
         $email->setSubject('Testing SMTP');
         $date = date(DATE_RFC2822);
         $email->setHeaders(['Date' => $date]);
@@ -445,6 +446,7 @@ class SmtpTransportTest extends TestCase
             ->will($this->returnValue(['First Line', 'Second Line', '.Third Line', '']));
 
         $data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
+        $data .= "Reply-To: ReplyTo CakePHP <replyto@cakephp.org>, Reply 2 <reply2@cakephp.org>\r\n";
         $data .= "Return-Path: CakePHP Return <pleasereply@cakephp.org>\r\n";
         $data .= "To: CakePHP <cake@cakephp.org>\r\n";
         $data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";