Uncaught RfcComplianceException when email From name contains a comma

Created on 30 July 2021, over 3 years ago
Updated 31 March 2023, over 1 year ago

Problem/Motivation

After the upgrade to 9.2.x two of my sites began to WSoD with uncaught exceptions when they send emails:

Symfony\Component\Mime\Exception\RfcComplianceException: Email ""Foo" does not comply with addr-spec of RFC 2822. in Symfony\Component\Mime\Address->__construct() (line 56 of \path\to\drupal\vendor\symfony\mime\Address.php).

These two sites have names that contain commas. I tested the issue on a vanilla install of 9.3.x-dev and gave the site the name "Foo, Bar, and Baz". The above exception was produced by testing the issue with that site. The exception was thrown because Symfony's MIME component attempted to validate the From address for an email up to the first comma, not the entire address.

I backtraced the issue to a commit made two months ago for #84883: Unicode::mimeHeaderEncode() doesn't correctly follow RFC 2047 β†’ . In comments #202 and #203 of that issue code was added to the patch that explodes multiple From addresses using commas as a delimiter. See the following snippet from core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php lines 87-92:

    $headers = new Headers();
    foreach ($message['headers'] as $name => $value) {
      if (in_array(strtolower($name), self::MAILBOX_LIST_HEADERS, TRUE)) {
        $value = explode(',', $value);
      }
      $headers->addHeader($name, $value);
    }

Unfortunately, it does not account for commas in the name part of addresses and explodes them too. Thus, I assume this issue would occur for any names that include commas, not only site names.

I attempted to write a failing test for this issue - which I will upload - and wasn't successful. I don't know why, but the test passes with the From address being generated correctly. That's despite the fact that this issue is really easy to reproduce with a vanilla D9 install. But this might explain why this issue wasn't caught before now. There is a test for special characters in email From fields, including commas. It could be that the email test is flawed in a way that causes this problem to not be caught.

This issue is major because it is preventing the site from carrying out normal operations that require sending emails. In my case, I discovered it when attempting to send password reset emails.

Steps to reproduce

1. Create a vanilla install of Drupal 9.
2. Set the site name to one that contains commas, e.g. "Foo, Bar, and Baz".
3. Log out (this might not be necessary, but I did it every time I tested).
4. Visit /user/password and attempt to send yourself a password reset email.

Expected behavior:
The front page should load with a message saying that a password reset email has been sent.

Actual behavior:
After submitting the password reset form the site gives you a WSoD with the above uncaught exception.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Fixed

Version

9.5

Component
MailΒ  β†’

Last updated 14 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States dcam

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024