Skip to content

Commit b36961c

Browse files
committed
feature #33270 [Mime] Remove NamedAddress (fabpot)
This PR was merged into the 4.4 branch. Discussion ---------- [Mime] Remove NamedAddress | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | yes <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a | License | MIT | Doc PR | n/a While working on #33091, we realize that having both `NamedAddress` and `Address` brings some unneeded complexity. Initially, I added both as some headers do not support `NamedAddress`es. But the code already does the right thing by "downgrading" them to `Address` instances. So, let's simplify our internal code and make life easier for our users too. Commits ------- eb7d74e [Mime] Remove NamedAddress
2 parents 5a753b1 + eb7d74e commit b36961c

File tree

17 files changed

+96
-158
lines changed

17 files changed

+96
-158
lines changed

UPGRADE-4.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ Messenger
157157
* Deprecated passing a `ContainerInterface` instance as first argument of the `ConsumeMessagesCommand` constructor,
158158
pass a `RoutableMessageBus` instance instead.
159159

160+
Mime
161+
----
162+
163+
* Removed `NamedAddress`, use `Address` instead (which supports a name now)
164+
160165
MonologBridge
161166
--------------
162167

src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bridge\Twig\Mime;
1313

1414
use Symfony\Component\Mime\Address;
15-
use Symfony\Component\Mime\NamedAddress;
1615
use Twig\Environment;
1716

1817
/**
@@ -33,9 +32,7 @@ public function __construct(Environment $twig, TemplatedEmail $message)
3332

3433
public function toName(): string
3534
{
36-
$to = $this->message->getTo()[0];
37-
38-
return $to instanceof NamedAddress ? $to->getName() : '';
35+
return $this->message->getTo()[0]->getName();
3936
}
4037

4138
public function image(string $image, string $contentType = null): string
@@ -93,15 +90,15 @@ public function getReturnPath(): string
9390
/**
9491
* @return $this
9592
*/
96-
public function addFrom(string $address, string $name = null): self
93+
public function addFrom(string $address, string $name = ''): self
9794
{
98-
$this->message->addFrom($name ? new NamedAddress($address, $name) : new Address($address));
95+
$this->message->addFrom(new Address($address, $name));
9996

10097
return $this;
10198
}
10299

