From ec4a8b88e5df07303dd6707c79f3d1b6a1fad801 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 26 Jan 2023 14:40:13 +1300 Subject: [PATCH] API Allow array style email addresses --- src/Control/Email/Email.php | 20 ++++++------ tests/php/Control/Email/EmailTest.php | 46 ++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/Control/Email/Email.php b/src/Control/Email/Email.php index fa83e8c0056..21420d61f09 100644 --- a/src/Control/Email/Email.php +++ b/src/Control/Email/Email.php @@ -162,12 +162,12 @@ public static function obfuscate(string $email, string $method = 'visible'): str } public function __construct( - string $from = '', - string $to = '', + string|array $from = '', + string|array $to = '', string $subject = '', string $body = '', - string $cc = '', - string $bcc = '', + string|array $cc = '', + string|array $bcc = '', string $returnPath = '' ) { parent::__construct(); @@ -197,13 +197,14 @@ public function __construct( $this->data = ViewableData::create(); } - private function getDefaultFrom(): string + private function getDefaultFrom(): string|array { // admin_email can have a string or an array config // https://docs.silverstripe.org/en/4/developer_guides/email/#administrator-emails $adminEmail = $this->config()->get('admin_email'); if (is_array($adminEmail) && count($adminEmail ?? []) > 0) { - $defaultFrom = array_keys($adminEmail)[0]; + $email = array_keys($adminEmail)[0]; + $defaultFrom = [$email => $adminEmail[$email]]; } else { if (is_string($adminEmail)) { $defaultFrom = $adminEmail; @@ -238,8 +239,9 @@ public function setBody(AbstractPart|string $body = null): static /** * The following arguments combinations are valid * a) $address = 'my@email.com', $name = 'My name' - * b) $address = ['my@email.com' => 'My name', 'other@email.com' => 'My other name'] - * c) $address = ['my@email.com' => 'My name', 'other@email.com'] + * b) $address = ['my@email.com' => 'My name'] + * c) $address = ['my@email.com' => 'My name', 'other@email.com' => 'My other name'] + * d) $address = ['my@email.com' => 'My name', 'other@email.com'] */ private function createAddressArray(string|array $address, $name = ''): array { @@ -266,7 +268,7 @@ public function setFrom(string|array $address, string $name = ''): static /** * @see createAddressArray() */ - public function setTo(string|array $address, $name = ''): static + public function setTo(string|array $address, string $name = ''): static { return $this->to(...$this->createAddressArray($address, $name)); } diff --git a/tests/php/Control/Email/EmailTest.php b/tests/php/Control/Email/EmailTest.php index 369cb543851..699d910b63f 100644 --- a/tests/php/Control/Email/EmailTest.php +++ b/tests/php/Control/Email/EmailTest.php @@ -272,6 +272,47 @@ public function testConstructor(): void $this->assertEquals('bounce@example.com', $email->getReturnPath()->getAddress()); } + public function testConstructorArray(): void + { + $email = new Email( + ['from@example.com' => 'From name'], + ['a@example.com' => "A", 'b@example.com' => "B", 'c@example.com', 'd@example.com'], + 'subject', + '

body

', + ['cca@example.com' => 'CCA', 'ccb@example.com' => "CCB", 'ccc@example.com', 'ccd@example.com'], + ['bcca@example.com' => 'BCCA', 'bccb@example.com' => "BCCB", 'bccc@example.com', 'bccd@example.com'], + 'bounce@example.com' + ); + $this->assertCount(1, $email->getFrom()); + $this->assertSame('from@example.com', $email->getFrom()[0]->getAddress()); + $this->assertSame('From name', $email->getFrom()[0]->getName()); + $this->assertCount(4, $email->getTo()); + $this->assertSame('a@example.com', $email->getTo()[0]->getAddress()); + $this->assertSame('A', $email->getTo()[0]->getName()); + $this->assertSame('b@example.com', $email->getTo()[1]->getAddress()); + $this->assertSame('B', $email->getTo()[1]->getName()); + $this->assertSame('c@example.com', $email->getTo()[2]->getAddress()); + $this->assertSame('', $email->getTo()[2]->getName()); + $this->assertCount(4, $email->getCC()); + $this->assertEquals('cca@example.com', $email->getCC()[0]->getAddress()); + $this->assertEquals('CCA', $email->getCC()[0]->getName()); + $this->assertEquals('ccb@example.com', $email->getCC()[1]->getAddress()); + $this->assertEquals('CCB', $email->getCC()[1]->getName()); + $this->assertEquals('ccc@example.com', $email->getCC()[2]->getAddress()); + $this->assertEquals('', $email->getCC()[2]->getName()); + $this->assertEquals('ccd@example.com', $email->getCC()[3]->getAddress()); + $this->assertEquals('', $email->getCC()[2]->getName()); + $this->assertCount(4, $email->getBCC()); + $this->assertEquals('bcca@example.com', $email->getBCC()[0]->getAddress()); + $this->assertEquals('BCCA', $email->getBCC()[0]->getName()); + $this->assertEquals('bccb@example.com', $email->getBCC()[1]->getAddress()); + $this->assertEquals('BCCB', $email->getBCC()[1]->getName()); + $this->assertEquals('bccc@example.com', $email->getBCC()[2]->getAddress()); + $this->assertEquals('', $email->getBCC()[2]->getName()); + $this->assertEquals('bccd@example.com', $email->getBCC()[3]->getAddress()); + $this->assertEquals('', $email->getBCC()[2]->getName()); + } + public function testSetBody(): void { $email = new Email(); @@ -490,7 +531,10 @@ public function testGetDefaultFrom(): void // use admin_email config array syntax Email::config()->set('admin_email', ['anotheradmin@somewhere.com' => 'Admin-email']); - $this->assertSame('anotheradmin@somewhere.com', $method->invokeArgs($email, [])); + $this->assertSame( + ['anotheradmin@somewhere.com' => 'Admin-email'], + $method->invokeArgs($email, []) + ); $this->assertTrue(true); }