Merge branch '4.9' into 4.10

This commit is contained in:
Steve Boyd 2022-03-08 12:21:03 +13:00
commit 59800b5879
2 changed files with 41 additions and 3 deletions

View File

@ -3,6 +3,7 @@
namespace SilverStripe\Control\Email; namespace SilverStripe\Control\Email;
use DateTime; use DateTime;
use RuntimeException;
use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation; use Egulias\EmailValidator\Validation\RFCValidation;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
@ -25,7 +26,6 @@ use Swift_MimePart;
*/ */
class Email extends ViewableData class Email extends ViewableData
{ {
/** /**
* @var array * @var array
* @config * @config
@ -276,14 +276,30 @@ class Email extends ViewableData
$dateTime = new DateTime(); $dateTime = new DateTime();
$dateTime->setTimestamp(DBDatetime::now()->getTimestamp()); $dateTime->setTimestamp(DBDatetime::now()->getTimestamp());
$swiftMessage->setDate($dateTime); $swiftMessage->setDate($dateTime);
if (!$swiftMessage->getFrom() && ($defaultFrom = $this->config()->get('admin_email'))) { if (!$swiftMessage->getFrom()) {
$swiftMessage->setFrom($defaultFrom); $swiftMessage->setFrom($this->getDefaultFrom());
} }
$this->swiftMessage = $swiftMessage; $this->swiftMessage = $swiftMessage;
return $this; 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[] * @return string[]
*/ */

View File

@ -4,6 +4,7 @@ namespace SilverStripe\Control\Tests\Email;
use DateTime; use DateTime;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use SilverStripe\Control\Director;
use SilverStripe\Control\Email\Email; use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer; use SilverStripe\Control\Email\Mailer;
use SilverStripe\Control\Email\SwiftMailer; use SilverStripe\Control\Email\SwiftMailer;
@ -24,6 +25,12 @@ use Swift_RfcComplianceException;
class EmailTest extends SapphireTest class EmailTest extends SapphireTest
{ {
protected function setUp()
{
parent::setUp();
Director::config()->set('alternate_base_url', 'http://www.mysite.com/');
}
public function testAddAttachment() public function testAddAttachment()
{ {
$email = new Email(); $email = new Email();
@ -662,6 +669,21 @@ class EmailTest extends SapphireTest
$this->assertStringContainsString('Test', $plainPart->getBody()); $this->assertStringContainsString('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 MockObject|Email * @return MockObject|Email
*/ */