diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index 198d29434..6b5a03caf 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -322,9 +322,9 @@ class ServiceProvider extends ModuleServiceProvider /* * Override standard Mailer content with template */ - Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data) { - MailManager::instance()->addContentToMailer($message, $view, $data); - return false; + Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw) { + $method = $raw === null ? 'addContentToMailer' : 'addRawContentToMailer'; + return !MailManager::instance()->$method($message, $raw ?: $view, $data); }); } diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index e6c4b6b44..3dd1be6c3 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -56,9 +56,25 @@ class MailManager */ protected $isTwigStarted = false; + /** + * Same as `addContentToMailer` except with raw content. + * @return bool + */ + public function addRawContentToMailer($message, $content, $data) + { + $template = new MailTemplate; + + $template->fillFromContent($content); + + $this->addContentToMailerInternal($message, $template, $data); + + return true; + } + /** * This function hijacks the `addContent` method of the `October\Rain\Mail\Mailer` * class, using the `mailer.beforeAddContent` event. + * @return bool */ public function addContentToMailer($message, $code, $data) { @@ -69,6 +85,21 @@ class MailManager $this->templateCache[$code] = $template = MailTemplate::findOrMakeTemplate($code); } + if (!$template) { + return false; + } + + $this->addContentToMailerInternal($message, $template, $data); + + return true; + } + + /** + * Internal method used to share logic between `addRawContentToMailer` and `addContentToMailer` + * @return void + */ + protected function addContentToMailerInternal($message, $template, $data) + { /* * Start twig transaction */ diff --git a/modules/system/models/MailTemplate.php b/modules/system/models/MailTemplate.php index bfb55bd6a..19d34c4ed 100644 --- a/modules/system/models/MailTemplate.php +++ b/modules/system/models/MailTemplate.php @@ -105,14 +105,22 @@ class MailTemplate extends Model public function afterFetch() { if (!$this->is_custom) { - $this->fillFromView(); + $this->fillFromView($this->code); } } - public function fillFromView() + public function fillFromContent($content) { - $sections = self::getTemplateSections($this->code); + $this->fillFromSections(MailParser::parse($content)); + } + public function fillFromView($path) + { + $this->fillFromSections(self::getTemplateSections($path)); + } + + protected function fillFromSections($sections) + { $this->content_html = $sections['html']; $this->content_text = $sections['text']; $this->subject = array_get($sections, 'settings.subject', 'No subject'); @@ -128,10 +136,12 @@ class MailTemplate extends Model public static function findOrMakeTemplate($code) { - if (!$template = self::whereCode($code)->first()) { + $template = self::whereCode($code)->first(); + + if (!$template && View::exists($code)) { $template = new self; $template->code = $code; - $template->fillFromView(); + $template->fillFromView($code); } return $template; diff --git a/modules/system/views/mail/partial-subcopy.htm b/modules/system/views/mail/partial-subcopy.htm index ffebb5ef4..1b3a08f6c 100644 --- a/modules/system/views/mail/partial-subcopy.htm +++ b/modules/system/views/mail/partial-subcopy.htm @@ -1,5 +1,6 @@ name = "Subcopy" == +----- {{ body|trim }} ==