Problem/Motivation

MailHandler::sendMail() always sets a Content-Type: text/html; charset=UTF-8 header. However, when the site uses core's default mail backend (PhpMail), or any backend that inherits PhpMail::format(), the body is converted to plain text via MailFormatHelper::htmlToText() before being sent.

The result is a message whose body is plain text but whose header claims it's HTML. Mail clients then render the plain text as HTML: line breaks are lost, and since the plain-text conversion decodes HTML entities, any text that was escaped in the rendered template (order data, customer input, etc.) is interpreted as markup by the recipient's client.

The Content-Type header should match the actual body format.

Proposed resolution

Only send the HTML Content-Type header when the mail backend actually produces an HTML body. If the backend's format() method is the one declared by PhpMail, drop our override and let core's text/plain header apply.

Sites wanting HTML emails keep using an HTML-capable backend (Symfony Mailer, Mail System + Swiftmailer, etc.), which is unaffected.

Remaining tasks

  • Review the MR.

User interface changes

None.

API changes

None. Sites on the default PhpMail backend will now receive Commerce emails as text/plain, matching their actual content.

Issue fork commerce-3641202

Command icon 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:

Comments

jsacksick created an issue. See original summary.

  • jsacksick committed 9bf0a4fe on 3.x
    fix: #3641202 Do not send an HTML Content-Type header when the mail...

  • jsacksick committed 7ffa8d1b on 4.x
    fix: #3641202 Do not send an HTML Content-Type header when the mail...
jsacksick’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.