From 601cc668d51e93111ac99d95696a9e3c6cb4e2c1 Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Mon, 30 Apr 2018 17:04:02 +0100 Subject: [PATCH 1/2] autogenerate the plain text version after token substitution --- src/Listener/DefaultComposer.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Listener/DefaultComposer.php b/src/Listener/DefaultComposer.php index a962fc8..5201577 100644 --- a/src/Listener/DefaultComposer.php +++ b/src/Listener/DefaultComposer.php @@ -41,6 +41,8 @@ */ class DefaultComposer extends BaseListener { + protected $autoGeneratePlainText = false; + public function onRun(RunEvent $e) { // FIXME: This probably doesn't belong here... if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { @@ -74,10 +76,14 @@ public function onCompose(ComposeBatchEvent $e) { $tpls = $this->createMessageTemplates($e); $tp->addMessage('subject', $tpls['subject'], 'text/plain'); - $tp->addMessage('body_text', isset($tpls['text']) ? $tpls['text'] : '', - 'text/plain'); - $tp->addMessage('body_html', isset($tpls['html']) ? $tpls['html'] : '', - 'text/html'); + if(isset($tpls['html'])){ + $tp->addMessage('body_html', isset($tpls['html']) ? $tpls['html'] : '', 'text/html'); + } + if(isset($tpls['text'])){ + $tp->addMessage('body_text', isset($tpls['text']) ? $tpls['text'] : '', 'text/plain'); + }else{ + $this->autoGeneratePlainText = true; + } $hasContent = FALSE; foreach ($e->getTasks() as $key => $task) { @@ -160,11 +166,13 @@ public function createMailParams( FlexMailerTask $task, TokenRow $row ) { - return array( - 'Subject' => $row->render('subject'), - 'text' => $row->render('body_text'), - 'html' => $row->render('body_html'), - ); + $params['Subject'] = $row->render('subject'); + $params['html'] = $row->render('body_html'); + $params['text'] = $row->render('body_text'); + if($this->autoGeneratePlainText){ + $params['text'] = \CRM_Utils_String::htmlToText($params['html']); + }else{} + return $params; } /** @@ -226,5 +234,4 @@ public function isClickTracking(ComposeBatchEvent $e) { // of garbage data. return $e->getMailing()->url_tracking && !$e->isPreview(); } - } From 1f94caa30724fef209ef1f4e65b51c04b95642e5 Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Wed, 8 Aug 2018 13:06:40 +0100 Subject: [PATCH 2/2] WIP improvements for autogenerating plain text --- src/Listener/DefaultComposer.php | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Listener/DefaultComposer.php b/src/Listener/DefaultComposer.php index 5201577..ca2085e 100644 --- a/src/Listener/DefaultComposer.php +++ b/src/Listener/DefaultComposer.php @@ -41,7 +41,7 @@ */ class DefaultComposer extends BaseListener { - protected $autoGeneratePlainText = false; + protected $autoGeneratePlainText = FALSE; public function onRun(RunEvent $e) { // FIXME: This probably doesn't belong here... @@ -76,13 +76,12 @@ public function onCompose(ComposeBatchEvent $e) { $tpls = $this->createMessageTemplates($e); $tp->addMessage('subject', $tpls['subject'], 'text/plain'); - if(isset($tpls['html'])){ - $tp->addMessage('body_html', isset($tpls['html']) ? $tpls['html'] : '', 'text/html'); - } - if(isset($tpls['text'])){ - $tp->addMessage('body_text', isset($tpls['text']) ? $tpls['text'] : '', 'text/plain'); - }else{ - $this->autoGeneratePlainText = true; + $tp->addMessage('body_text', isset($tpls['text']) ? $tpls['text'] : '', + 'text/plain'); + $tp->addMessage('body_html', isset($tpls['html']) ? $tpls['html'] : '', + 'text/html'); + if (empty($tpls['text'])) { + $this->autoGeneratePlainText = TRUE; } $hasContent = FALSE; @@ -166,13 +165,15 @@ public function createMailParams( FlexMailerTask $task, TokenRow $row ) { - $params['Subject'] = $row->render('subject'); - $params['html'] = $row->render('body_html'); - $params['text'] = $row->render('body_text'); - if($this->autoGeneratePlainText){ - $params['text'] = \CRM_Utils_String::htmlToText($params['html']); - }else{} - return $params; + $html = $row->render('body_html'); + $text = $this->autoGeneratePlainText ? + \CRM_Utils_String::htmlToText($html) : + $row->render('body_text'); + return array( + 'Subject' => $row->render('subject'), + 'text' => $text, + 'html' => $html, + ); } /** @@ -234,4 +235,5 @@ public function isClickTracking(ComposeBatchEvent $e) { // of garbage data. return $e->getMailing()->url_tracking && !$e->isPreview(); } + }