Using a Name <Email> in Reply-to results in a RfcComplianceException

Created on 27 September 2023, over 1 year ago
Updated 30 September 2023, over 1 year ago

Problem/Motivation

Send an email with a Reply-to set as "Name " results in

Symfony\Component\Mime\Exception\RfcComplianceException: Email "Name" does not comply with addr-spec of RFC 2822. in Symfony\Component\Mime\Address->__construct() (line 54 of /var/www/html/vendor/symfony/mime/Address.php).

This is because the SymfonyMailer::parseMailboxes method returns mailboxes as associative array, which is neither supported by symfony/mime:^5.3 Address::createArray() nor symfony/mime:^6.0 Address::createArray().

If $mailboxes[] = $mailbox_name . ' <' . $mailbox_address . '>'; or $mailboxes[] = $mailbox_raw; is used, then the Reply-to is set correctly.

Steps to reproduce

My use case was sending the Test Newsletter with Simplenews module. The Newsletter "From name" is set to "Name".

But this would not work if calling the following:

  \Drupal::get('plugin.manager.mail')->mail('my_module', 'my_module_key', 'blah@example.com', \Drupal\Core\Language\LanguageInterface::LANGCODE_DEFAULT, [], 'Name <blah@example.com>');

Proposed resolution

Change $mailboxes to use an indexed array rather than an associative array as supported by symfony/mime.

Remaining tasks

Review an appropriate fix.

API changes

SymfonyMailer::parseMailboxes() will no longer return an associative array.

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mradcliffe USA

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

Comments & Activities

  • Issue created by @mradcliffe
  • @mradcliffe opened merge request.
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States mradcliffe USA

    I added a merge request with a simple fix, but this may not be the most appropriate resolution, and it does not have any tests.

  • πŸ‡ΊπŸ‡ΈUnited States zengenuity

    Thanks for the MR. I think this approach sounds reasonable. There's already an indexed array below that code for addresses without name.

    My only issue is that I think we should use $mailbox_raw instead of reconstructing the address. In my code, I'm removing the quotes from the name. I'm not totally sure where I got that from. Either Swiftmailer or Symfony Mailer module code probably. I'm not sure it's appropriate. We could probably just get rid of the line creating $mailbox_name and use $mailbox_raw once we validate the address.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States zengenuity
  • πŸ‡ΊπŸ‡ΈUnited States mradcliffe USA

    My only issue is that I think we should use $mailbox_raw instead of reconstructing the address. In my code, I'm removing the quotes from the name. I'm not totally sure where I got that from. Either Swiftmailer or Symfony Mailer module code probably. I'm not sure it's appropriate. We could probably just get rid of the line creating $mailbox_name and use $mailbox_raw once we validate the address.

    +1. I was thinking this as well, but I wasn't sure about using a variable with the name "raw" in it. :-)

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States zengenuity

    I committed the updated version of the code.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024