zbateson / mail-mime-parser

An email parser written in PHP
https://mail-mime-parser.org/
BSD 2-Clause "Simplified" License
459 stars 58 forks source link

__toString Messing Up SMTP From Header #243

Open mwyres opened 2 months ago

mwyres commented 2 months ago

Weird modification to the "From" line:

$content = "From user@example.com   ".@date("D M d H:i:s Y")."\n";
$content .= stream_get_content(STDIN);
$parser = new MailMimeParser();
$message = $parser->parse($content,FALSE);

<perform actions on $message>

$result .= $message->__toString();

The resulting "From" line in $result comes back with weird additional spaces in the date - (note the weird space in between hours and minutes in the formatted date string?)

From user@example.com  Wed Sep 04 01: 31:55 2024 

Workaround is not to pass the content with the "From" line through the __toString() function, that is:

$content = stream_get_content(STDIN);
$parser = new MailMimeParser();
$message = $parser->parse($content,FALSE);

<perform actions on $message>

$output = $message->__toString();
$result = "From user@example.com   ".@date("D M d H:i:s Y")."\n";
$result .= $output;

This gives the "From" line correctly, without the weird space:

From user@example.com  Wed Sep 04 01:31:55 2024 

Given there is a workaround, this is not a critical issue, but somewhere between the creation of MimeMailParser object, and the output using __toString, the "From" line is being handled weirdly.

Note, this is not the "From:" header from further down - this is the very first line that appears at the top of emails after processing via Postfix, and needs to exist if you are post-processing back through Postfix.

Happy to explain further if this isn't completely clear.

zbateson commented 2 months ago

This library only parses mime messages, the first "SMTP" line shouldn't be passed to $parser->parse in your example, it should be filtered out.

mwyres commented 1 month ago

As in it is treating that first line as:

From user@example.com Wed Sep 04 01:

...as the header name, adding the space, and then:

31:55 2024

...as the value of the header, given it's after the first colon.

Make sense - relatively easy to workaround, but would be a "nice to have" if it could work around it.