103100
/**
104-
* @return (Address|NamedAddress)[]
101+
* @return Address[]
105102
*/
106103
public function getFrom(): array
107104
{
@@ -129,15 +126,15 @@ public function getReplyTo(): array
129126
/**
130127
* @return $this
131128
*/
132-
public function addTo(string $address, string $name = null): self
129+
public function addTo(string $address, string $name = ''): self
133130
{
134-
$this->message->addTo($name ? new NamedAddress($address, $name) : new Address($address));
131+
$this->message->addTo(new Address($address, $name));
135132

136133
return $this;
137134
}
138135

139136
/**
140-
* @return (Address|NamedAddress)[]
137+
* @return Address[]
141138
*/
142139
public function getTo(): array
143140
{
@@ -147,15 +144,15 @@ public function getTo(): array
147144
/**
148145
* @return $this
149146
*/
150-
public function addCc(string $address, string $name = null): self
147+
public function addCc(string $address, string $name = ''): self
151148
{
152-
$this->message->addCc($name ? new NamedAddress($address, $name) : new Address($address));
149+
$this->message->addCc(new Address($address, $name));
153150

154151
return $this;
155152
}
156153

157154
/**
158-
* @return (Address|NamedAddress)[]
155+
* @return Address[]
159156
*/
160157
public function getCc(): array
161158
{
@@ -165,15 +162,15 @@ public function getCc(): array
165162
/**
166163
* @return $this
167164
*/
168-
public function addBcc(string $address, string $name = null): self
165+
public function addBcc(string $address, string $name = ''): self
169166
{
170-
$this->message->addBcc($name ? new NamedAddress($address, $name) : new Address($address));
167+
$this->message->addBcc(new Address($address, $name));
171168

172169
return $this;
173170
}
174171

175172
/**
176-
* @return (Address|NamedAddress)[]
173+
* @return Address[]
177174
*/
178175
public function getBcc(): array
179176
{

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\Mailer\MailerInterface;
16+
use Symfony\Component\Mime\Address;
1617
use Symfony\Component\Mime\Email;
17-
use Symfony\Component\Mime\NamedAddress;
1818

1919
class EmailController
2020
{
@@ -29,7 +29,7 @@ public function indexAction(MailerInterface $mailer)
2929
);
3030

3131
$mailer->send((new Email())->to('fabien@symfony.com', 'thomas@symfony.com')->from('fabien@symfony.com')->subject('Foo')
32-
->addReplyTo(new NamedAddress('me@symfony.com', 'Fabien Potencier'))
32+
->addReplyTo(new Address('me@symfony.com', 'Fabien Potencier'))
3333
->addCc('cc@symfony.com')
3434
->text('Bar!')
3535
->html('<p>Foo</p>')

src/Symfony/Component/Mailer/Tests/SmtpEnvelopeTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Mime\Address;
1717
use Symfony\Component\Mime\Header\Headers;
1818
use Symfony\Component\Mime\Message;
19-
use Symfony\Component\Mime\NamedAddress;
2019

2120
class SmtpEnvelopeTest extends TestCase
2221
{
@@ -28,13 +27,13 @@ public function testConstructorWithAddressSender()
2827

2928
public function testConstructorWithNamedAddressSender()
3029
{
31-
$e = new SmtpEnvelope(new NamedAddress('fabien@symfony.com', 'Fabien'), [new Address('thomas@symfony.com')]);
30+
$e = new SmtpEnvelope(new Address('fabien@symfony.com', 'Fabien'), [new Address('thomas@symfony.com')]);
3231
$this->assertEquals(new Address('fabien@symfony.com'), $e->getSender());
3332
}
3433

3534
public function testConstructorWithAddressRecipients()
3635
{
37-
$e = new SmtpEnvelope(new Address('fabien@symfony.com'), [new Address('thomas@symfony.com'), new NamedAddress('lucas@symfony.com', 'Lucas')]);
36+
$e = new SmtpEnvelope(new Address('fabien@symfony.com'), [new Address('thomas@symfony.com'), new Address('lucas@symfony.com', 'Lucas')]);
3837
$this->assertEquals([new Address('thomas@symfony.com'), new Address('lucas@symfony.com')], $e->getRecipients());
3938
}
4039

@@ -53,19 +52,19 @@ public function testConstructorWithWrongRecipients()
5352
public function testSenderFromHeaders()
5453
{
5554
$headers = new Headers();
56-
$headers->addPathHeader('Return-Path', new NamedAddress('return@symfony.com', 'return'));
55+
$headers->addPathHeader('Return-Path', new Address('return@symfony.com', 'return'));
5756
$headers->addMailboxListHeader('To', ['from@symfony.com']);
5857
$e = SmtpEnvelope::create(new Message($headers));
5958
$this->assertEquals('return@symfony.com', $e->getSender()->getAddress());
6059

6160
$headers = new Headers();
62-
$headers->addMailboxHeader('Sender', new NamedAddress('sender@symfony.com', 'sender'));
61+
$headers->addMailboxHeader('Sender', new Address('sender@symfony.com', 'sender'));
6362
$headers->addMailboxListHeader('To', ['from@symfony.com']);
6463
$e = SmtpEnvelope::create(new Message($headers));
6564
$this->assertEquals('sender@symfony.com', $e->getSender()->getAddress());
6665

6766
$headers = new Headers();
68-
$headers->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from'), 'some@symfony.com']);
67+
$headers->addMailboxListHeader('From', [new Address('from@symfony.com', 'from'), 'some@symfony.com']);
6968
$headers->addMailboxListHeader('To', ['from@symfony.com']);
7069
$e = SmtpEnvelope::create(new Message($headers));
7170
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
@@ -76,17 +75,17 @@ public function testSenderFromHeadersWithoutFrom()
7675
$headers = new Headers();
7776
$headers->addMailboxListHeader('To', ['from@symfony.com']);
7877
$e = SmtpEnvelope::create($message = new Message($headers));
79-
$message->getHeaders()->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from')]);
78+
$message->getHeaders()->addMailboxListHeader('From', [new Address('from@symfony.com', 'from')]);
8079
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
8180
}
8281

8382
public function testRecipientsFromHeaders()
8483
{
8584
$headers = new Headers();
8685
$headers->addPathHeader('Return-Path', 'return@symfony.com');
87-
$headers->addMailboxListHeader('To', [new NamedAddress('to@symfony.com', 'to')]);
88-
$headers->addMailboxListHeader('Cc', [new NamedAddress('cc@symfony.com', 'cc')]);
89-
$headers->addMailboxListHeader('Bcc', [new NamedAddress('bcc@symfony.com', 'bcc')]);
86+
$headers->addMailboxListHeader('To', [new Address('to@symfony.com', 'to')]);
87+
$headers->addMailboxListHeader('Cc', [new Address('cc@symfony.com', 'cc')]);
88+
$headers->addMailboxListHeader('Bcc', [new Address('bcc@symfony.com', 'bcc')]);
9089
$e = SmtpEnvelope::create(new Message($headers));
9190
$this->assertEquals([new Address('to@symfony.com'), new Address('cc@symfony.com'), new Address('bcc@symfony.com')], $e->getRecipients());
9291
}

src/Symfony/Component/Mime/Address.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
/**
2222
* @author Fabien Potencier <fabien@symfony.com>
2323
*/
24-
class Address
24+
final class Address
2525
{
2626
private static $validator;
2727
private static $encoder;
2828

2929
private $address;
30+
private $name;
3031

31-
public function __construct(string $address)
32+
public function __construct(string $address, string $name = '')
3233
{
3334
if (!class_exists(EmailValidator::class)) {
3435
throw new LogicException(sprintf('The "%s" class cannot be used as it needs "%s"; try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class));
@@ -43,13 +44,19 @@ public function __construct(string $address)
4344
}
4445

4546
$this->address = $address;
47+
$this->name = $name;
4648
}
4749

4850
public function getAddress(): string
4951
{
5052
return $this->address;
5153
}
5254

55+
public function getName(): string
56+
{
57+
return $this->name;
58+
}
59+
5360
public function getEncodedAddress(): string
5461
{
5562
if (null === self::$encoder) {
@@ -61,7 +68,7 @@ public function getEncodedAddress(): string
6168

6269
public function toString(): string
6370
{
64-
return $this->getEncodedAddress();
71+
return ($n = $this->getName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
6572
}
6673

6774
/**

src/Symfony/Component/Mime/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* [BC BREAK] Removed `NamedAddress` (`Address` now supports a name)
78
* Added PHPUnit constraints
89
* Added `AbstractPart::asDebugString()`
910

src/Symfony/Component/Mime/Email.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function getSender(): ?Address
101101
}
102102

103103
/**
104-
* @param Address|NamedAddress|string ...$addresses
104+
* @param Address|string ...$addresses
105105
*
106106
* @return $this
107107
*/
@@ -111,7 +111,7 @@ public function addFrom(...$addresses)
111111
}
112112

113113
/**
114-
* @param Address|NamedAddress|string ...$addresses
114+
* @param Address|string ...$addresses
115115
*
116116
* @return $this
117117
*/
@@ -121,7 +121,7 @@ public function from(...$addresses)
121121
}
122122

123123
/**
124-
* @return (Address|NamedAddress)[]
124+
* @return Address[]
125125
*/
126126
public function getFrom(): array
127127
{
@@ -157,7 +157,7 @@ public function getReplyTo(): array
157157
}
158158

159159
/**
160-
* @param Address|NamedAddress|string ...$addresses
160+
* @param Address|string ...$addresses
161161
*
162162
* @return $this
163163
*/
@@ -167,7 +167,7 @@ public function addTo(...$addresses)
167167
}
168168

169169
/**
170-
* @param Address|NamedAddress|string ...$addresses
170+
* @param Address|string ...$addresses
171171
*
172172
* @return $this
173173
*/
@@ -177,15 +177,15 @@ public function to(...$addresses)
177177
}
178178

179179
/**
180-
* @return (Address|NamedAddress)[]
180+
* @return Address[]
181181
*/
182182
public function getTo(): array
183183
{
184184
return $this->getHeaders()->getHeaderBody('To') ?: [];
185185
}
186186

187187
/**
188-
* @param Address|NamedAddress|string ...$addresses
188+
* @param Address|string ...$addresses
189189
*
190190
* @return $this
191191
*/
@@ -205,15 +205,15 @@ public function cc(...$addresses)
205205
}
206206

207207
/**
208-
* @return (Address|NamedAddress)[]
208+
* @return Address[]
209209
*/
210210
public function getCc(): array
211211
{
212212
return $this->getHeaders()->getHeaderBody('Cc') ?: [];
213213
}
214214

215215
/**
216-
* @param Address|NamedAddress|string ...$addresses
216+
* @param Address|string ...$addresses
217217
*
218218
* @return $this
219219
*/
@@ -233,7 +233,7 @@ public function bcc(...$addresses)
233233
}
234234

235235
/**
236-
* @return (Address|NamedAddress)[]
236+
* @return Address[]
237237
*/
238238
public function getBcc(): array
239239
{
@@ -524,10 +524,10 @@ private function setHeaderBody(string $type, string $name, $body)
524524

525525
private function addListAddressHeaderBody(string $name, array $addresses)
526526
{
527-
if (!$to = $this->getHeaders()->get($name)) {
527+
if (!$header = $this->getHeaders()->get($name)) {
528528
return $this->setListAddressHeaderBody($name, $addresses);
529529
}
530-
$to->addAddresses(Address::createArray($addresses));
530+
$header->addAddresses(Address::createArray($addresses));
531531

532532
return $this;
533533
}
@@ -536,8 +536,8 @@ private function setListAddressHeaderBody(string $name, array $addresses)
536536
{
537537
$addresses = Address::createArray($addresses);
538538
$headers = $this->getHeaders();
539-
if ($to = $headers->get($name)) {
540-
$to->setAddresses($addresses);
539+
if ($header = $headers->get($name)) {
540+
$header->setAddresses($addresses);
541541
} else {
542542
$headers->addMailboxListHeader($name, $addresses);
543543
}

src/Symfony/Component/Mime/Header/Headers.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Mime\Address;
1515
use Symfony\Component\Mime\Exception\LogicException;
16-
use Symfony\Component\Mime\NamedAddress;
1716

1817
/**
1918
* A collection of headers.
@@ -60,7 +59,7 @@ public function getMaxLineLength(): int
6059
}
6160

6261
/**
63-
* @param (NamedAddress|Address|string)[] $addresses
62+
* @param (Address|string)[] $addresses
6463
*
6564
* @return $this
6665
*/
@@ -70,7 +69,7 @@ public function addMailboxListHeader(string $name, array $addresses): self
7069
}
7170

7271
/**
73-
* @param NamedAddress|Address|string $address
72+
* @param Address|string $address
7473
*
7574
* @return $this
7675
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy