Merge pull request #10258 from creative-commoners/pulls/4.9/email-array

FIX Handle admin_email array config
This commit is contained in:
Michal Kleiner 2022-03-10 11:04:36 +13:00 committed by GitHub
commit e1dd712645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -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');

View File

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