Fix the format=flowed; delsp=yes encoding of email messages

Created on 31 May 2024, 10 months ago
Updated 5 August 2024, 8 months ago

Problem/Motivation

Drupal documents that email messages are formatted using format=flowed; delsp=yes encoding, and specifies this encoding in the Content-Type header for email messages. However, #2078917: E-mails contain double spaces in soft-wrapped sentences β†’ broke the format=flowed; delsp=yes encoding (I guess because folks didn't test on a client that interprets this encoding, and didn't fully understand how the encoding works).

In addition, the PhpMail class re-wraps email messages by calling MailFormatHelper::htmlToText() and then MailFormatHelper::wrapMail(). First of all, MailFormatHelper::htmlToText() calls MailFormatHelper::wrapMail() on each chunk internally, so this is not necessary.

More importantly, when called on an already-formatted body, MailFormatHelper::wrapMail() breaks the format=flowed; delsp=yes encoding because it converts soft breaks to hard breaks. So, this unnecessary call to MailFormatHelper::wrapMail() needs to be removed.

Steps to reproduce

To reproduce this issue, you will need to read email messages sent by Drupal using any email client that can properly interpret format=flowed; delsp=yes encoding, such as Mutt, Thunderbird, etc.

In these email readers, you will see that soft-wrapped lines have been converted to hard-wrap: Lines that were supposed to be soft-wrapped purely for purposes of sending short lines on the wire, and unwrapped/re-flowed by the email reader, will still be hard-wrapped in the displayed email message. We want these lines to appear "flowed," and only soft-wrapped by the email reader where needed to fit in the user's viewport. See the attached screenshots for what email messages look like in an email reader (e.g. Thunderbird) with and without the patch.

In addition, in any cases where MailFormatHelper::wrapMail() is called only once on the email body (e.g. the SymfonyMailer plugin), email readers supporting format=flowed; delsp=yes will be missing a space when they re-flow wrapped lines, which is also incorrect. A space is deleted when the encoding is interpreted, but no extra space was added during encoding, so you end up with words stuck together.

Proposed resolution

Undo the change from #2078917: E-mails contain double spaces in soft-wrapped sentences β†’ and port πŸ› DefaultMailSystem implements MailSystemInterface::format() incorrectly RTBC to Drupal 11.

Remaining tasks

User interface changes

Vastly improved display of email messages in email clients that interpret format=flowed; delsp=yes encoding. An extra space will be present in soft-wrapped lines if the email reader does not properly interpret this encoding.

Before

After

API changes

Email messages are already specified in the email headers and documented in the code as being format=flowed; delsp=yes so there is no API change here.

Data model changes

None

Release notes snippet

πŸ› Bug report
Status

Fixed

Version

10.3 ✨

Component
MailΒ  β†’

Last updated 12 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024