From 76c970f80737d77c00790c955b03295545c740f4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 20 Dec 2017 17:55:43 -0800 Subject: [PATCH] Revert "CRM-21197 Convert from html to plain text AFTER token substitution has happened" --- CRM/Mailing/BAO/Mailing.php | 39 ++++++-------- .../CRM/Mailing/BaseMailingSystemTest.php | 53 +++++++++++-------- xml/templates/civicrm_data.tpl | 5 +- 3 files changed, 51 insertions(+), 46 deletions(-) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 373ab95baf..fab0bcb82d 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -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); diff --git a/tests/phpunit/CRM/Mailing/BaseMailingSystemTest.php b/tests/phpunit/CRM/Mailing/BaseMailingSystemTest.php index 4838fb912a..080d3fd4c4 100644 --- a/tests/phpunit/CRM/Mailing/BaseMailingSystemTest.php +++ b/tests/phpunit/CRM/Mailing/BaseMailingSystemTest.php @@ -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( ";" . - "

Sample Header for HTML formatted content\.

.*" . // Default header + "Sample Header for HTML formatted content.\n" . // Default header // FIXME: CiviMail puts double " after hyperlink! - "

You can go to Google or opt out.

.*" . // body_html - "

Sample Footer for HTML formatted content\.

.*" . // Default footer + "

You can go to Google or opt out.

\n" . // body_html + "Sample Footer for HTML formatted content" . // Default footer + ".*\n" . "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 { "

You can go to Google" . " or opt out.

\n" . // Default footer - "

Sample Footer for HTML formatted content\.

" . + "Sample Footer for HTML formatted content" . ".*\n" . // Open-tracking code "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. "

Please go to: http://example.net/

", ";

Please go to: http://example\.net/

;", - ';Please go to: http://example\.net/;', + ';Please go to: .*extern/url.php\?u=\d+&qid=\d+;', array('url_tracking' => 1), ); diff --git a/xml/templates/civicrm_data.tpl b/xml/templates/civicrm_data.tpl index 30e01ec2e0..ff474c4d85 100644 --- a/xml/templates/civicrm_data.tpl +++ b/xml/templates/civicrm_data.tpl @@ -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"}

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.

Unsubscribe.

{ldelim}domain.address{rdelim}

{/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
Unsubscribe
{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 here.{/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 here.{/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), -- 2.25.1