CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 4.4 Release Testing »
  • html2text fails to convert Mailing token links
Pages: [1]

Author Topic: html2text fails to convert Mailing token links  (Read 2101 times)

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
html2text fails to convert Mailing token links
October 14, 2013, 05:29:52 pm
When sending a CiviMail with no text part, the text part is generated from the HTML part using the html2text package.

This conversion occurs before token replacement, so html2text has to process links of the form "{action.forward}". html2text thinks these are relative links so it prefixes them giving "http://example.org/{action.forward}". The token processing then converts this to "http://example.org/http://example.org/civicrm/mailing/forward?..." which is invalid.

One option is to do the token processing BEFORE generating the text part.

Another option is to modify html2text slightly ...

Code: [Select]
--- /data/Download/CiviCRM/civicrm-4.4.beta4-drupal/./packages/html2text/rcube_html2text.php 2013-10-02 23:25:39.000000000 +1000
+++ /data/Work/IT/CiviCRM/Local/4.4.beta4/civicrm/./packages/html2text/rcube_html2text.php 2013-10-15 10:45:15.469465000 +1100
@@ -524,7 +524,8 @@
             return $display;
         }
 
-        if (preg_match('!^([a-z][a-z0-9.+-]+:)!i', $link)) {
+        if (preg_match('!^([a-z][a-z0-9.+-]+:)!i', $link)
+            || substr($link, 0, 1) == '{') {
             $url = $link;
         }
         else {

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: html2text fails to convert Mailing token links
October 20, 2013, 06:28:37 am
"One option is to do the token processing BEFORE generating the text part"

Looking at the code, that might be be tricky as there appears to be a lot of processing between generating the text part and doing the token processing, with lots of potential dependencies.

On the other hand, changing a package is not a good idea either.

How about this idea.

Wrap html2text in a wrapper that does the following ...
  • Convert "{...}" tokens to "token://{...}"
  • (Note that this string looks to html2text like an absolute URL reference)
  • Call html2text
  • Convert "token://{...}" tokens to "{...}"

This avoids changing a package and having to refactor the mail processing code.

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: html2text fails to convert Mailing token links
October 27, 2013, 04:56:48 am
Here's a better patch that doesn't require the html2text package to be patched

Code: [Select]
--- /data/Download/CiviCRM/civicrm-4.4.beta4-drupal/CRM/Utils/String.php 2013-10-02 23:25:16.000000000 +1000
+++ /var/www/citybibleforum/sites/all/modules/civicrm/CRM/Utils/String.php 2013-10-27 22:44:28.214427508 +1100
@@ -410,8 +410,11 @@
    */
   static function htmlToText($html) {
     require_once 'packages/html2text/rcube_html2text.php';
-    $converter = new rcube_html2text($html);
-    return $converter->get_text();
+    $token_html = preg_replace('!\{([a-z.]+)\}!i', 'token:{$1}', $html);
+    $converter = new rcube_html2text($token_html);
+    $token_text = $converter->get_text();
+    $text = preg_replace('!token\:\{([a-z.]+)\}!i', '{$1}', $token_text);
+    return $text;
   }
 
   static function extractName($string, &$params) {

What this does is ...
  • Convert tokens in the HTML to a format html2text thinks is an absolute URL ("{action.unsubscribeUrl}" -> "token:{action.unsubscribeUrl}")
  • Run html2text over that
  • Convert the Text to undo the first step ("token:{action.unsubscribeUrl}" -> "{action.unsubscribeUrl}")
  • Return the Text

Can someone who knows the token code better than I check ...
  • The regular expressions for recognising tokens are OK
  • The change doesn't interfere with other parts of the HTML (eg, "{" in CSS)

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: html2text fails to convert Mailing token links
October 28, 2013, 01:59:21 pm
Some token names include an underscore character. So the patch should be ...

Code: [Select]
--- /data/Download/CiviCRM/civicrm-4.4.0-drupal/./CRM/Utils/String.php 2013-10-24 07:53:25.000000000 +1100
+++ /data/Work/IT/CiviCRM/Local/4.4.0/cbf/php/./CRM/Utils/String.php 2013-10-29 07:56:40.574785938 +1100
@@ -410,8 +410,11 @@
    */
   static function htmlToText($html) {
     require_once 'packages/html2text/rcube_html2text.php';
-    $converter = new rcube_html2text($html);
-    return $converter->get_text();
+    $token_html = preg_replace('!\{([a-z_.]+)\}!i', 'token:{$1}', $html);
+    $converter = new rcube_html2text($token_html);
+    $token_text = $converter->get_text();
+    $text = preg_replace('!token\:\{([a-z_.]+)\}!i', '{$1}', $token_text);
+    return $text;
   }
 
   static function extractName($string, &$params) {

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: html2text fails to convert Mailing token links
October 30, 2013, 12:47:57 am
Ken,

Can you please file an issue and then submit a PR in github for this.

Thanks
Kurund
Found this reply helpful? Support CiviCRM

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: html2text fails to convert Mailing token links
October 30, 2013, 08:13:50 pm
ken - curious - did you also observe that if no text version was supplied that no Subject line was included in text only version - we just hit this on a slightly older version. still testing to see if providing the text version (rather than resolving html2text issue) gets around this
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: html2text fails to convert Mailing token links
October 31, 2013, 05:25:42 pm
I haven't tried a text-only email.

Our emails have ...
  • A subject header
  • A html part that we edit when sending the email
  • A text part that's generated from the html part

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: html2text fails to convert Mailing token links
October 31, 2013, 05:36:13 pm
@kurund,

I created issue 13690. However, I'm not set up with github yet, so a PR will not be immediately forthcoming, sorry.

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: html2text fails to convert Mailing token links
October 31, 2013, 05:39:23 pm
understood - what i meant was when the text version created by html2text is received and viewed as text only - as well as missing tokens, is it missing the subject?
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: html2text fails to convert Mailing token links
October 31, 2013, 07:58:03 pm
@petednz,

I see the subject in the text version. I'm using Gmail, so I normally see the HTML-part, but I can see the text-part by clicking on "Message text garbled?".

Since the Subject is a header, and not part of the text-part, I'm kinda surprised you're not seeing it.

<alert style="dad joke">Could it be a mail client issue that you're being "subjected" to?</alert>

Ken

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: html2text fails to convert Mailing token links
October 31, 2013, 08:00:59 pm
all we know is client had a couple of 'my email didn't have any data in the tokens' and in both cases what they forwarded also had the <br /> so assuming at this point their mail clients only show them text only - but we also noted that in both cases the subject was blank - when we know the html one was fine - hence the tiny thread connecting two issues
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 4.4 Release Testing »
  • html2text fails to convert Mailing token links

This forum was archived on 2017-11-26.