mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENH Create fallback email from address
This commit is contained in:
parent
2841e1bd3d
commit
badc17891c
@ -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[]
|
||||||
*/
|
*/
|
||||||
|
@ -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->assertContains('Test', $plainPart->getBody());
|
$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
|
* @return PHPUnit_Framework_MockObject_MockObject|Email
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user