Skip to content

Commit

Permalink
[9.x] Add X headers when using Mail::alwaysTo (#41101)
Browse files Browse the repository at this point in the history
* add x headers when forgetting

* fqcn

* add tests back

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
morrislaptop and taylorotwell committed Feb 18, 2022
1 parent d9f7f2b commit 18a996f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ protected function renderView($view, $data)
*/
protected function setGlobalToAndRemoveCcAndBcc($message)
{
$message->forgetTo();

$message->to($this->to['address'], $this->to['name'], true);

$message->forgetCc();
Expand Down
37 changes: 37 additions & 0 deletions src/Illuminate/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ public function to($address, $name = null, $override = false)
return $this->addAddresses($address, $name, 'To');
}

/**
* Remove all "to" addresses from the message.
*
* @return $this
*/
public function forgetTo()
{
if ($header = $this->message->getHeaders()->get('To')) {
$this->addAddressDebugHeader('X-To', $this->message->getTo());

$header->setAddresses([]);
}

return $this;
}

/**
* Add a carbon copy to the message.
*
Expand Down Expand Up @@ -134,6 +150,8 @@ public function cc($address, $name = null, $override = false)
public function forgetCc()
{
if ($header = $this->message->getHeaders()->get('Cc')) {
$this->addAddressDebugHeader('X-Cc', $this->message->getCC());

$header->setAddresses([]);
}

Expand Down Expand Up @@ -169,6 +187,8 @@ public function bcc($address, $name = null, $override = false)
public function forgetBcc()
{
if ($header = $this->message->getHeaders()->get('Bcc')) {
$this->addAddressDebugHeader('X-Bcc', $this->message->getBcc());

$header->setAddresses([]);
}

Expand Down Expand Up @@ -220,6 +240,23 @@ protected function addAddresses($address, $name, $type)
return $this;
}

/**
* Add an address debug header for a list of recipients.
*
* @param string $header
* @param \Symfony\Component\Mime\Address[] $addresses
* @return $this
*/
protected function addAddressDebugHeader(string $header, array $addresses)
{
$this->message->getHeaders()->addTextHeader(
$header,
implode(', ', array_map(fn ($a) => $a->toString(), $addresses)),
);

return $this;
}

/**
* Set the subject of the message.
*
Expand Down
10 changes: 8 additions & 2 deletions tests/Mail/MailMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public function testGlobalToIsRespectedOnAllMessages()

$sentMessage = $mailer->send('foo', ['data'], function (Message $message) {
$message->from('hello@laravel.com');
$message->to('nuno@laravel.com');
$message->cc('dries@laravel.com');
$message->bcc('james@laravel.com');
});
Expand All @@ -209,8 +210,13 @@ public function testGlobalToIsRespectedOnAllMessages()
});

$this->assertSame('taylor@laravel.com', $sentMessage->getEnvelope()->getRecipients()[0]->getAddress());
$this->assertStringNotContainsString('dries@laravel.com', $sentMessage->toString());
$this->assertStringNotContainsString('james@laravel.com', $sentMessage->toString());
$this->assertDoesNotMatchRegularExpression('/^To: nuno@laravel.com/m', $sentMessage->toString());
$this->assertDoesNotMatchRegularExpression('/^Cc: dries@laravel.com/m', $sentMessage->toString());
$this->assertMatchesRegularExpression('/^X-To: nuno@laravel.com/m', $sentMessage->toString());
$this->assertMatchesRegularExpression('/^X-Cc: dries@laravel.com/m', $sentMessage->toString());
$this->assertMatchesRegularExpression('/^X-Bcc: james@laravel.com/m', $sentMessage->toString());
$this->assertFalse($recipients->contains('nuno@laravel.com'));
$this->assertFalse($recipients->contains('dries@laravel.com'));
$this->assertFalse($recipients->contains('james@laravel.com'));
}

Expand Down

0 comments on commit 18a996f

Please sign in to comment.