Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

AddressList does not parse address with parens in name field properly #148

Merged

Conversation

glensc
Copy link
Contributor

@glensc glensc commented May 20, 2017

"Supports (E-mail)" <support@example.org>

this gets parsed as Support, not as expected Supports (E-mail)

@glensc
Copy link
Contributor Author

glensc commented May 20, 2017

broken by #12, but it was likely even more broken before that PR.

@glensc
Copy link
Contributor Author

glensc commented May 20, 2017

i think Zend\Mail\Address should have $this->comment property to handle this properly.
currently the toString makes information being lost, because it doesn't quote at all!

consider such code:

        $name = "Supports (E-mail)";
        $email = 'support@example.org';
        $address = new \Zend\Mail\Address($email, $name);
        // prints: Supports (E-mail) <support@example.org>
        echo $address->toString(), "\n";

        $to = Header\To::fromString('To:' . $address->toString());
        // prints: To: Supports <support@example.org>
        echo $to->toString(), "\n";

@weierophinney weierophinney force-pushed the address-parser-loses-comments branch from 9d5d8bb to 4d7550f Compare June 6, 2018 18:32
@weierophinney
Copy link
Member

In looking into this, I found this line:

We're currently explicitly stripping comments from names.

As such, I think you're correct: we should aggregate the comment separately, and then have toString() include any comment found. I'll see if I can create a patch.

This patch accomplishes several things:

- `Address` adds a method, `getComment()`, mapping to a new, optional
  constructor value `$comment`. When present, the generated address will
  include the comment: `Name (Comment) <Email>`.
- It moves the logic for parsing an address value in order to create an
  `Address` to the `Address` class itself.
- `addFromString()` (and the new `Address::fromString()`) now accepts an
  optional `$comment = null` value; when present, the value is passed to
  the `Address` constructor during instantiation.
- `AbstractAddressList` adds a new method, `getComments()`.
  `fromString()` was updated to replace its `array_walk()` with an
  `array_map()` that calls this method, and uses the value to pass to
  `Address::fromString()` (along with the filtered `$value`). This
  ensures we can aggregate the comments, if any, with the address they
  belong to.
@weierophinney weierophinney merged commit a1dc7c9 into zendframework:master Jun 6, 2018
weierophinney added a commit that referenced this pull request Jun 6, 2018
weierophinney added a commit that referenced this pull request Jun 6, 2018
@glensc glensc deleted the address-parser-loses-comments branch June 6, 2018 21:05

$name = null;
if (isset($matches['name'])) {
$name = trim($matches['name']);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

you could write as: $name = trim($matches['name']) ?: null and drop next if block ;)

Copy link
Member

Choose a reason for hiding this comment

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

We'll get an error if $matches['name'] is not set, however. (Tried it previously which is why I went with this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nah, $name is assigned null on line 37 before the isset block.

$address = $addressList->get('support@example.org');
$this->assertEquals('Supports', $address->getName());
$this->assertEquals('E-mail', $address->getComment());
$this->assertEquals('support@example.org', $address->getEmail());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not testing the toString?

Copy link
Member

Choose a reason for hiding this comment

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

Probably should! I was primarily ensuring that the various accessors returned what was expected.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants