Skip to content

[Mime] Remove NamedAddress #33270

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

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ Messenger
* Deprecated passing a `ContainerInterface` instance as first argument of the `ConsumeMessagesCommand` constructor,
pass a `RoutableMessageBus` instance instead.

Mime
----

* Removed `NamedAddress`, use `Address` instead (which supports a name now)

MonologBridge
--------------

Expand Down
29 changes: 13 additions & 16 deletions src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Bridge\Twig\Mime;

use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\NamedAddress;
use Twig\Environment;

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

public function toName(): string
{
$to = $this->message->getTo()[0];

return $to instanceof NamedAddress ? $to->getName() : '';
return $this->message->getTo()[0]->getName();
}

public function image(string $image, string $contentType = null): string
Expand Down Expand Up @@ -93,15 +90,15 @@ public function getReturnPath(): string
/**
* @return $this
*/
public function addFrom(string $address, string $name = null): self
public function addFrom(string $address, string $name = ''): self
Copy link
Member

Choose a reason for hiding this comment

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

this should be documented in the upgrade guide too, as the parameter type changed from string|null to string, which is also a BC break.

Copy link
Member Author

Choose a reason for hiding this comment

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

The parameter type was string and null was only to express that no arguments was passed. Anyway, I think that's minor and we should not waste our time on such things.

