drupal_html_to_text() which is called by simplenews_html_to_text() completely ruins the mail output of any newsletter node. Newlines are completely missing, with sometimes even the last characters of the line being cut off.

I tend to think that the error stems from using drupal_html_to_text() with the wrong data input, as Heine pointed out here and here that the core function does work correctly, however the node body is just richtext which needs to be converted to HTML first.
However, after hours of work, I still didn't manage to get that done correctly, so I'm asking whether someone's more into this and has an idea what needs to be fixed.
Thanks!

Comments

simon georges’s picture

See #299138: Improve \Drupal\Core\Utility\Mail::htmlToText(). I think we all have these issues...
If somebody finds a way to have it working, I'd eventually be open to commit it, although it's not contrib task to "fix" core.

simon georges’s picture

Status: Active » Postponed
beatnikdude’s picture

subscribe

simon georges’s picture

Status: Postponed » Closed (duplicate)

I'm actually closing this one, which is clearly a duplicate of the "core" one. Please subscribe to the other one, so everybody can follow the same thread.

stephandale’s picture

Status: Closed (duplicate) » Active

As far as I can see after a brief look over the code, there will be a problem regardless of whether the core bug is fixed or not...

simplenews_mail (in simplenews.module) calls simplenews_build_node_mail which calls simplenews_html_to_text (in simplenews.mail.inc) which calls drupal_html_to_text (in mail.inc). The email is then sent using drupal_mail (in mail.inc) which calls drupal_html_to_text again.

The fact that drupal_html_to_text is called twice creates a problem, because the first call strips out all the HTML and the second call screws up the line breaks (drupal_html_to_text requires HTML, as mentioned in the initial report).

So, if simplenews_html_to_text were to return just $text rather than running it through drupal_html_to_text (and the function were renamed accordingly) then we'd be left with only the problems from the core bug, but as it stands there will be a problem regardless of whether the core bug is fixed or not.

stephandale’s picture

Removing the drupal_html_to_text call from the end of simplenews_html_to_text and patching mail.inc with the core patch in http://drupal.org/node/299138 seems to fix the problem (ignoring problems that the core patch doesn't fix _yet_).

simon georges’s picture

When is drupal_mail() calling drupal_html_to_text() ?

stephandale’s picture

drupal_mail() doesn't call it directly, but the default mail system calls it in its format() function which is called by drupal_mail(). See lines 144 - 148 in includes/mail.inc for calls to mail system and line 26 in modules/system/system.mail.inc for call to drupal_html_to_text().

pillarsdotnet’s picture

Status: Active » Needs review
StatusFileSize
new12.69 KB

Here's a workaround until the following issue is resolved:

#299138: Improve \Drupal\Core\Utility\Mail::htmlToText()

stephandale’s picture

StatusFileSize
new11.91 KB

Thanks.

The attached patch is identical to yours except I've removed your call to htmlspecialchars() from simplenews_text() as it should return plain text rather than HTML.

Emails now look a lot better, but not quite right - I'm investigating some odd spacing with lists.

pillarsdotnet’s picture

Crosslinked from #299138-149: Improve \Drupal\Core\Utility\Mail::htmlToText()

If you do make changes to the added html_to_text.inc file, please submit those changes back to the above issue; thanks.

stephandale’s picture

StatusFileSize
new24.34 KB

Will do, though that's not necessary with this...

With the last patch, indented content has incorrect spacing when on Drupal 7.2. This patch fixes this by incorporating the latest drupal_wrap_mail() function as simplenews_wrap_mail(). Tests have also been added. Both changes based on code from #299138-148: Improve \Drupal\Core\Utility\Mail::htmlToText().

Edit: Well I shouldn't really say "the latest drupal_wrap_mail()" - it's the latest in #299138-148: Improve \Drupal\Core\Utility\Mail::htmlToText() as of now.

Edit 30/05/2011: Use the patch in comment#13 instead of this one

stephandale’s picture

StatusFileSize
new24.34 KB

Here's that patch again with the correct function name this time (simplenews_wrap_mail() as opposed to simpletext_wrap_mail())! As before it's for Drupal 7.2.

I've noticed an oddity where preg_replace() behaves differently on different servers, probably due to the use of /m, the PCRE_MULTILINE modifier, so I'll be looking into that via #299138: Improve \Drupal\Core\Utility\Mail::htmlToText() and will update this issue when appropriate.

pillarsdotnet’s picture

pillarsdotnet’s picture

StatusFileSize
new26.51 KB
stephandale’s picture

StatusFileSize
new25.56 KB

Same patch with _drupal_html_to_text() call corrected to _simplenews_html_to_text() for Drupal 7.2.

stephandale’s picture

StatusFileSize
new25.73 KB

I've found that the use of the PCRE_MULTILINE modifier m in preg_replace() resulted in different behaviour on different servers, possibly due to the difference in the PCRE version though I can't be sure at this stage.

Server 1 (tests pass):

php > $s = "one\ntwo\n";
php > echo preg_replace('/^/m','FOO',$s);
FOOone
FOOtwo

Server 2 (tests fail):

php > $s = "one\ntwo\n";
php > echo preg_replace('/^/m','FOO',$s);
FOOone
FOOtwo
FOO

Note how on server 2 (which has an older PCRE library), the replacement is also made on the trailing new line.

I've added a three line workaround to the _simplenews_wrap_mail() function, so tests are now green on both servers. The attached patch is the same as the last but with the workaround.

stephandale’s picture

StatusFileSize
new25.76 KB

Attached is the same except with the simpler solution by pillarsdotnet in core issue http://drupal.org/node/299138#comment-4544368

pillarsdotnet’s picture

@stephandale: You beat me to it! :-)

berdir’s picture

Bump to trigger testbot and see if this still applies.

berdir’s picture

Category: support » bug

Updated the patch with the following changes:

- moved tests into existing tests/simplenews.test file instead of creating a new one
- Updated the call to simplenews_text()
- renamed html_to_text.inc to includes/simplenews.html_to_text.inc to be more in line with everything else.

This should allow to test bot to pick up the new tests. Just to see if it works..

I haven't actually reviewed the patch yet.

Question:

It looks like that fun html_to_text.inc is part of mailsystem.module as well. Instead of adding it yet another time, can't we call it from mailsystem.module if it's enabled? Then we actually don't need all that code/tests. Something like

if (module_exists('mailsystem')) {
  // call stuff from mailsystem
}

inside simplenews_text().

I'm also open to discuss to depend on mailsystem completely, especially if we can get support for the mail theme setting in it, see #1382036: Add a common way to configure the theme that will render the email and/or other advantages of doing so. After all, mimemail now does, so most probably have it anyway.

berdir’s picture

StatusFileSize
new25.51 KB

Erm, and now with patch.

Status: Needs review » Needs work

The last submitted patch, simplenews_html_to_text-1099188-21.patch, failed testing.