Revert "CRM-21197 Convert from html to plain text AFTER token substitution has happened"
authorTim Otten <totten@civicrm.org>
Thu, 21 Dec 2017 01:55:43 +0000 (17:55 -0800)
committerGitHub <noreply@github.com>
Thu, 21 Dec 2017 01:55:43 +0000 (17:55 -0800)
CRM/Mailing/BAO/Mailing.php
tests/phpunit/CRM/Mailing/BaseMailingSystemTest.php
xml/templates/civicrm_data.tpl

index 373ab95bafff07da3a7e279b11eb71612e9f2c97..fab0bcb82de38ab49141c857898830fdad0236f4 100644 (file)
@@ -769,7 +769,7 @@ ORDER BY   {$orderBy}
     if (!$this->templates) {
       $this->getHeaderFooter();
       $this->templates = array();
-      if ($this->body_text) {
+      if ($this->body_text || !empty($this->header)) {
         $template = array();
         if (!empty($this->header->body_text)) {
           $template[] = $this->header->body_text;
@@ -778,7 +778,12 @@ ORDER BY   {$orderBy}
           $template[] = CRM_Utils_String::htmlToText($this->header->body_html);
         }
 
-        $template[] = $this->body_text;
+        if ($this->body_text) {
+          $template[] = $this->body_text;
+        }
+        else {
+          $template[] = CRM_Utils_String::htmlToText($this->body_html);
+        }
 
         if (!empty($this->footer->body_text)) {
           $template[] = $this->footer->body_text;
@@ -806,6 +811,12 @@ ORDER BY   {$orderBy}
 
         $this->templates['html'] = implode("\n", $template);
 
+        // this is where we create a text template from the html template if the text template did not exist
+        // this way we ensure that every recipient will receive an email even if the pref is set to text and the
+        // user uploads an html email only
+        if (empty($this->templates['text'])) {
+          $this->templates['text'] = CRM_Utils_String::htmlToText($this->templates['html']);
+        }
       }
 
       if ($this->subject) {
@@ -1269,6 +1280,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
         array_push($pEmail, $template[($idx + 1)]);
       }
     }
+
     $html = NULL;
     if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) {
       $html = &$pEmails['html'];
@@ -1296,33 +1308,15 @@ ORDER BY   civicrm_email.is_bulkmail DESC
     }
 
     $mailParams = $headers;
-
-    // If we should be sending a text version of the email
-    if (($test || $contact['preferred_mail_format'] == 'Text' ||
+    if ($text && ($test || $contact['preferred_mail_format'] == 'Text' ||
         $contact['preferred_mail_format'] == 'Both' ||
         ($contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))
       )
     ) {
-
-      // The following if elseif allows us to ensure that people who have a
-      // preference for text emails will get one even when the person composing
-      // the email has not uploaded a text version.
-
-      if ($text) {
-        // If the text version exists, use it
-        $textBody = implode('', $text);
-      }
-      elseif ($html) {
-        // Else if it doesn't exist and the html version exists, use it
-        $textBody = implode('', $html);
-        $textBody = htmlspecialchars_decode(htmlspecialchars_decode($textBody)); // Some &s have become 'really encoded'
-        $textBody = CRM_Utils_String::htmlToText($textBody);
-      }
-
+      $textBody = implode('', $text);
       if ($useSmarty) {
         $textBody = $smarty->fetch("string:$textBody");
       }
-
       $mailParams['text'] = $textBody;
     }
 
@@ -1346,6 +1340,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
       $res = NULL;
       return $res;
     }
+
     $mailParams['attachments'] = $attachments;
 
     $mailParams['Subject'] = CRM_Utils_Array::value('subject', $pEmails);
index 4838fb912a4cad9d694f2fa5f59a59e940b92663..080d3fd4c4aa8a412b5516c3a9de05d5e05a70e2 100644 (file)
@@ -127,7 +127,7 @@ abstract class CRM_Mailing_BaseMailingSystemTest extends CiviUnitTestCase {
         ";" .
         "Sample Header for TEXT formatted content.\n" . // Default header
         "BEWARE children need regular infusions of toys. Santa knows your .*\\. There is no http.*civicrm/mailing/optout.*\\.\n" .
-        "Unsubscribe: http.*civicrm/mailing/optout" . // Default footer
+        "to unsubscribe: http.*civicrm/mailing/optout" . // Default footer
         ";",
         $message->body->text
       );
@@ -156,26 +156,29 @@ abstract class CRM_Mailing_BaseMailingSystemTest extends CiviUnitTestCase {
       $this->assertEquals('html', $htmlPart->subType);
       $this->assertRegExp(
         ";" .
-        "<p>Sample Header for HTML formatted content\.</p>.*" . // Default header
+        "Sample Header for HTML formatted content.\n" . // Default header
         // FIXME: CiviMail puts double " after hyperlink!
-        "<p>You can go to <a href=\"http://example.net/first\\?cs=[0-9a-f_]+\"\"?>Google</a> or <a href=\"http.*civicrm/mailing/optout.*\">opt out</a>.</p>.*" . // body_html
-        "<p>Sample Footer for HTML formatted content\.</p>.*" . // Default footer
+        "<p>You can go to <a href=\"http://example.net/first\\?cs=[0-9a-f_]+\"\"?>Google</a> or <a href=\"http.*civicrm/mailing/optout.*\">opt out</a>.</p>\n" . // body_html
+        "Sample Footer for HTML formatted content" . // Default footer
+        ".*\n" .
         "<img src=\".*extern/open.php.*\"" .
-        ";s",
+        ";",
         $htmlPart->text
       );
+
       $this->assertEquals('plain', $textPart->subType);
       $this->assertRegExp(
         ";" .
-        "Sample Header for HTML formatted content\\..*" . // Default header (converted from HTML as it was not supplied)
-        "You can go to Google \\[1\\] or opt out \\[2\\]\\..*" . //  Text body (converted from HTML as it was not supplied)
-        "Sample Footer for HTML formatted content\..*" . // Footer footer (converted from HTML as it was not supplied)
-        "Unsubscribe \\[2\\]\\..*" . //  Text body (converted from HTML as it was not supplied)
-        "Links:.*" .
-        "------.*" .
-        "\\[1\\] http://example.net/first\\?cs=[0-9a-f_]+.*" .
-        "\\[2\\] http.*civicrm/mailing/optout.*" .
-        ";s",
+        "Sample Header for TEXT formatted content.\n" . // Default header
+        "You can go to Google \\[1\\] or opt out \\[2\\]\\.\n" . //  body_html, filtered
+        "\n" .
+        "Links:\n" .
+        "------\n" .
+        "\\[1\\] http://example.net/first\\?cs=[0-9a-f_]+\n" .
+        "\\[2\\] http.*civicrm/mailing/optout.*\n" .
+        "\n" .
+        "to unsubscribe: http.*civicrm/mailing/optout" . // Default footer
+        ";",
         $textPart->text
       );
     }
@@ -207,23 +210,28 @@ abstract class CRM_Mailing_BaseMailingSystemTest extends CiviUnitTestCase {
         "<p>You can go to <a href=['\"].*extern/url\.php\?u=\d+&amp\\;qid=\d+['\"]>Google</a>" .
         " or <a href=\"http.*civicrm/mailing/optout.*\">opt out</a>.</p>\n" .
         // Default footer
-        "<p>Sample Footer for HTML formatted content\.</p>" .
+        "Sample Footer for HTML formatted content" .
         ".*\n" .
         // Open-tracking code
         "<img src=\".*extern/open.php.*\"" .
         ";",
         $htmlPart->text
       );
+
       $this->assertEquals('plain', $textPart->subType);
       $this->assertRegExp(
         ";" .
-        "You can go to Google \\[1\\] or opt out \\[2\\]\\..*" .
-        "Unsubscribe \\[2\\].*" .
+        //  body_html, filtered
+        "You can go to Google \\[1\\] or opt out \\[2\\]\\.\n" .
+        "\n" .
         "Links:\n" .
         "------\n" .
-        "\\[1\\] .*extern/url\.php\?u=\d+&qid=\d+.*" .
-        "\\[2\\] http.*civicrm/mailing/optout.*" .
-        ";s",
+        "\\[1\\] .*extern/url\.php\?u=\d+&qid=\d+\n" .
+        "\\[2\\] http.*civicrm/mailing/optout.*\n" .
+        "\n" .
+        // Default footer
+        "to unsubscribe: http.*civicrm/mailing/optout" .
+        ";",
         $textPart->text
       );
     }
