Merge branch '4.10' into 4

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

View File

@ -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[]
*/

View File

@ -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->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
*/