Skip to content

Commit

Permalink
API Allow array style email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 26, 2023
1 parent 5e22931 commit ec4a8b8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/Control/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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));
}
Expand Down
46 changes: 45 additions & 1 deletion tests/php/Control/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit ec4a8b8

Please sign in to comment.