bug #33387 [Mailer] maintain sender/recipient name in SMTP envelopes (xabbuh)
This PR was merged into the 4.3 branch.
Discussion
----------
[Mailer] maintain sender/recipient name in SMTP envelopes
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #33383
| License | MIT
| Doc PR |
Commits
-------
46ed0e84cd
maintain sender/recipient name in SMTP envelopes
This commit is contained in:
commit
828e5a4ae3
@ -47,7 +47,7 @@ final class DelayedSmtpEnvelope extends SmtpEnvelope
|
|||||||
return parent::getSender();
|
return parent::getSender();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Address(self::getSenderFromHeaders($this->message->getHeaders())->getAddress());
|
return self::getSenderFromHeaders($this->message->getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setRecipients(array $recipients): void
|
public function setRecipients(array $recipients): void
|
||||||
@ -75,7 +75,7 @@ final class DelayedSmtpEnvelope extends SmtpEnvelope
|
|||||||
foreach (['to', 'cc', 'bcc'] as $name) {
|
foreach (['to', 'cc', 'bcc'] as $name) {
|
||||||
foreach ($headers->all($name) as $header) {
|
foreach ($headers->all($name) as $header) {
|
||||||
foreach ($header->getAddresses() as $address) {
|
foreach ($header->getAddresses() as $address) {
|
||||||
$recipients[] = new Address($address->getAddress());
|
$recipients[] = $address;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,19 +56,19 @@ class SmtpEnvelopeTest extends TestCase
|
|||||||
$headers->addPathHeader('Return-Path', new NamedAddress('return@symfony.com', 'return'));
|
$headers->addPathHeader('Return-Path', new NamedAddress('return@symfony.com', 'return'));
|
||||||
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
||||||
$e = SmtpEnvelope::create(new Message($headers));
|
$e = SmtpEnvelope::create(new Message($headers));
|
||||||
$this->assertEquals('return@symfony.com', $e->getSender()->getAddress());
|
$this->assertEquals(new NamedAddress('return@symfony.com', 'return'), $e->getSender());
|
||||||
|
|
||||||
$headers = new Headers();
|
$headers = new Headers();
|
||||||
$headers->addMailboxHeader('Sender', new NamedAddress('sender@symfony.com', 'sender'));
|
$headers->addMailboxHeader('Sender', new NamedAddress('sender@symfony.com', 'sender'));
|
||||||
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
||||||
$e = SmtpEnvelope::create(new Message($headers));
|
$e = SmtpEnvelope::create(new Message($headers));
|
||||||
$this->assertEquals('sender@symfony.com', $e->getSender()->getAddress());
|
$this->assertEquals(new NamedAddress('sender@symfony.com', 'sender'), $e->getSender());
|
||||||
|
|
||||||
$headers = new Headers();
|
$headers = new Headers();
|
||||||
$headers->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from'), 'some@symfony.com']);
|
$headers->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from'), 'some@symfony.com']);
|
||||||
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
||||||
$e = SmtpEnvelope::create(new Message($headers));
|
$e = SmtpEnvelope::create(new Message($headers));
|
||||||
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
|
$this->assertEquals(new NamedAddress('from@symfony.com', 'from'), $e->getSender());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSenderFromHeadersWithoutFrom()
|
public function testSenderFromHeadersWithoutFrom()
|
||||||
@ -77,10 +77,21 @@ class SmtpEnvelopeTest extends TestCase
|
|||||||
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
||||||
$e = SmtpEnvelope::create($message = new Message($headers));
|
$e = SmtpEnvelope::create($message = new Message($headers));
|
||||||
$message->getHeaders()->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from')]);
|
$message->getHeaders()->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from')]);
|
||||||
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
|
$this->assertEquals(new NamedAddress('from@symfony.com', 'from'), $e->getSender());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRecipientsFromHeaders()
|
public function testRecipientsFromHeaders()
|
||||||
|
{
|
||||||
|
$headers = new Headers();
|
||||||
|
$headers->addPathHeader('Return-Path', 'return@symfony.com');
|
||||||
|
$headers->addMailboxListHeader('To', [new Address('to@symfony.com')]);
|
||||||
|
$headers->addMailboxListHeader('Cc', [new Address('cc@symfony.com')]);
|
||||||
|
$headers->addMailboxListHeader('Bcc', [new Address('bcc@symfony.com')]);
|
||||||
|
$e = SmtpEnvelope::create(new Message($headers));
|
||||||
|
$this->assertEquals([new Address('to@symfony.com'), new Address('cc@symfony.com'), new Address('bcc@symfony.com')], $e->getRecipients());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRecipientsFromHeadersWithNames()
|
||||||
{
|
{
|
||||||
$headers = new Headers();
|
$headers = new Headers();
|
||||||
$headers->addPathHeader('Return-Path', 'return@symfony.com');
|
$headers->addPathHeader('Return-Path', 'return@symfony.com');
|
||||||
@ -88,6 +99,6 @@ class SmtpEnvelopeTest extends TestCase
|
|||||||
$headers->addMailboxListHeader('Cc', [new NamedAddress('cc@symfony.com', 'cc')]);
|
$headers->addMailboxListHeader('Cc', [new NamedAddress('cc@symfony.com', 'cc')]);
|
||||||
$headers->addMailboxListHeader('Bcc', [new NamedAddress('bcc@symfony.com', 'bcc')]);
|
$headers->addMailboxListHeader('Bcc', [new NamedAddress('bcc@symfony.com', 'bcc')]);
|
||||||
$e = SmtpEnvelope::create(new Message($headers));
|
$e = SmtpEnvelope::create(new Message($headers));
|
||||||
$this->assertEquals([new Address('to@symfony.com'), new Address('cc@symfony.com'), new Address('bcc@symfony.com')], $e->getRecipients());
|
$this->assertEquals([new NamedAddress('to@symfony.com', 'to'), new NamedAddress('cc@symfony.com', 'cc'), new NamedAddress('bcc@symfony.com', 'bcc')], $e->getRecipients());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,9 +152,9 @@ class SmtpTransport extends AbstractTransport
|
|||||||
protected function doSend(SentMessage $message): void
|
protected function doSend(SentMessage $message): void
|
||||||
{
|
{
|
||||||
$envelope = $message->getEnvelope();
|
$envelope = $message->getEnvelope();
|
||||||
$this->doMailFromCommand($envelope->getSender()->toString());
|
$this->doMailFromCommand($envelope->getSender()->getAddress());
|
||||||
foreach ($envelope->getRecipients() as $recipient) {
|
foreach ($envelope->getRecipients() as $recipient) {
|
||||||
$this->doRcptToCommand($recipient->toString());
|
$this->doRcptToCommand($recipient->getAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->executeCommand("DATA\r\n", [354]);
|
$this->executeCommand("DATA\r\n", [354]);
|
||||||
@ -170,12 +170,12 @@ class SmtpTransport extends AbstractTransport
|
|||||||
$this->executeCommand(sprintf("HELO %s\r\n", $this->domain), [250]);
|
$this->executeCommand(sprintf("HELO %s\r\n", $this->domain), [250]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function doMailFromCommand($address): void
|
private function doMailFromCommand(string $address): void
|
||||||
{
|
{
|
||||||
$this->executeCommand(sprintf("MAIL FROM:<%s>\r\n", $address), [250]);
|
$this->executeCommand(sprintf("MAIL FROM:<%s>\r\n", $address), [250]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function doRcptToCommand($address): void
|
private function doRcptToCommand(string $address): void
|
||||||
{
|
{
|
||||||
$this->executeCommand(sprintf("RCPT TO:<%s>\r\n", $address), [250, 251, 252]);
|
$this->executeCommand(sprintf("RCPT TO:<%s>\r\n", $address), [250, 251, 252]);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user