Problem/Motivation

The drupal_html_to_text function removes images throwing away the alternate text. This is arguably bad from an accessibility perspective. Images could contain semantically meaningful information which is expressed in the alt attributes. In addition, I did a quick comparison and mailchimp appears to add alt attributes from images to their plain text versions by default. It is a little different with them as their plain text is editable. However, if the industry leader is doing it by default, it's probably a good idea.

I suggest replacing images with alt text when alt text exists. Below is one proposed solution (patch coming d7/d8).

include/mail.inc (d7)

401: $supported_tags = array('a', 'em', 'i', 'strong', 'b', 'br', 'p', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img');
...
414:  // Replace images with alternate text
415:  $string = preg_replace('@<img[^>]+?alt="([^"]*)"[^>]*?>@i', '$1', $string);

Note, there is a related 250 commenter, but it seems to be inactive (2011) and didn't appear to mention image/alt handling.
See #299138: Improve \Drupal\Core\Utility\Mail::htmlToText()

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

jasonlttl’s picture

StatusFileSize
new1.22 KB

Attached is a patch for d7 (d8 coming later). This is more intended for reference by others. I doubt it would be a good idea to change the behavior of the d7 function unexpectedly.

jasonlttl’s picture

Status: Active » Needs review
StatusFileSize
new2.15 KB

Here is a patch that adds this functionality to Drupal 8. It includes some tests as well. Let me know if there are any changes needed.

mgifford’s picture

#2: drupal-imgaltplain-1911876-2.patch queued for re-testing.

mgifford’s picture

Issue tags: +Accessibility

This is an interesting idea. When is drupal_html_to_text() used? I do wonder how often the alt text is written in a way that it would make sense in a sentence. I expect it would benefit sighted people as much as the non-sighted if the alt text is written the right way.

Useful to consider though.

mgifford’s picture

Issue tags: -Accessibility

#2: drupal-imgaltplain-1911876-2.patch queued for re-testing.

mgifford’s picture

Jalandhar’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll
mgifford’s picture

Status: Needs work » Needs review
StatusFileSize
new2.06 KB

Thank goodness for grep.. Bloody files moving about all over the place.

Thanks again @Jalandhar!

tibbsa’s picture

Issue tags: -Needs reroll

Still applies to 8.0.x.

This winds up getting called for all outgoing mail by default, from what I can tell. (If you're using PhpMail as the mailer, PhpMail::format() calls MailFormatHelper::htmlToText().) Other mailers might or might not do this (if they are sending MIME messages with plain-text parts, for example), but there seems to be some suggestion that one option would be to still rely on Drupal's implementation to produce plain-text versions, per the comment at MailInterface::format():

  /**
   * Allows to preprocess, format, and postprocess a mail message before it is
   * passed to the sending system. By default, all messages may contain HTML and
   * are converted to plain-text by the Drupal\Core\Mail\Plugin\Mail\PhpMail
   * implementation. For example, an alternative implementation could override
   * the default implementation and also sanitize the HTML for usage in a MIME-
   * encoded email, but still invoking the Drupal\Core\Mail\Plugin\Mail\PhpMail
   * implementation to generate an alternate plain-text version for sending.
   ...
   */
mgifford’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs review » Postponed

This is a feature request, so it's getting bumped to 8.1.

@tibbsa you're happy with the patch as it is now? We can try to bring it into 8.1.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jungle’s picture

Title: drupal_html_to_text img/alt handling » MailFormatHelper::htmlToText() img/alt handling
Issue summary: View changes
Status: Postponed » Needs work
Issue tags: +Needs reroll
  1. Applying IS template.
  2. Changing the title as drupal_html_to_text() is now MailFormatHelper::htmlToText()
  3. Let's make this active again. NW for rerolling.
hardik_patel_12’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new2.08 KB

Re-rolling for 9.1.x , kindly review.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.86 KB

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.