Skip to content
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

[9.x] Add X headers when using Mail::alwaysTo #41101

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ protected function renderView($view, $data)
*/
protected function setGlobalToAndRemoveCcAndBcc($message)
{
$message->forgetTo();
$message->to($this->to['address'], $this->to['name'], true);

$message->forgetCc();
Expand Down
34 changes: 34 additions & 0 deletions src/Illuminate/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ 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->addAddressesDebugHeader('X-To', $this->message->getTo());
$header->setAddresses([]);
}

return $this;
}

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

Expand Down Expand Up @@ -169,6 +185,7 @@ public function bcc($address, $name = null, $override = false)
public function forgetBcc()
{
if ($header = $this->message->getHeaders()->get('Bcc')) {
$this->addAddressesDebugHeader('X-Bcc', $this->message->getBcc());
$header->setAddresses([]);
}

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

/**
* Adds a debug header for a list of recipients.
*
* @param string $header
* @param \Symfony\Component\Mime\Address[] $addresses
* @return $this
*/
protected function addAddressesDebugHeader(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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this for extra measure

$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());
driesvints marked this conversation as resolved.
Show resolved Hide resolved
$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'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be a bit carefully here because we're not actually checking anymore if the CC: and BCC: headers are not present anymore (something the original tests implicitly did). I don't think BCC recipients, for example, are listed in the recipients of an envelope to begin with?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah ok i'll add those checks back in, with the specific headers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BCC won't be in the string (but this is tested by checking the list of recipients)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints Specifically checking for these headers now 👍 Confirmed that the BCC address is normally returned with $sentMessage->getEnvelope()->getRecipients()

$this->assertFalse($recipients->contains('james@laravel.com'));
}

Expand Down