Skip to content

[12.x] Fix cc/bcc/replyTo address merging in MailMessage#55404

Merged
taylorotwell merged 2 commits into
laravel:12.xfrom
onlime:fix-mailmessage-address-merging
Apr 15, 2025
Merged

[12.x] Fix cc/bcc/replyTo address merging in MailMessage#55404
taylorotwell merged 2 commits into
laravel:12.xfrom
onlime:fix-mailmessage-address-merging

Conversation

@onlime

@onlime onlime commented Apr 14, 2025

Copy link
Copy Markdown
Contributor

Address merging in MailMessage is broken, when cc(), bcc(), or replyTo() are called multiple times and we're using unnamed address(es) (sequentially indexed), e.g.:

$message = (new MailMessage)
    ->bcc('bcc@example.com', 'Test')
    ->bcc(['bcc2@example.com', 'bcc3@example.com']);

results in:

> $message->bcc
= [
    [
      "bcc@example.com",
      "Test",
    ],
    [
      "bcc3@example.com",
      null,
    ],
  ]

expected:

> $message->bcc
= [
    [
      "bcc@example.com",
      "Test",
    ],
    [
      "bcc2@example.com",
      null,
    ],
    [
      "bcc3@example.com",
      null,
    ],
  ]

The first item of the 2nd bcc(['bcc2@example.com', 'bcc3@example.com']) call is lost, as MailMessage::bcc() (same in cc() and replyTo()) uses PHP's += union operator to merge the arrays and in this case does not add the first item as there is already an address with 0 array key present.

Using array_merge() instead fixes it.

For me, this is a common use case, as in my app most notifications are using a MailMessage template with an admin recipient set in an initial bcc() call, while the notification's getMailMessage() method may add one or more bcc recipients, usually unnamed (indexed with int keys).

@taylorotwell taylorotwell merged commit 5829a18 into laravel:12.x Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants