From edcc4af68aa70dc4b84b687e34edb90d14191ca0 Mon Sep 17 00:00:00 2001 From: Levente Huszko Date: Tue, 25 Sep 2018 00:54:16 +0200 Subject: [PATCH] Properly utilize the plainOnly flag in mail.beforeAddContent (#3479) Credit to @hlev --- modules/system/ServiceProvider.php | 5 ++-- modules/system/classes/MailManager.php | 36 +++++++++++++++++--------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index c3a651f1f..fd7ec13c1 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -325,9 +325,10 @@ class ServiceProvider extends ModuleServiceProvider /* * Override standard Mailer content with template */ - Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw) { + Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw, $plain) { $method = $raw === null ? 'addContentToMailer' : 'addRawContentToMailer'; - return !MailManager::instance()->$method($message, $raw ?: $view, $data); + $plainOnly = $view === null; // When "plain-text only" email is sent, $view is null, this sets the flag appropriately + return !MailManager::instance()->$method($message, $raw ?: $view ?: $plain, $data, $plainOnly); }); } diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index 5f370e2de..59d659523 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -56,6 +56,7 @@ class MailManager /** * Same as `addContentToMailer` except with raw content. + * * @return bool */ public function addRawContentToMailer($message, $content, $data) @@ -70,11 +71,16 @@ class MailManager } /** - * This function hijacks the `addContent` method of the `October\Rain\Mail\Mailer` + * This function hijacks the `addContent` method of the `October\Rain\Mail\Mailer` * class, using the `mailer.beforeAddContent` event. + * + * @param \Illuminate\Mail\Message $message + * @param string $code + * @param array $data + * @param bool $plainOnly Add only plain text content to the message * @return bool */ - public function addContentToMailer($message, $code, $data) + public function addContentToMailer($message, $code, $data, $plainOnly = false) { if (isset($this->templateCache[$code])) { $template = $this->templateCache[$code]; @@ -87,16 +93,21 @@ class MailManager return false; } - $this->addContentToMailerInternal($message, $template, $data); + $this->addContentToMailerInternal($message, $template, $data, $plainOnly); return true; } /** * Internal method used to share logic between `addRawContentToMailer` and `addContentToMailer` + * + * @param \Illuminate\Mail\Message $message + * @param string $template + * @param array $data + * @param bool $plainOnly Add only plain text content to the message * @return void */ - protected function addContentToMailerInternal($message, $template, $data) + protected function addContentToMailerInternal($message, $template, $data, $plainOnly = false) { /* * Start twig transaction @@ -124,12 +135,14 @@ class MailManager 'subject' => $swiftMessage->getSubject() ]; - /* - * HTML contents - */ - $html = $this->renderTemplate($template, $data); + if (!$plainOnly) { + /* + * HTML contents + */ + $html = $this->renderTemplate($template, $data); - $message->setBody($html, 'text/html'); + $message->setBody($html, 'text/html'); + } /* * Text contents @@ -200,9 +213,8 @@ class MailManager /** * Render the Markdown template into text. - * - * @param string $view - * @param array $data + * @param $content + * @param array $data * @return string */ public function renderText($content, $data = [])