Skip to content

Commit 18a996f

Browse files
[9.x] Add X headers when using Mail::alwaysTo (#41101)
* add x headers when forgetting * fqcn * add tests back * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent d9f7f2b commit 18a996f

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/Illuminate/Mail/Mailer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ protected function renderView($view, $data)
389389
*/
390390
protected function setGlobalToAndRemoveCcAndBcc($message)
391391
{
392+
$message->forgetTo();
393+
392394
$message->to($this->to['address'], $this->to['name'], true);
393395

394396
$message->forgetCc();

src/Illuminate/Mail/Message.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ public function to($address, $name = null, $override = false)
105105
return $this->addAddresses($address, $name, 'To');
106106
}
107107

108+
/**
109+
* Remove all "to" addresses from the message.
110+
*
111+
* @return $this
112+
*/
113+
public function forgetTo()
114+
{
115+
if ($header = $this->message->getHeaders()->get('To')) {
116+
$this->addAddressDebugHeader('X-To', $this->message->getTo());
117+
118+
$header->setAddresses([]);
119+
}
120+
121+
return $this;
122+
}
123+
108124
/**
109125
* Add a carbon copy to the message.
110126
*
@@ -134,6 +150,8 @@ public function cc($address, $name = null, $override = false)
134150
public function forgetCc()
135151
{
136152
if ($header = $this->message->getHeaders()->get('Cc')) {
153+
$this->addAddressDebugHeader('X-Cc', $this->message->getCC());
154+
137155
$header->setAddresses([]);
138156
}
139157

@@ -169,6 +187,8 @@ public function bcc($address, $name = null, $override = false)
169187
public function forgetBcc()
170188
{
171189
if ($header = $this->message->getHeaders()->get('Bcc')) {
190+
$this->addAddressDebugHeader('X-Bcc', $this->message->getBcc());
191+
172192
$header->setAddresses([]);
173193
}
174194

@@ -220,6 +240,23 @@ protected function addAddresses($address, $name, $type)
220240
return $this;
221241
}
222242

243+
/**
244+
* Add an address debug header for a list of recipients.
245+
*
246+
* @param string $header
247+
* @param \Symfony\Component\Mime\Address[] $addresses
248+
* @return $this
249+
*/
250+
protected function addAddressDebugHeader(string $header, array $addresses)
251+
{
252+
$this->message->getHeaders()->addTextHeader(
253+
$header,
254+
implode(', ', array_map(fn ($a) => $a->toString(), $addresses)),
255+
);
256+
257+
return $this;
258+
}
259+
223260
/**
224261
* Set the subject of the message.
225262
*

tests/Mail/MailMailerTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public function testGlobalToIsRespectedOnAllMessages()
200200

201201
$sentMessage = $mailer->send('foo', ['data'], function (Message $message) {
202202
$message->from('[email protected]');
203+
$message->to('[email protected]');
203204
$message->cc('[email protected]');
204205
$message->bcc('[email protected]');
205206
});
@@ -209,8 +210,13 @@ public function testGlobalToIsRespectedOnAllMessages()
209210
});
210211

211212
$this->assertSame('[email protected]', $sentMessage->getEnvelope()->getRecipients()[0]->getAddress());
212-
$this->assertStringNotContainsString('[email protected]', $sentMessage->toString());
213-
$this->assertStringNotContainsString('[email protected]', $sentMessage->toString());
213+
$this->assertDoesNotMatchRegularExpression('/^To: [email protected]/m', $sentMessage->toString());
214+
$this->assertDoesNotMatchRegularExpression('/^Cc: [email protected]/m', $sentMessage->toString());
215+
$this->assertMatchesRegularExpression('/^X-To: [email protected]/m', $sentMessage->toString());
216+
$this->assertMatchesRegularExpression('/^X-Cc: [email protected]/m', $sentMessage->toString());
217+
$this->assertMatchesRegularExpression('/^X-Bcc: [email protected]/m', $sentMessage->toString());
218+
$this->assertFalse($recipients->contains('[email protected]'));
219+
$this->assertFalse($recipients->contains('[email protected]'));
214220
$this->assertFalse($recipients->contains('[email protected]'));
215221
}
216222

0 commit comments

Comments
 (0)