-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Allow array style email addresses
- Loading branch information
1 parent
5e22931
commit ec4a8b8
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = '[email protected]', $name = 'My name' | ||
* b) $address = ['[email protected]' => 'My name', '[email protected]' => 'My other name'] | ||
* c) $address = ['[email protected]' => 'My name', '[email protected]'] | ||
* b) $address = ['[email protected]' => 'My name'] | ||
* c) $address = ['[email protected]' => 'My name', '[email protected]' => 'My other name'] | ||
* d) $address = ['[email protected]' => 'My name', '[email protected]'] | ||
*/ | ||
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)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,6 +272,47 @@ public function testConstructor(): void | |
$this->assertEquals('[email protected]', $email->getReturnPath()->getAddress()); | ||
} | ||
|
||
public function testConstructorArray(): void | ||
{ | ||
$email = new Email( | ||
['[email protected]' => 'From name'], | ||
['[email protected]' => "A", '[email protected]' => "B", '[email protected]', '[email protected]'], | ||
'subject', | ||
'<p>body</p>', | ||
['[email protected]' => 'CCA', '[email protected]' => "CCB", '[email protected]', '[email protected]'], | ||
['[email protected]' => 'BCCA', '[email protected]' => "BCCB", '[email protected]', '[email protected]'], | ||
'[email protected]' | ||
); | ||
$this->assertCount(1, $email->getFrom()); | ||
$this->assertSame('[email protected]', $email->getFrom()[0]->getAddress()); | ||
$this->assertSame('From name', $email->getFrom()[0]->getName()); | ||
$this->assertCount(4, $email->getTo()); | ||
$this->assertSame('[email protected]', $email->getTo()[0]->getAddress()); | ||
$this->assertSame('A', $email->getTo()[0]->getName()); | ||
$this->assertSame('[email protected]', $email->getTo()[1]->getAddress()); | ||
$this->assertSame('B', $email->getTo()[1]->getName()); | ||
$this->assertSame('[email protected]', $email->getTo()[2]->getAddress()); | ||
$this->assertSame('', $email->getTo()[2]->getName()); | ||
$this->assertCount(4, $email->getCC()); | ||
$this->assertEquals('[email protected]', $email->getCC()[0]->getAddress()); | ||
$this->assertEquals('CCA', $email->getCC()[0]->getName()); | ||
$this->assertEquals('[email protected]', $email->getCC()[1]->getAddress()); | ||
$this->assertEquals('CCB', $email->getCC()[1]->getName()); | ||
$this->assertEquals('[email protected]', $email->getCC()[2]->getAddress()); | ||
$this->assertEquals('', $email->getCC()[2]->getName()); | ||
$this->assertEquals('[email protected]', $email->getCC()[3]->getAddress()); | ||
$this->assertEquals('', $email->getCC()[2]->getName()); | ||
$this->assertCount(4, $email->getBCC()); | ||
$this->assertEquals('[email protected]', $email->getBCC()[0]->getAddress()); | ||
$this->assertEquals('BCCA', $email->getBCC()[0]->getName()); | ||
$this->assertEquals('[email protected]', $email->getBCC()[1]->getAddress()); | ||
$this->assertEquals('BCCB', $email->getBCC()[1]->getName()); | ||
$this->assertEquals('[email protected]', $email->getBCC()[2]->getAddress()); | ||
$this->assertEquals('', $email->getBCC()[2]->getName()); | ||
$this->assertEquals('[email protected]', $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', ['[email protected]' => 'Admin-email']); | ||
$this->assertSame('[email protected]', $method->invokeArgs($email, [])); | ||
$this->assertSame( | ||
['[email protected]' => 'Admin-email'], | ||
$method->invokeArgs($email, []) | ||
); | ||
$this->assertTrue(true); | ||
} | ||
|
||
|