{
$this->message->addFrom($name ? new NamedAddress($address, $name) : new Address($address));
$this->message->addFrom(new Address($address, $name));

return $this;
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getFrom(): array
{
Expand Down Expand Up @@ -129,15 +126,15 @@ public function getReplyTo(): array
/**
* @return $this
*/
public function addTo(string $address, string $name = null): self
public function addTo(string $address, string $name = ''): self
{
$this->message->addTo($name ? new NamedAddress($address, $name) : new Address($address));
$this->message->addTo(new Address($address, $name));

return $this;
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getTo(): array
{
Expand All @@ -147,15 +144,15 @@ public function getTo(): array
/**
* @return $this
*/
public function addCc(string $address, string $name = null): self
public function addCc(string $address, string $name = ''): self
{
$this->message->addCc($name ? new NamedAddress($address, $name) : new Address($address));
$this->message->addCc(new Address($address, $name));

return $this;
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getCc(): array
{
Expand All @@ -165,15 +162,15 @@ public function getCc(): array
/**
* @return $this
*/
public function addBcc(string $address, string $name = null): self
public function addBcc(string $address, string $name = ''): self
{
$this->message->addBcc($name ? new NamedAddress($address, $name) : new Address($address));
$this->message->addBcc(new Address($address, $name));

return $this;
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getBcc(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\NamedAddress;

class EmailController
{
Expand All @@ -29,7 +29,7 @@ public function indexAction(MailerInterface $mailer)
);

$mailer->send((new Email())->to('fabien@symfony.com', 'thomas@symfony.com')->from('fabien@symfony.com')->subject('Foo')
->addReplyTo(new NamedAddress('me@symfony.com', 'Fabien Potencier'))
->addReplyTo(new Address('me@symfony.com', 'Fabien Potencier'))
->addCc('cc@symfony.com')
->text('Bar!')
->html('<p>Foo</p>')
Expand Down
19 changes: 9 additions & 10 deletions src/Symfony/Component/Mailer/Tests/SmtpEnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\NamedAddress;

class SmtpEnvelopeTest extends TestCase
{
Expand All @@ -28,13 +27,13 @@ public function testConstructorWithAddressSender()

public function testConstructorWithNamedAddressSender()
{
$e = new SmtpEnvelope(new NamedAddress('fabien@symfony.com', 'Fabien'), [new Address('thomas@symfony.com')]);
$e = new SmtpEnvelope(new Address('fabien@symfony.com', 'Fabien'), [new Address('thomas@symfony.com')]);
$this->assertEquals(new Address('fabien@symfony.com'), $e->getSender());
}

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

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

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

$headers = new Headers();
$headers->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from'), 'some@symfony.com']);
$headers->addMailboxListHeader('From', [new Address('from@symfony.com', 'from'), 'some@symfony.com']);
$headers->addMailboxListHeader('To', ['from@symfony.com']);
$e = SmtpEnvelope::create(new Message($headers));
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
Expand All @@ -76,17 +75,17 @@ public function testSenderFromHeadersWithoutFrom()
$headers = new Headers();
$headers->addMailboxListHeader('To', ['from@symfony.com']);
$e = SmtpEnvelope::create($message = new Message($headers));
$message->getHeaders()->addMailboxListHeader('From', [new NamedAddress('from@symfony.com', 'from')]);
$message->getHeaders()->addMailboxListHeader('From', [new Address('from@symfony.com', 'from')]);
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
}

public function testRecipientsFromHeaders()
{
$headers = new Headers();
$headers->addPathHeader('Return-Path', 'return@symfony.com');
$headers->addMailboxListHeader('To', [new NamedAddress('to@symfony.com', 'to')]);
$headers->addMailboxListHeader('Cc', [new NamedAddress('cc@symfony.com', 'cc')]);
$headers->addMailboxListHeader('Bcc', [new NamedAddress('bcc@symfony.com', 'bcc')]);
$headers->addMailboxListHeader('To', [new Address('to@symfony.com', 'to')]);
$headers->addMailboxListHeader('Cc', [new Address('cc@symfony.com', 'cc')]);
$headers->addMailboxListHeader('Bcc', [new Address('bcc@symfony.com', 'bcc')]);
$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());
}
Expand Down
13 changes: 10 additions & 3 deletions src/Symfony/Component/Mime/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Address
final class Address
Copy link
Member

Choose a reason for hiding this comment

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

this needs to be documented in the upgrade guide too, as it is a BC break.

{
private static $validator;
private static $encoder;

private $address;
private $name;

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

$this->address = $address;
$this->name = $name;
}

public function getAddress(): string
{
return $this->address;
}

public function getName(): string
{
return $this->name;
}

public function getEncodedAddress(): string
{
if (null === self::$encoder) {
Expand All @@ -61,7 +68,7 @@ public function getEncodedAddress(): string

public function toString(): string
{
return $this->getEncodedAddress();
return ($n = $this->getName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
Copy link
Member

Choose a reason for hiding this comment

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

use $this->name directly rather than adding another variable.

}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Mime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

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

Expand Down
28 changes: 14 additions & 14 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getSender(): ?Address
}

/**
* @param Address|NamedAddress|string ...$addresses
* @param Address|string ...$addresses
*
* @return $this
*/
Expand All @@ -111,7 +111,7 @@ public function addFrom(...$addresses)
}

/**
* @param Address|NamedAddress|string ...$addresses
* @param Address|string ...$addresses
*
* @return $this
*/
Expand All @@ -121,7 +121,7 @@ public function from(...$addresses)
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getFrom(): array
{
Expand Down Expand Up @@ -157,7 +157,7 @@ public function getReplyTo(): array
}

/**
* @param Address|NamedAddress|string ...$addresses
* @param Address|string ...$addresses
*
* @return $this
*/
Expand All @@ -167,7 +167,7 @@ public function addTo(...$addresses)
}

/**
* @param Address|NamedAddress|string ...$addresses
* @param Address|string ...$addresses
*
* @return $this
*/
Expand All @@ -177,15 +177,15 @@ public function to(...$addresses)
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getTo(): array
{
return $this->getHeaders()->getHeaderBody('To') ?: [];
}

/**
* @param Address|NamedAddress|string ...$addresses
* @param Address|string ...$addresses
*
* @return $this
*/
Expand All @@ -205,15 +205,15 @@ public function cc(...$addresses)
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getCc(): array
{
return $this->getHeaders()->getHeaderBody('Cc') ?: [];
}

/**
* @param Address|NamedAddress|string ...$addresses
* @param Address|string ...$addresses
*
* @return $this
*/
Expand All @@ -233,7 +233,7 @@ public function bcc(...$addresses)
}

/**
* @return (Address|NamedAddress)[]
* @return Address[]
*/
public function getBcc(): array
{
Expand Down Expand Up @@ -524,10 +524,10 @@ private function setHeaderBody(string $type, string $name, $body)

private function addListAddressHeaderBody(string $name, array $addresses)
{
if (!$to = $this->getHeaders()->get($name)) {
if (!$header = $this->getHeaders()->get($name)) {
return $this->setListAddressHeaderBody($name, $addresses);
}
$to->addAddresses(Address::createArray($addresses));
$header->addAddresses(Address::createArray($addresses));

return $this;
}
Expand All @@ -536,8 +536,8 @@ private function setListAddressHeaderBody(string $name, array $addresses)
{
$addresses = Address::createArray($addresses);
$headers = $this->getHeaders();
if ($to = $headers->get($name)) {
$to->setAddresses($addresses);
if ($header = $headers->get($name)) {
$header->setAddresses($addresses);
} else {
$headers->addMailboxListHeader($name, $addresses);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Mime/Header/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\NamedAddress;

/**
* A collection of headers.
Expand Down Expand Up @@ -60,7 +59,7 @@ public function getMaxLineLength(): int
}

/**
* @param (NamedAddress|Address|string)[] $addresses
* @param (Address|string)[] $addresses
*
* @return $this
*/
Expand All @@ -70,7 +69,7 @@ public function addMailboxListHeader(string $name, array $addresses): self
}

/**
* @param NamedAddress|Address|string $address
* @param Address|string $address
*
* @return $this
*/
Expand Down
Loading