zbateson / mail-mime-parser

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

Not parsing every part of received header - skips the first recevied part #167

Closed sirkokoenig closed 3 years ago

sirkokoenig commented 3 years ago

Hello,

I´m new in working with mail-mime-parser and got the main things working. My main purpose of using mail-mime-parser is to fastly show the sender of the e-mail.

I have the following example: Received: from reley.testserver.de ([109.237.142.xxx]) by mx.emig.restersdasd.de (mxeue110 [217.72.192.xx]) with ESMTPS (Nemesis) id 1MRlbo-1lDIxQ07UA-00TPTv for max.mustermann@online.de; Wed, 24 Mar 2021 08:21:34 +0100 Received: by relay01.atest-serverr.de (Postfix, from userid 1001) id 3497F32C1B61; Wed, 24 Mar 2021 08:21:34 +0100 (CET) Received: from alfa3063.testserver.de (alfa3063.testserver.de [109.237.140.xx]) by relay01.testserver.de (Postfix) with ESMTPS id 2DB8032C1B61 for max.mustertmann@online.de; Wed, 24 Mar 2021 08:21:34 +0100 (CET) Received: from [192.168.178.xx] (dslb-088-073-211-233.088.073.pools.telecomunication.de [88.73.211.xxx]) by alfa3063.testserver.de (Postfix) with ESMTPSA id 8710548F4B7A for max.mustermann@online.de; Wed, 24 Mar 2021 08:21:33 +0100 (CET)

When I use $message->getHeader('Received'); I don´t get the last Received part with the most important lines (with the private ip-address). When I use var_dump there are only the other 3 Received parts. I can´t find the problem, maybe it is a bug?

zbateson commented 3 years ago

Hi @sirkokoenig --

Were you able to figure this out? Is it possible something about the headers is formatted incorrectly or have you figured out if it's a bug in the parser?

I copied over your headers to a test email and was able to read all of them:

public function testParseEmailGitHub_167()
{
    $handle = fopen($this->messageDir . '/github-167.txt', 'r');
    $message = $this->parser->parse($handle);
    fclose($handle);
    var_dump($message->getHeader('Received', 3)->getValue());
    var_dump(count($message->getAllHeadersByName('Received')));
}

Outputs:

string(231) "from [192.168.178.xx] (dslb-088-073-211-233.088.073.pools.telecomunication.de [88.73.211.xxx])
 by alfa3063.testserver.de (Postfix) with ESMTPSA id 8710548F4B7A
 for max.mustermann@online.de; Wed, 24 Mar 2021 08:21:33 +0100 (CET)"
int(4)
sirkokoenig commented 3 years ago

Hi, thanks for that hint. I used only

$receivedheader = $message->getHeader('Received');

if (ISSET($receivedheader))
{
    $ip = $receivedheader->getFromAddress();
    $ipname = $receivedheader->getFromName();

and then I got only the second line from the end, so it skipped a one. Now I make it like

$countHeaders = count($message->getAllHeadersByName('Received'));
$receivedheader = $message->getHeader('Received', $countHeaders - 1);

if (ISSET($receivedheader))
{
    $ip = $receivedheader->getFromAddress();
    $ipname = $receivedheader->getFromName();

Thank you very much for fast help and this great class!