Skip to content
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

Remove header name from ->getHeader() #123

Closed
ThomasLandauer opened this issue Aug 4, 2020 · 8 comments
Closed

Remove header name from ->getHeader() #123

ThomasLandauer opened this issue Aug 4, 2020 · 8 comments

Comments

@ThomasLandauer
Copy link
Contributor

Is there a limit for issues per day? ;-)

I think that the main function for reading a header (i.e. ->getHeader()) should return just the value/content, not the header name:

If this is in the email

->getHeader('from') now returns (see AbstractHeader::__toString()):

but should return:

Why?

  • Semantics: The string From: is not part of the information
  • Pragmatics: If I ask for the From header, there's no need to prefix the answer with something like "Here comes the From header". And if I store it in a database (in a column probably called from), there's no need to store "From:" over and over.

I think it's a good idea to do have a function that returns the full line, it just shouldn't be the "main" function. Maybe something like getFullHeader()? Speaking of naming: What was your idea behind ->getHeaderValue()? For e.g. the Subject this would be what I'm asking for here, but for From it just returns the email address, not the person's name.

And I spotted a (somewhat related) inconsistency in README.md: getPersonName() vs. getName():

echo $message
    ->getHeader('from')                         // AddressHeader
    ->getPersonName();                          // Person Name
echo $message
    ->getHeader('to')                           // also AddressHeader
    ->getAddresses()[0]                         // AddressPart
    ->getName();                                // Person Name

The former is a method of AddressHeader. But since the latter is a method of the more generic ParameterPart, you probably cannot change it in the code?

@zbateson
Copy link
Owner

zbateson commented Aug 4, 2020

I disagree with this one, although there may be less use for a user looking at the entire header, the object itself doesn't care that you called getHeader('from'), and in this case the __toString is telling you about the whole object you're looking at... essentially "this is the String representation of this object"... i.e. __toString.

It's also used when writing out an email, essentially the "string" representation of a Message is all the headers and body... I don't assume that because you're looking at a Message you know that the Subject is X, that you're only interested in the text content, etc..., etc..., I'm assuming __toString should give the "string" representation of whatever object it is you're looking at.

This may not be consistent in every project, etc... but I think it makes the most sense. If you only want a specific part of it, you'd call $message->getTextContent or $header->getSomethingSpecific, etc....

@ThomasLandauer
Copy link
Contributor Author

I see what you mean by "string representation of the object". However, if I have a Person entity, my __toString() looks like this:

return $this->getFirstName() . $this->getLastName();

not like this:

return 'Person: ' . $this->getFirstName() . $this->getLastName();

you'd call $header->getSomethingSpecific, etc....

But there just is no function that gives me the entire header without the name-prefix! (That's what I'm saying) Or is it?

@zbateson
Copy link
Owner

zbateson commented Aug 4, 2020

However, if I have a Person entity, my __toString() looks like this

Yes, but this is not an "Address" entity, it's an "AddressHeader" entity, this makes sense to me. You can echo $message; and get the whole message (which internally calls __toString() on each header, so you can change headers and the whole message will get written out cleanly for example), and it's also not returning "Address: user [email protected]" it's returning "From: user [email protected]", From being the name of the AddressHeader itself.

It certainly wouldn't make sense to put "Person: " in front of some random Person object's "__toString", but if it were PersonHeader as part of a mime object, the name of which is "Person", then I think it would 😄

In another ticket I proposed maybe $header->getDisplayValue that could be written to do something like that potentially?

@ThomasLandauer
Copy link
Contributor Author

You can echo $message; and get the whole message (which internally calls __toString() on each header, so you can change headers and the whole message will get written out cleanly for example)

Since you mentioned this repeatedly: Is there a use-case for this? I mean, this is an email parser, not an email creator. So if I want to see the entire message, I can take a look at what I passed into $mailParser->parse(); :-)

@zbateson
Copy link
Owner

Yeah, you can modify a parsed email (add attachments/parts, etc..., change content, whatever you want) and do what you will with it after.

It's a bit simplistic in that you have to do all the header formatting and such yourself, but yes, it's used.

I don't think anyone's using this to create a fresh mime message though...

@ThomasLandauer
Copy link
Contributor Author

and do what you will with it after.

But you can't send it? Or can you?

@zbateson
Copy link
Owner

There's nothing in zbateson/mail-mime-parser to help with sending emails.

@ThomasLandauer
Copy link
Contributor Author

I still think that the Headers main string representation (i.e. __toString()) should not include the header name. Your argument

You can echo $message; and get the whole message (which internally calls __toString() on each header

...doesn't convince me since the very sense of an email parser is to look at single parts/headers, not at the entire message. However, I'm closing this in favor of #122, since changing the current behavior only makes sense as part of a larger overhaul of the nomenclature, which we're discussing there.

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

No branches or pull requests

2 participants