diff --git a/src/Control/Email/Email.php b/src/Control/Email/Email.php index 7396a129c..b7184c35e 100644 --- a/src/Control/Email/Email.php +++ b/src/Control/Email/Email.php @@ -289,8 +289,19 @@ class Email extends ViewableData */ private function getDefaultFrom(): string { - $defaultFrom = $this->config()->get('admin_email'); - if (!$defaultFrom) { + // 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]; + } else { + if (is_string($adminEmail)) { + $defaultFrom = $adminEmail; + } else { + $defaultFrom = ''; + } + } + if (empty($defaultFrom)) { $host = Director::host(); if (empty($host)) { throw new RuntimeException('Host not defined'); diff --git a/tests/php/Control/Email/EmailTest.php b/tests/php/Control/Email/EmailTest.php index 6c88490ec..4781f9975 100644 --- a/tests/php/Control/Email/EmailTest.php +++ b/tests/php/Control/Email/EmailTest.php @@ -677,11 +677,22 @@ class EmailTest extends SapphireTest $method->setAccessible(true); // default to no-reply@mydomain.com if admin_email config not set + Email::config()->set('admin_email', null); $this->assertSame('no-reply@www.mysite.com', $method->invokeArgs($email, [])); - // use admin_email config + // default to no-reply@mydomain.com if admin_email config is misconfigured + Email::config()->set('admin_email', 123); + $this->assertSame('no-reply@www.mysite.com', $method->invokeArgs($email, [])); + + // use admin_email config string syntax Email::config()->set('admin_email', 'myadmin@somewhere.com'); $this->assertSame('myadmin@somewhere.com', $method->invokeArgs($email, [])); + $this->assertTrue(true); + + // 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->assertTrue(true); } /**