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 #1447236: DefaultMailSystem implements MailSystemInterface::format() incorrectly 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
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | with-patch-soft-wrapped.png | 31.15 KB | mfb |
| #8 | without-patch-hard-wrapped.png | 34.22 KB | mfb |
Issue fork drupal-3451611
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3451611-fix-the-formatflowed
changes, plain diff MR !8249
Comments
Comment #3
mfbComment #4
mfbComment #5
smustgrave commentedCan we provide examples in the summary for how this is better?
Thanks
Comment #6
mfb@smustgrave I fleshed out part of the steps to reproduce a bit. If we need more, maybe you can clarify what you would like an example of.
Comment #7
smustgrave commentedcan we get screenshots of the improvements
Comment #8
mfbOk I added some screenshots for the default out-of-the-box mailer, with and without the patch applied. (The other bug fixed by this patch is that the body is soft-wrapped but words are stuck together due to missing space; I didn't yet dig into how to reproduce it, but I suspect it could happen with a non-default mailer setup.)
Comment #9
smustgrave commentedThanks, examples seem good improvements.
Comment #10
quietone commentedI read the issue summary, comments and the MR. Just about everything here is in order. It would be better if the screenshots were available from the issue summary but there are only 8 comments so far, so they are not hard to find. There are no unanswered questions. The comments in the MR are clear and easy to understand.
Leaving at RTBC
Comment #11
xjmEmbedding the screenshots.
Comment #12
xjmMade a change to the issue summary... originally it sounded like this needed to be postponed on reverting another issue, but the issue was committed in 2013, so what is actually proposed here is to undo the change from that issue and implement a different fix instead. The situation was further confused by the fact that a better fix was proposed for Drupal 7 but not for Drupal 8, violating the backport policy. :)
Comment #13
xjmAn important thing to understand for this issue (which I had to re-read the summary to get) is that
MailFormatHelper::htmlToText()already callsMailFormatHelper::wrapMail(), so the second call in the PHP mailer was redundant.Comment #14
xjmNW for the above comments/suggestions I posted on the MR, which should get reviewed by @mfb, another previous issue contributor, or a relevant maintainer (i.e., credit will not be granted to someone doing a drive-by application of my suggestions). Thanks!
Comment #15
xjmComment #16
mfbThanks @xjm, I further clarified the comments.
Comment #17
smustgrave commentedFeedback appears to be reviewed by @mfb mvoing back to RTBC.
Comment #19
longwaveBackported down to 10.3.x as a valid bug fix, although this is a very minor behaviour change it makes things better and I see no reason to keep it to major releases only.
Committed and pushed c29768f406 to 11.x and 1e5f032e26 to 11.0.x and bc8de78fe2 to 10.4.x and 5be98c43a8 to 10.3.x. Thanks!
Comment #23
longwaveFixing issue status and credits after crossposting with the bot.