Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reply to list support #1109

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
* [
* '[email protected]',
* '[email protected]',
* ]
* 2. items with email address and name
* [
* 'email' => '[email protected]',
* 'name' => 'John Doe',
* ], [
* 'email' => '[email protected]',
* '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
*
Expand Down Expand Up @@ -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(),
Expand Down
86 changes: 86 additions & 0 deletions test/unit/ReplyToListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace SendGrid\Tests\Unit;

use PHPUnit\Framework\TestCase;
use SendGrid\Mail\Mail;
use SendGrid\Mail\ReplyTo;

class ReplyToListTest extends TestCase
{
public function testSetReplyToListWithValidReplyToObjects()
{
$emailObject = new Mail();
$replyToList = [
new ReplyTo('[email protected]', 'Test1'),
new ReplyTo('[email protected]', 'Test2'),
];

$emailObject->setReplyToList($replyToList);

$this->assertCount(2, $emailObject->getReplyToList());
$this->assertEquals('[email protected]', $emailObject->getReplyToList()[0]->getEmail());
$this->assertEquals('Test1', $emailObject->getReplyToList()[0]->getName());
$this->assertEquals('[email protected]', $emailObject->getReplyToList()[1]->getEmail());
$this->assertEquals('Test2', $emailObject->getReplyToList()[1]->getName());
}

public function testSetReplyToListWithValidReplyToArray()
{
$emailObject = new Mail();
$replyToList = [
['email' => '[email protected]', 'name' => 'Test1'],
['email' => '[email protected]'],
];

$emailObject->setReplyToList($replyToList);

$this->assertCount(2, $emailObject->getReplyToList());
$this->assertEquals('[email protected]', $emailObject->getReplyToList()[0]->getEmail());
$this->assertEquals('Test1', $emailObject->getReplyToList()[0]->getName());
$this->assertEquals('[email protected]', $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 = [
'[email protected]',
];

$emailObject->setReplyToList($replyToList);

$this->assertCount(1, $emailObject->getReplyToList());
$this->assertEquals('[email protected]', $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, '[email protected]');

// 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);
}
}
Loading