diff --git a/lib/mail/Mail.php b/lib/mail/Mail.php index c8511852..e5f493a9 100644 --- a/lib/mail/Mail.php +++ b/lib/mail/Mail.php @@ -78,6 +78,9 @@ class Mail implements \JsonSerializable /** @var $reply_to ReplyTo Email to be use when replied to */ private $reply_to; + /** @var $reply_to_list ReplyToList Email to be use when replied to */ + private $reply_to_list; + /** @var $personalization Personalization[] Messages and their metadata */ private $personalization; @@ -982,6 +985,57 @@ public function getReplyTo() return $this->reply_to; } + /** + * Set a list of ReplyTo email addresses. The following input formats are supported: + * + * 1. only email addresses + * [ + * 'email1@domain.com', + * 'email2@domain.com', + * ] + * 2. items with email address and name + * [ + * 'email' => 'email1@domain.com', + * 'name' => 'John Doe', + * ], [ + * 'email' => 'email2@domain.com', + * 'name' => '', + * ] + * + * @param array $replyToList + * @throws \InvalidArgumentException + */ + public function setReplyToList(array $replyToList) + { + Assert::minItems($replyToList, 'replyToList', 1); + Assert::maxItems($replyToList, 'replyToList', 1000); + + foreach ($replyToList as $replyTo) { + if ($replyTo instanceof ReplyTo ) { + $this->reply_to_list[] = $replyTo; + } elseif (is_array($replyTo)) { + if (! isset($replyTo['email'])) { + throw new \InvalidArgumentException('Email is mandatory on ReplyToList array.'); + } + $replyTo['name'] = isset($replyTo['name']) ? $replyTo['name'] : null; + $this->reply_to_list[] = new ReplyTo($replyTo['email'], $replyTo['name']); + } else { + $this->reply_to_list[] = new ReplyTo($replyTo); + } + } + } + + /** + * Retrieve the reply to list to information attached to a Mail object + * + * @return ReplyToList + */ + + public function getReplyToList() + { + return $this->reply_to_list; + } + /** * Add a subject to a Mail object * @@ -1970,6 +2024,7 @@ static function ($value) { )), 'from' => $this->getFrom(), 'reply_to' => $this->getReplyTo(), + 'reply_to_list' => $this->getReplyToList(), 'subject' => $this->getGlobalSubject(), 'content' => $this->getContents(), 'attachments' => $this->getAttachments(), diff --git a/test/unit/ReplyToListTest.php b/test/unit/ReplyToListTest.php new file mode 100644 index 00000000..3e0b45f1 --- /dev/null +++ b/test/unit/ReplyToListTest.php @@ -0,0 +1,86 @@ +setReplyToList($replyToList); + + $this->assertCount(2, $emailObject->getReplyToList()); + $this->assertEquals('test1@example.com', $emailObject->getReplyToList()[0]->getEmail()); + $this->assertEquals('Test1', $emailObject->getReplyToList()[0]->getName()); + $this->assertEquals('test2@example.com', $emailObject->getReplyToList()[1]->getEmail()); + $this->assertEquals('Test2', $emailObject->getReplyToList()[1]->getName()); + } + + public function testSetReplyToListWithValidReplyToArray() + { + $emailObject = new Mail(); + $replyToList = [ + ['email' => 'test1@example.com', 'name' => 'Test1'], + ['email' => 'test2@example.com'], + ]; + + $emailObject->setReplyToList($replyToList); + + $this->assertCount(2, $emailObject->getReplyToList()); + $this->assertEquals('test1@example.com', $emailObject->getReplyToList()[0]->getEmail()); + $this->assertEquals('Test1', $emailObject->getReplyToList()[0]->getName()); + $this->assertEquals('test2@example.com', $emailObject->getReplyToList()[1]->getEmail()); + $this->assertNull($emailObject->getReplyToList()[1]->getName()); + } + + public function testSetReplyToListWithInvalidReplyToArray() + { + $emailObject = new Mail(); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Email is mandatory on ReplyToList array.'); + + $replyToList = [ + ['name' => 'Test1'], + ]; + + $emailObject->setReplyToList($replyToList); + } + + public function testSetReplyToListWithSingleEmailString() + { + $emailObject = new Mail(); + $replyToList = [ + 'test1@example.com', + ]; + + $emailObject->setReplyToList($replyToList); + + $this->assertCount(1, $emailObject->getReplyToList()); + $this->assertEquals('test1@example.com', $emailObject->getReplyToList()[0]->getEmail()); + $this->assertNull($emailObject->getReplyToList()[0]->getName()); + } + + public function testSetReplyToListWithMoreThanMaxItems() + { + $emailObject = new \SendGrid\Mail\Mail(); + // Create a replyToList with 1001 items + $replyToList = array_fill(0, 1001, 'test@example.com'); + + // Update to expect the correct exception type + $this->expectException(\SendGrid\Mail\TypeException::class); + $this->expectExceptionMessage('Number of elements in "$replyToList" can not be more than 1000.'); + + // Call the method that is expected to throw the exception + $emailObject->setReplyToList($replyToList); + } +}