@@ -306,10 +314,11 @@ abstract class CRM_Mailing_BaseMailingSystemTest extends CiviUnitTestCase {
       array('url_tracking' => 1),
     );
     $cases[] = array(
-      // Plain-text URLs are not tracked.
+      // Plain-text URL's are tracked in plain-text emails...
+      // but not in HTML emails.
       "<p>Please go to: http://example.net/</p>",
       ";<p>Please go to: http://example\.net/</p>;",
-      ';Please go to: http://example\.net/;',
+      ';Please go to: .*extern/url.php\?u=\d+&qid=\d+;',
       array('url_tracking' => 1),
     );
 
index 30e01ec2e07b72dafd23a2cf1a8d80de920a934c..ff474c4d8511869874ef3419c6253a46ecc88ca0 100644 (file)
@@ -107,8 +107,9 @@ INSERT INTO civicrm_tag( name, description, parent_id,used_for )
 INSERT INTO civicrm_mailing_component
     (name,component_type,subject,body_html,body_text,is_default,is_active)
 VALUES
-    ('{ts escape="sql"}Mailing Header{/ts}','Header','{ts escape="sql"}Descriptive Title for this Header{/ts}','{ts escape="sql"}<p>Sample Header for HTML formatted content.</p>{/ts}','{ts escape="sql"}Sample Header for TEXT formatted content.{/ts}',1,1),
-    ('{ts escape="sql"}Mailing Footer{/ts}','Footer','{ts escape="sql"}Descriptive Title for this Footer.{/ts}','{ts escape="sql"}<p>Sample Footer for HTML formatted content.</p><p><a href="{ldelim}action.optOutUrl{rdelim}">Unsubscribe</a>.</p><p>{ldelim}domain.address{rdelim}</p>{/ts}','{ts escape="sql"}Unsubscribe: {ldelim}action.optOutUrl{rdelim}{"\n"}{ldelim}domain.address{rdelim}.{/ts}',1,1),
+    ('{ts escape="sql"}Mailing Header{/ts}','Header','{ts escape="sql"}Descriptive Title for this Header{/ts}','{ts escape="sql"}Sample Header for HTML formatted content.{/ts}','{ts escape="sql"}Sample Header for TEXT formatted content.{/ts}',1,1),
+    ('{ts escape="sql"}Mailing Footer{/ts}','Footer','{ts escape="sql"}Descriptive Title for this Footer.{/ts}','{ts escape="sql"}Sample Footer for HTML formatted content<br/><a href="{ldelim}action.optOutUrl{rdelim}">Unsubscribe</a>  <br/> {ldelim}domain.address{rdelim}{/ts}','{ts escape="sql"}to unsubscribe: {ldelim}action.optOutUrl{rdelim}
+{ldelim}domain.address{rdelim}{/ts}',1,1),
     ('{ts escape="sql"}Subscribe Message{/ts}','Subscribe','{ts escape="sql"}Subscription Confirmation Request{/ts}','{ts escape="sql" 1=$subgroup 2=$suburl}You have a pending subscription to the %1 mailing list. To confirm this subscription, reply to this email or click <a href="%2">here</a>.{/ts}','{ts escape="sql" 1=$subgroup 2=$suburl}You have a pending subscription to the %1 mailing list. To confirm this subscription, reply to this email or click on this link: %2{/ts}',1,1),
     ('{ts escape="sql"}Welcome Message{/ts}','Welcome','{ts escape="sql"}Your Subscription has been Activated{/ts}','{ts escape="sql" 1=$welgroup}Welcome. Your subscription to the %1 mailing list has been activated.{/ts}','{ts escape="sql" 1=$welgroup}Welcome. Your subscription to the %1 mailing list has been activated.{/ts}',1,1),
     ('{ts escape="sql"}Unsubscribe Message{/ts}','Unsubscribe','{ts escape="sql"}Un-subscribe Confirmation{/ts}','{ts escape="sql" 1=$unsubgroup 2=$actresub 3=$actresuburl}You have been un-subscribed from the following groups: %1. You can re-subscribe by mailing %2 or clicking <a href="%3">here</a>.{/ts}','{ts escape="sql" 1=$unsubgroup 2=$actresub}You have been un-subscribed from the following groups: %1. You can re-subscribe by mailing %2 or clicking %3{/ts}',1,1),