ddeboer / imap

Object-oriented, fully tested PHP IMAP library
MIT License
888 stars 254 forks source link

Fatal error: Call to a member function getName() on null #313

Open Marlight opened 6 years ago

Marlight commented 6 years ago

Hi,

If no e-mail address is stored at the sender of the e-mail, the following error is output. Fatal error: Call to a function member getName () on null

Example: ... From: Maik Smith ...

The problem is that imap_headerinfo() does not create an array entry.

My idea to solve the problem, but it still has small errors:

        if(!array_key_exists('from', $headers)){
            $headers['from'] = NULL;
            $head = \imap_fetchheader($stream, $number, \FT_UID);
            if(preg_match_all("/From\: (.*)/i", $head, $output_array)){
                $headers['fromadress'] = NULL;
                $headers['from'][0] = (object)["personal" => str_replace(array("\r", "\n"), '', $output_array[1][0]), "host" => NULL, "mailbox" => NULL];
            }
        }

Notice: Unknown: Must use comma to separate addresses: xxxxx (errflg=3) in Unknown on line 0

Sry for my english. Greetings from Germany Marius

Slamdunk commented 6 years ago

Hi, running a test against a From: Maik Smith header it passes and returns a regular Ddeboer\Imap\Message\EmailAddress. If no From is specified, a null is return, as shown in the method signature. So as far as I can see there is no bug here.

May you post the original raw email content that gave you the bug?

Marlight commented 6 years ago

Hi, so i use php5.6 because any scripts on my server don't run on php7.

I have make a test mail with Maik Smith and the Problem ist the same. That returns imap_headerinfo:

stdClass::__set_state(array(
'date' => 'Mon, 12 Mar 2018 09:12:14 +0000',
'Date' => 'Mon, 12 Mar 2018 09:12:14 +0000',
'subject' => 'Gigmanager: Test',
'Subject' => 'Gigmanager: Test',
'message_id' => '',
'toaddress' => 'maiksmith@gmx.de',
'to' => 
array (
0 => 
stdClass::__set_state(array(
'mailbox' => 'maiksmith',
'host' => 'gmx.de',
)),
),
'reply_toaddress' => 'maiksmith@web.de',
'reply_to' => 
array (
0 => 
stdClass::__set_state(array(
'mailbox' => 'maiksmith',
'host' => 'web.de',
)),
),
'Recent' => ' ',
'Unseen' => ' ',
'Flagged' => ' ',
'Answered' => ' ',
'Deleted' => ' ',
'Draft' => ' ',
'Msgno' => '6322',
'MailDate' => '12-Mar-2018 09:12:14 +0000',
'Size' => '4288',
'udate' => 1520845934,
))

And the raw header is:

Return-Path: 
Received: from mout.kundenserver.de ([212.227.126.187]) by mx-ha.gmx.net
(mxgmx115 [212.227.17.5]) with ESMTPS (Nemesis) id 1MfIEF-1eSbiv1cbL-00gUSj
for ; Mon, 12 Mar 2018 10:12:14 +0100
Received: from infong-uk70.kundenserver.de ([217.160.62.15]) by
mrelayeu.kundenserver.de (mreue006 [172.19.35.7]) with ESMTPA (Nemesis) id
0Mf6wH-1fJkF5105h-00OXzs for ; Mon, 12 Mar 2018 10:12:14
+0100
Received: from 88.208.169.246 (IP may be forged by CGI script)
by infong-uk70.kundenserver.de with HTTP
id zLd7Nf-1eIBKB0s7d-00moC8; Mon, 12 Mar 2018 09:12:14 +0000
X-Sender-Info: <676150706@infong-uk70.kundenserver.de>
Date: Mon, 12 Mar 2018 09:12:14 +0000
Message-Id: 
Precedence: bulk
To: maiksmith@gmx.de
Subject: Gigmanager: Test
From: Maik Smith - Gigmanager
Reply-To: maiksmith@web.de
Mime-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Mailer: PHP v5.6.34

Without my little code come this: Fatal error: Call to a member function getName() on null ....

Slamdunk commented 6 years ago

Hi, so i use php5.6 because any scripts on my server don't run on php7.

