Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Handle admin_email array config #10258

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Control/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,19 @@ public function setSwiftMessage($swiftMessage)
*/
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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$defaultFrom = array_keys($adminEmail)[0];
$defaultFrom = array_key_first($adminEmail);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted, PHP 7.1 (which 4.9 supports) doesn't have the array_key_first, it's new with php 7.3.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok, I thought we dropped support below 7.4 now but didn't realise 4.9 still has it. All good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah 4.10 was when older PHP support was dropped.

} else {
if (is_string($adminEmail)) {
$defaultFrom = $adminEmail;
} else {
$defaultFrom = '';
}
}
if (empty($defaultFrom)) {
$host = Director::host();
if (empty($host)) {
throw new RuntimeException('Host not defined');
Expand Down
13 changes: 12 additions & 1 deletion tests/php/Control/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,22 @@ public function testGetDefaultFrom()
$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);
}

/**
Expand Down