From badc17891c0c61a5e096f432780f451b52f1e39e Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 7 Mar 2022 16:18:57 +1300 Subject: [PATCH] ENH Create fallback email from address --- src/Control/Email/Email.php | 22 +++++++++++++++++++--- tests/php/Control/Email/EmailTest.php | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Control/Email/Email.php b/src/Control/Email/Email.php index 66ecfe3fd..7396a129c 100644 --- a/src/Control/Email/Email.php +++ b/src/Control/Email/Email.php @@ -3,6 +3,7 @@ namespace SilverStripe\Control\Email; use DateTime; +use RuntimeException; use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\Validation\RFCValidation; use SilverStripe\Control\Director; @@ -25,7 +26,6 @@ use Swift_MimePart; */ class Email extends ViewableData { - /** * @var array * @config @@ -276,14 +276,30 @@ class Email extends ViewableData $dateTime = new DateTime(); $dateTime->setTimestamp(DBDatetime::now()->getTimestamp()); $swiftMessage->setDate($dateTime); - if (!$swiftMessage->getFrom() && ($defaultFrom = $this->config()->get('admin_email'))) { - $swiftMessage->setFrom($defaultFrom); + if (!$swiftMessage->getFrom()) { + $swiftMessage->setFrom($this->getDefaultFrom()); } $this->swiftMessage = $swiftMessage; return $this; } + /** + * @return string + */ + private function getDefaultFrom(): string + { + $defaultFrom = $this->config()->get('admin_email'); + if (!$defaultFrom) { + $host = Director::host(); + if (empty($host)) { + throw new RuntimeException('Host not defined'); + } + $defaultFrom = sprintf('no-reply@%s', $host); + } + return $defaultFrom; + } + /** * @return string[] */ diff --git a/tests/php/Control/Email/EmailTest.php b/tests/php/Control/Email/EmailTest.php index 8857cfed6..6c88490ec 100644 --- a/tests/php/Control/Email/EmailTest.php +++ b/tests/php/Control/Email/EmailTest.php @@ -4,6 +4,7 @@ namespace SilverStripe\Control\Tests\Email; use DateTime; use PHPUnit_Framework_MockObject_MockObject; +use SilverStripe\Control\Director; use SilverStripe\Control\Email\Email; use SilverStripe\Control\Email\Mailer; use SilverStripe\Control\Email\SwiftMailer; @@ -24,6 +25,12 @@ use Swift_RfcComplianceException; class EmailTest extends SapphireTest { + protected function setUp() + { + parent::setUp(); + Director::config()->set('alternate_base_url', 'http://www.mysite.com/'); + } + public function testAddAttachment() { $email = new Email(); @@ -662,6 +669,21 @@ class EmailTest extends SapphireTest $this->assertContains('Test', $plainPart->getBody()); } + public function testGetDefaultFrom() + { + $email = new Email(); + $class = new \ReflectionClass(Email::class); + $method = $class->getMethod('getDefaultFrom'); + $method->setAccessible(true); + + // default to no-reply@mydomain.com if admin_email config not set + $this->assertSame('no-reply@www.mysite.com', $method->invokeArgs($email, [])); + + // use admin_email config + Email::config()->set('admin_email', 'myadmin@somewhere.com'); + $this->assertSame('myadmin@somewhere.com', $method->invokeArgs($email, [])); + } + /** * @return PHPUnit_Framework_MockObject_MockObject|Email */