silverstripe-framework/src/Control/Email/Email.php

888 lines
21 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Control\Email;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTP;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
use SilverStripe\View\SSViewer;
use SilverStripe\View\ThemeResourceLoader;
use SilverStripe\View\ViewableData;
use Swift_Message;
use Swift_MimePart;
/**
* Class to support sending emails.
*/
2016-11-29 00:31:16 +01:00
class Email extends ViewableData
{
/**
* @var array
* @config
2016-11-29 00:31:16 +01:00
*/
private static $send_all_emails_to = [];
2016-11-29 00:31:16 +01:00
/**
* @var array
* @config
2016-11-29 00:31:16 +01:00
*/
private static $cc_all_emails_to = [];
2016-11-29 00:31:16 +01:00
/**
* @var array
* @config
2016-11-29 00:31:16 +01:00
*/
private static $bcc_all_emails_to = [];
2016-11-29 00:31:16 +01:00
/**
* @var array
* @config
2016-11-29 00:31:16 +01:00
*/
private static $send_all_emails_from = [];
2016-11-29 00:31:16 +01:00
/**
* This will be set in the config on a site-by-site basis
* @see https://docs.silverstripe.org/en/4/developer_guides/email/#administrator-emails
2016-11-29 00:31:16 +01:00
*
* @config
* @var string|array The default administrator email address or array of [email => name]
2016-11-29 00:31:16 +01:00
*/
private static $admin_email = null;
2016-11-29 00:31:16 +01:00
/**
* @var Swift_Message
2016-11-29 00:31:16 +01:00
*/
private $swiftMessage;
2016-11-29 00:31:16 +01:00
/**
* @var string The name of the HTML template to render the email with (without *.ss extension)
2016-11-29 00:31:16 +01:00
*/
private $HTMLTemplate = null;
2016-11-29 00:31:16 +01:00
/**
* @var string The name of the plain text template to render the plain part of the email with
2016-11-29 00:31:16 +01:00
*/
private $plainTemplate = null;
2016-11-29 00:31:16 +01:00
/**
* @var Swift_MimePart
2016-11-29 00:31:16 +01:00
*/
private $plainPart;
2016-11-29 00:31:16 +01:00
/**
* @var array|ViewableData Additional data available in a template.
2016-11-29 00:31:16 +01:00
* Used in the same way than {@link ViewableData->customize()}.
*/
private $data = [];
2016-11-29 00:31:16 +01:00
/**
* @var array
2016-11-29 00:31:16 +01:00
*/
private $failedRecipients = [];
2016-11-29 00:31:16 +01:00
/**
* Checks for RFC822-valid email format.
2016-11-29 00:31:16 +01:00
*
* @param string $address
* @return boolean
2016-11-29 00:31:16 +01:00
*
* @copyright Cal Henderson <cal@iamcal.com>
* This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
* http://creativecommons.org/licenses/by-sa/2.5/
2016-11-29 00:31:16 +01:00
*/
public static function is_valid_address($address)
{
return \Swift_Validate::email($address);
}
2016-11-29 00:31:16 +01:00
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
/**
* Get send_all_emails_to
*
* @return array Keys are addresses, values are names
*/
public static function getSendAllEmailsTo()
{
return static::mergeConfiguredEmails('send_all_emails_to', 'SS_SEND_ALL_EMAILS_TO');
}
/**
* Get cc_all_emails_to
*
* @return array
*/
public static function getCCAllEmailsTo()
{
return static::mergeConfiguredEmails('cc_all_emails_to', 'SS_CC_ALL_EMAILS_TO');
}
/**
* Get bcc_all_emails_to
*
* @return array
*/
public static function getBCCAllEmailsTo()
{
return static::mergeConfiguredEmails('bcc_all_emails_to', 'SS_BCC_ALL_EMAILS_TO');
}
/**
* Get send_all_emails_from
*
* @return array
*/
public static function getSendAllEmailsFrom()
{
return static::mergeConfiguredEmails('send_all_emails_from', 'SS_SEND_ALL_EMAILS_FROM');
}
/**
* Normalise email list from config merged with env vars
*
* @param string $config Config key
* @param string $env Env variable key
* @return array Array of email addresses
*/
protected static function mergeConfiguredEmails($config, $env)
{
// Normalise config list
$normalised = [];
$source = (array)static::config()->get($config);
foreach ($source as $address => $name) {
if ($address && !is_numeric($address)) {
$normalised[$address] = $name;
} elseif ($name) {
$normalised[$name] = null;
}
}
$extra = Environment::getEnv($env);
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
if ($extra) {
$normalised[$extra] = null;
}
return $normalised;
}
2016-11-29 00:31:16 +01:00
/**
* Encode an email-address to protect it from spambots.
* At the moment only simple string substitutions,
* which are not 100% safe from email harvesting.
2016-11-29 00:31:16 +01:00
*
* @param string $email Email-address
* @param string $method Method for obfuscating/encoding the address
* - 'direction': Reverse the text and then use CSS to put the text direction back to normal
* - 'visible': Simple string substitution ('@' to '[at]', '.' to '[dot], '-' to [dash])
* - 'hex': Hexadecimal URL-Encoding - useful for mailto: links
* @return string
2016-11-29 00:31:16 +01:00
*/
public static function obfuscate($email, $method = 'visible')
{
switch ($method) {
2017-02-22 04:14:53 +01:00
case 'direction':
Requirements::customCSS('span.codedirection { unicode-bidi: bidi-override; direction: rtl; }', 'codedirectionCSS');
2016-11-29 00:31:16 +01:00
return '<span class="codedirection">' . strrev($email) . '</span>';
2017-02-22 04:14:53 +01:00
case 'visible':
$obfuscated = ['@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '];
2016-11-29 00:31:16 +01:00
return strtr($email, $obfuscated);
2017-02-22 04:14:53 +01:00
case 'hex':
$encoded = '';
$emailLength = strlen($email);
for ($x = 0; $x < $emailLength; $x++) {
$encoded .= '&#x' . bin2hex($email[$x]) . ';';
}
return $encoded;
default:
user_error('Email::obfuscate(): Unknown obfuscation method', E_USER_NOTICE);
return $email;
}
}
2016-11-29 00:31:16 +01:00
/**
* Email constructor.
* @param string|array|null $from
* @param string|array|null $to
2016-11-29 00:31:16 +01:00
* @param string|null $subject
* @param string|null $body
* @param string|array|null $cc
* @param string|array|null $bcc
* @param string|null $returnPath
2016-11-29 00:31:16 +01:00
*/
public function __construct(
$from = null,
$to = null,
$subject = null,
$body = null,
$cc = null,
$bcc = null,
$returnPath = null
2016-11-29 00:31:16 +01:00
) {
if ($from) {
$this->setFrom($from);
2016-11-29 00:31:16 +01:00
}
if ($to) {
$this->setTo($to);
2016-11-29 00:31:16 +01:00
}
if ($subject) {
$this->setSubject($subject);
2016-11-29 00:31:16 +01:00
}
if ($body) {
$this->setBody($body);
2016-11-29 00:31:16 +01:00
}
if ($cc) {
$this->setCC($cc);
2016-11-29 00:31:16 +01:00
}
if ($bcc) {
$this->setBCC($bcc);
2016-11-29 00:31:16 +01:00
}
if ($returnPath) {
$this->setReturnPath($returnPath);
2016-11-29 00:31:16 +01:00
}
parent::__construct();
}
/**
* @return Swift_Message
2016-11-29 00:31:16 +01:00
*/
public function getSwiftMessage()
2016-11-29 00:31:16 +01:00
{
if (!$this->swiftMessage) {
$this->setSwiftMessage(new Swift_Message(null, null, 'text/html', 'utf-8'));
}
return $this->swiftMessage;
2016-11-29 00:31:16 +01:00
}
/**
* @param Swift_Message $swiftMessage
2016-11-29 00:31:16 +01:00
*
* @return $this
*/
public function setSwiftMessage($swiftMessage)
2016-11-29 00:31:16 +01:00
{
$swiftMessage->setDate(DBDatetime::now()->getTimestamp());
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
if (!$swiftMessage->getFrom() && ($defaultFrom = $this->config()->get('admin_email'))) {
$swiftMessage->setFrom($defaultFrom);
}
$this->swiftMessage = $swiftMessage;
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @return string[]
*/
public function getFrom()
{
return $this->getSwiftMessage()->getFrom();
}
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function setFrom($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setFrom($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @param string|array $address
* @param string|null $name
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function addFrom($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->addFrom($address, $name);
return $this;
2016-11-29 00:31:16 +01:00
}
/**
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
* @return string
2016-11-29 00:31:16 +01:00
*/
public function getSender()
2016-11-29 00:31:16 +01:00
{
return $this->getSwiftMessage()->getSender();
2016-11-29 00:31:16 +01:00
}
/**
* @param string $address
* @param string|null $name
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function setSender($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setSender($address, $name);
return $this;
2016-11-29 00:31:16 +01:00
}
/**
* @return string
2016-11-29 00:31:16 +01:00
*/
public function getReturnPath()
2016-11-29 00:31:16 +01:00
{
return $this->getSwiftMessage()->getReturnPath();
2016-11-29 00:31:16 +01:00
}
/**
* The bounce handler address
*
* @param string $address Email address where bounce notifications should be sent
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function setReturnPath($address)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setReturnPath($address);
return $this;
2016-11-29 00:31:16 +01:00
}
/**
* @return array
2016-11-29 00:31:16 +01:00
*/
public function getTo()
2016-11-29 00:31:16 +01:00
{
return $this->getSwiftMessage()->getTo();
2016-11-29 00:31:16 +01:00
}
/**
* Set recipient(s) of the email
*
* To send to many, pass an array:
* ['me@example.com' => 'My Name', 'other@example.com'];
*
* @param string|array $address The message recipient(s) - if sending to multiple, use an array of address => name
* @param string|null $name The name of the recipient (if one)
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function setTo($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setTo($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function addTo($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->addTo($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @return array
*/
public function getCC()
{
return $this->getSwiftMessage()->getCc();
}
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function setCC($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setCc($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function addCC($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->addCc($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @return array
*/
public function getBCC()
{
return $this->getSwiftMessage()->getBcc();
}
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function setBCC($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setBcc($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function addBCC($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->addBcc($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
public function getReplyTo()
{
return $this->getSwiftMessage()->getReplyTo();
}
2016-11-29 00:31:16 +01:00
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function setReplyTo($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setReplyTo($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @param string|array $address
* @param string|null $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function addReplyTo($address, $name = null)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->addReplyTo($address, $name);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @return string
*/
public function getSubject()
2016-11-29 00:31:16 +01:00
{
return $this->getSwiftMessage()->getSubject();
2016-11-29 00:31:16 +01:00
}
/**
* @param string $subject The Subject line for the email
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function setSubject($subject)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setSubject($subject);
2016-11-29 00:31:16 +01:00
return $this;
2016-11-29 00:31:16 +01:00
}
/**
* @return int
*/
public function getPriority()
{
return $this->getSwiftMessage()->getPriority();
}
/**
* @param int $priority
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function setPriority($priority)
2016-11-29 00:31:16 +01:00
{
$this->getSwiftMessage()->setPriority($priority);
2016-11-29 00:31:16 +01:00
return $this;
}
/**
* @param string $path Path to file
* @param string $alias An override for the name of the file
* @param string $mime The mime type for the attachment
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function addAttachment($path, $alias = null, $mime = null)
2016-11-29 00:31:16 +01:00
{
$attachment = \Swift_Attachment::fromPath($path);
if ($alias) {
$attachment->setFilename($alias);
}
if ($mime) {
$attachment->setContentType($mime);
}
$this->getSwiftMessage()->attach($attachment);
return $this;
2016-11-29 00:31:16 +01:00
}
/**
* @param string $data
* @param string $name
* @param string $mime
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function addAttachmentFromData($data, $name, $mime = null)
2016-11-29 00:31:16 +01:00
{
$attachment = new \Swift_Attachment($data, $name);
if ($mime) {
$attachment->setContentType($mime);
2016-11-29 00:31:16 +01:00
}
$this->getSwiftMessage()->attach($attachment);
return $this;
2016-11-29 00:31:16 +01:00
}
/**
* @return array|ViewableData The template data
2016-11-29 00:31:16 +01:00
*/
public function getData()
2016-11-29 00:31:16 +01:00
{
return $this->data;
2016-11-29 00:31:16 +01:00
}
/**
* @param array|ViewableData $data The template data to set
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function setData($data)
2016-11-29 00:31:16 +01:00
{
$this->data = $data;
return $this;
}
/**
* @param string|array $name The data name to add or array to names => value
* @param string|null $value The value of the data to add
* @return $this
*/
public function addData($name, $value = null)
{
if (is_array($name)) {
$this->data = array_merge($this->data, $name);
} elseif (is_array($this->data)) {
$this->data[$name] = $value;
2016-11-29 00:31:16 +01:00
} else {
$this->data->$name = $value;
2016-11-29 00:31:16 +01:00
}
return $this;
}
/**
* Remove a datum from the message
2016-11-29 00:31:16 +01:00
*
* @param string $name
2016-11-29 00:31:16 +01:00
* @return $this
*/
public function removeData($name)
{
if (is_array($this->data)) {
unset($this->data[$name]);
} else {
$this->data->$name = null;
2016-11-29 00:31:16 +01:00
}
return $this;
}
/**
* @return string
2016-11-29 00:31:16 +01:00
*/
public function getBody()
2016-11-29 00:31:16 +01:00
{
return $this->getSwiftMessage()->getBody();
}
2016-11-29 00:31:16 +01:00
/**
* @param string $body The email body
* @return $this
*/
public function setBody($body)
{
$plainPart = $this->findPlainPart();
if ($plainPart) {
$this->getSwiftMessage()->detach($plainPart);
}
unset($plainPart);
$body = HTTP::absoluteURLs($body);
$this->getSwiftMessage()->setBody($body);
2016-11-29 00:31:16 +01:00
return $this;
}
2016-11-29 00:31:16 +01:00
/**
* @return string The base URL for the email
*/
public function BaseURL()
{
return Director::absoluteBaseURL();
}
2016-11-29 00:31:16 +01:00
/**
* Debugging help
*
* @return string Debug info
*/
public function debug()
{
$this->render();
2016-11-29 00:31:16 +01:00
$class = static::class;
return "<h2>Email template {$class}:</h2>\n" . '<pre>' . $this->getSwiftMessage()->toString() . '</pre>';
}
2016-11-29 00:31:16 +01:00
/**
* @return string
*/
public function getHTMLTemplate()
{
if ($this->HTMLTemplate) {
return $this->HTMLTemplate;
}
return ThemeResourceLoader::inst()->findTemplate(
SSViewer::get_templates_by_class(static::class, '', self::class),
SSViewer::get_themes()
);
}
2016-11-29 00:31:16 +01:00
/**
* Set the template to render the email with
*
* @param string $template
* @return $this
*/
public function setHTMLTemplate($template)
{
if (substr($template, -3) == '.ss') {
$template = substr($template, 0, -3);
2016-11-29 00:31:16 +01:00
}
$this->HTMLTemplate = $template;
2016-11-29 00:31:16 +01:00
return $this;
}
2016-11-29 00:31:16 +01:00
/**
* Get the template to render the plain part with
*
* @return string
*/
public function getPlainTemplate()
{
return $this->plainTemplate;
}
2016-11-29 00:31:16 +01:00
/**
* Set the template to render the plain part with
*
* @param string $template
* @return $this
*/
public function setPlainTemplate($template)
{
if (substr($template, -3) == '.ss') {
$template = substr($template, 0, -3);
}
$this->plainTemplate = $template;
2016-11-29 00:31:16 +01:00
return $this;
2016-11-29 00:31:16 +01:00
}
/**
* @param array $recipients
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function setFailedRecipients($recipients)
2016-11-29 00:31:16 +01:00
{
$this->failedRecipients = $recipients;
2016-11-29 00:31:16 +01:00
return $this;
}
2016-11-29 00:31:16 +01:00
/**
* @return array
*/
public function getFailedRecipients()
{
return $this->failedRecipients;
}
2016-11-29 00:31:16 +01:00
/**
* Used by {@link SSViewer} templates to detect if we're rendering an email template rather than a page template
*
* @return bool
*/
public function IsEmail()
{
return true;
}
2016-11-29 00:31:16 +01:00
/**
* Send the message to the recipients
*
* @return bool true if successful or array of failed recipients
*/
public function send()
{
if (!$this->getBody()) {
$this->render();
2016-11-29 00:31:16 +01:00
}
if (!$this->hasPlainPart()) {
$this->generatePlainPartFromBody();
2016-11-29 00:31:16 +01:00
}
return Injector::inst()->get(Mailer::class)->send($this);
}
2016-11-29 00:31:16 +01:00
/**
* @return array|bool
*/
public function sendPlain()
{
if (!$this->hasPlainPart()) {
$this->render(true);
2016-11-29 00:31:16 +01:00
}
return Injector::inst()->get(Mailer::class)->send($this);
}
2016-11-29 00:31:16 +01:00
/**
* Render the email
* @param bool $plainOnly Only render the message as plain text
* @return $this
*/
public function render($plainOnly = false)
{
if ($existingPlainPart = $this->findPlainPart()) {
$this->getSwiftMessage()->detach($existingPlainPart);
}
unset($existingPlainPart);
// Respect explicitly set body
$htmlPart = $plainOnly ? null : $this->getBody();
$plainPart = $plainOnly ? $this->getBody() : null;
// Ensure we can at least render something
$htmlTemplate = $this->getHTMLTemplate();
$plainTemplate = $this->getPlainTemplate();
if (!$htmlTemplate && !$plainTemplate && !$plainPart && !$htmlPart) {
return $this;
}
2016-11-29 00:31:16 +01:00
// Do not interfere with emails styles
Requirements::clear();
// Render plain part
if ($plainTemplate && !$plainPart) {
$plainPart = $this->renderWith($plainTemplate, $this->getData())->Plain();
2016-11-29 00:31:16 +01:00
}
// Render HTML part, either if sending html email, or a plain part is lacking
if (!$htmlPart && $htmlTemplate && (!$plainOnly || empty($plainPart))) {
$htmlPart = $this->renderWith($htmlTemplate, $this->getData());
2016-11-29 00:31:16 +01:00
}
// Plain part fails over to generated from html
if (!$plainPart && $htmlPart) {
/** @var DBHTMLText $htmlPartObject */
$htmlPartObject = DBField::create_field('HTMLFragment', $htmlPart);
$plainPart = $htmlPartObject->Plain();
}
// Rendering is finished
Requirements::restore();
// Fail if no email to send
if (!$plainPart && !$htmlPart) {
return $this;
}
// Build HTML / Plain components
if ($htmlPart && !$plainOnly) {
$this->setBody($htmlPart);
$this->getSwiftMessage()->setContentType('text/html');
$this->getSwiftMessage()->setCharset('utf-8');
if ($plainPart) {
$this->getSwiftMessage()->addPart($plainPart, 'text/plain', 'utf-8');
2016-11-29 00:31:16 +01:00
}
} else {
if ($plainPart) {
$this->setBody($plainPart);
}
$this->getSwiftMessage()->setContentType('text/plain');
$this->getSwiftMessage()->setCharset('utf-8');
2016-11-29 00:31:16 +01:00
}
return $this;
}
2016-11-29 00:31:16 +01:00
/**
* @return Swift_MimePart|false
*/
public function findPlainPart()
{
foreach ($this->getSwiftMessage()->getChildren() as $child) {
if ($child instanceof Swift_MimePart && $child->getContentType() == 'text/plain') {
return $child;
}
}
return false;
2016-11-29 00:31:16 +01:00
}
/**
* @return bool
*/
public function hasPlainPart()
2016-11-29 00:31:16 +01:00
{
if ($this->getSwiftMessage()->getContentType() === 'text/plain') {
return true;
}
return (bool) $this->findPlainPart();
2016-11-29 00:31:16 +01:00
}
/**
* Automatically adds a plain part to the email generated from the current Body
2016-11-29 00:31:16 +01:00
*
* @return $this
2016-11-29 00:31:16 +01:00
*/
public function generatePlainPartFromBody()
2016-11-29 00:31:16 +01:00
{
$plainPart = $this->findPlainPart();
if ($plainPart) {
$this->getSwiftMessage()->detach($plainPart);
}
unset($plainPart);
$this->getSwiftMessage()->addPart(
Convert::xml2raw($this->getBody()),
'text/plain',
'utf-8'
);
return $this;
2016-11-29 00:31:16 +01:00
}
}