Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2025

Conversation

onlime
Copy link
Contributor

@onlime onlime commented Apr 14, 2025

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
61 checks passed
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