PHP 5.6 isn't supported by this library, please upgrade PHP to a newer version and try again to run your script.

Marlight commented 6 years ago

I have updated my server to PHP7, but the problem still exists 1: 1.

Marlight commented 6 years ago

Fatal error: Uncaught Error: Call to a member function getName() on null in [...] Stack trace: #0 [...]index.php(311): include() #1 {main} thrown in [...]test.php on line 130

has changed slightly

Slamdunk commented 6 years ago

In order to help you I need:

  1. the exact email raw content that caused the issue
  2. the exact script you are using
  3. the imap server specification you are interacting with
Marlight commented 6 years ago
  1. Email email_with_problem.txt private E-Mail (mailbox) changed to maiksmith

  2. 
    $server = new webmailer\Server("imap.gmx.net");
    $connection = $server->authenticate('[email]@gmx.de', 'Password');
    $mailboxes = $connection->getMailboxes();

$mailbox = $connection->getMailbox('INBOX');

$today = new DateTimeImmutable(); $lastMonth = $today->sub(new DateInterval('P30D'));

$messages = $mailbox->getMessages(new webmailer\Search\Date\Since($lastMonth),\SORTDATE,true);

$tpl = file_get_contents('./system/tpl/webmail/table_mail_entrie.tpl'); foreach ($messages as $message) { $sender = NULL; $sender = !is_null($message->getFrom()->getName()) ? $message->getFrom()->getName() : $message->getFrom()->getFullAddress(); $tmp = str_replace('{SUBJECT}', nl2br(var_export($message->getRawHeaders(), true)), $tpl); $tmp = str_replace('{SENDER}', $sender, $tmp); $tmp = str_replace('{TIME}', $message->getDate()->format('d.m.Y H:i'), $tmp); echo $tmp; }



3. https://hilfe.gmx.net/pop-imap/imap/imap-serverdaten.html
Slamdunk commented 6 years ago

This is the dump I get of imap_headerinfo for the email you provided:

stdClass Object
(
    [date] => Mon, 12 Mar 2018 09:12:14 +0000
    [Date] => Mon, 12 Mar 2018 09:12:14 +0000
    [subject] => Gigmanager: Test
    [Subject] => Gigmanager: Test
    [message_id] => <zLd7Nf-1eIBKB0s7d-00moC8@infong-uk70.kundenserver.de>
    [toaddress] => maiksmith@gmx.de
    [to] => Array
        (
            [0] => stdClass Object
                (
                    [mailbox] => maiksmith
                    [host] => gmx.de
                )

        )

    [fromaddress] => Maik Smith - Gigmanager <MISSING_MAILBOX@MISSING_DOMAIN>
    [from] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Maik Smith - Gigmanager
                    [mailbox] => MISSING_MAILBOX
                    [host] => MISSING_DOMAIN
                )

        )

    [reply_toaddress] => maiksmith@web.de
    [reply_to] => Array
        (
            [0] => stdClass Object
                (
                    [mailbox] => maiksmith
                    [host] => web.de
                )

        )

    [senderaddress] => Maik Smith - Gigmanager <MISSING_MAILBOX@MISSING_DOMAIN>
    [sender] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Maik Smith - Gigmanager
                    [mailbox] => MISSING_MAILBOX
                    [host] => MISSING_DOMAIN
                )

        )

    [Recent] => N
    [Unseen] =>
    [Flagged] =>
    [Answered] =>
    [Deleted] =>
    [Draft] =>
    [Msgno] =>    1
    [MailDate] => 12-Mar-2018 14:19:22 +0100
    [Size] => 4292
    [udate] => 1520860762
)

I think that the missing From is a defect of GMX.

I can't change the codebase without a proper test, it may break the current functioning behaviour.

In the meangime I suggest you to avoid directly calling $message->getFrom()->getName() and instead call only $message->getFrom() first and then check for it to be null or not.

Marlight commented 6 years ago

Okay, that's stupid, of course. So I do not get the error issued but also no name. How could I get the name that is obviously present in the header? With my little code I came to the name but has also produced a small notice.

Slamdunk commented 5 years ago

Try using \imap_rfc822_parse_headers($message->getRawHeaders())