Problem/Motivation

I am getting the following error when trying to send an email that has a bcc in the message header
$message['headers']['bcc'] = 'any@email.com'

The website encountered an unexpected error. Please try again later.
AssertionError: assert(isset($this->addresses[$name])) in assert() (line 58 of modules/contrib/symfony_mailer/src/BaseEmailTrait.php).
assert(, 'assert(isset($this->addresses[$name]))') (Line: 58)
Drupal\symfony_mailer\Email->setAddress('bcc', Array) (Line: 146)
Drupal\symfony_mailer\LegacyMailerHelper->emailFromArray(Object, Array) (Line: 136)
Drupal\symfony_mailer\Plugin\EmailBuilder\LegacyEmailBuilder->build(Object)
call_user_func(Array, Object) (Line: 490)
Drupal\symfony_mailer\Email->process() (Line: 180)
Drupal\symfony_mailer\Mailer->doSend(Object) (Line: 150)
Drupal\symfony_mailer\Mailer->Drupal\symfony_mailer\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 159)
Drupal\symfony_mailer\Mailer->send(Object) (Line: 292)
Drupal\symfony_mailer\Email->send() (Line: 92)

Steps to reproduce

using the legacy mailer try to send an email with a bcc header.

if I remove the bcc, the email sends.

The bcc is a valid email address.

Comments

loze created an issue. See original summary.

loze’s picture

Issue summary: View changes
adamps’s picture

Thanks for the report. I believe it is confusion over the header case (as a workaround, probably 'Bcc' would work). Probably introduced by #3334230: Support all headers in LegacyEmailBuilder, where the different cases were starting to drive me crazy😃.

The simplest fix is to change LegacyMailerHelper::emailFromArray() where the comment says "Convert to standard case.", make it like this

        // Convert to standard case.
        $name = ucfirst($name))
        if ($name == 'Reply-to') {
          $name = 'Reply-To';
        }

Another option is to copy what Symfony library code does and store all headers in lower case internally. We need to be careful with legacy emails though as Drupal uses capitals.

loze’s picture

Thank you. Changing it to $message['headers']['Bcc'] = 'any@email.com' (capital B) got rid of the error.

adamps’s picture

Title: AssertionError when sending legacy email that uses bcc header » AssertionError for legacy email with lower-case address headers

Thanks for reporting back.

sokru’s picture

Version: 1.2.0 » 1.x-dev
Status: Active » Needs review
StatusFileSize
new1.12 KB

Faced the same issue when upgrading to Drupal 10. The tests will fail without the $name = ucfirst($name)) change.

adamps’s picture

Status: Needs review » Needs work

Thanks for the patch. It's good and it works.

Two comments please:

  1. Symfony library stores all headers in lower case. I think the patch would be even better if we do the same. It's consistent with the library, and simpler code. We can remove if ($name == 'Reply-to') and we don't need to call ucfirst().
  2. There is a similar problem on BaseEmailTrait::setAddress() - we can put $name = strtolower($name) there too. Then fix all the string constants to lower case 'from' instead of 'From' etc.
sokru’s picture

Status: Needs work » Needs review
StatusFileSize
new4.2 KB
new3.39 KB

I hope I got all changes requested.

Status: Needs review » Needs work

The last submitted patch, 8: 3352597-assertion-error-on-legacy-email-8.patch, failed testing. View results

sokru’s picture

Status: Needs work » Needs review
StatusFileSize
new323 bytes
new4.62 KB

Missed a one thing on my previous patch.

adamps’s picture

Status: Needs review » Needs work

Great thanks.

public function setAddress(string $name, $addresses) {
     assert(isset($this->addresses[$name]));
-    if ($name == 'To') {
+    $name = strtolower($name);
+    if ($name == 'to') {
       $this->valid(self::PHASE_BUILD);
     }

You should put the strtolower before the assert.

Then you can put ToEmailAdjuster back as it was or also change the other 4 for consistency.

sokru’s picture

Status: Needs work » Needs review
StatusFileSize
new1.71 KB
new6.38 KB

Updated the all adjuster constants to lowercase.

  • AdamPS committed 8a6b6aae on 1.x authored by sokru
    Issue #3352597 by sokru, AdamPS: AssertionError for legacy email with...
adamps’s picture

Status: Needs review » Fixed

Great thanks

Status: Fixed » Closed (fixed)

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