imap icon indicating copy to clipboard operation
imap copied to clipboard

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

Open Marlight opened this issue 7 years ago • 10 comments
trafficstars

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

Marlight avatar Mar 09 '18 18:03 Marlight

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?

Slamdunk avatar Mar 12 '18 08:03 Slamdunk

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' => '[email protected]',
'to' => 
array (
0 => 
stdClass::__set_state(array(
'mailbox' => 'maiksmith',
'host' => 'gmx.de',
)),
),
'reply_toaddress' => '[email protected]',
'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: <[email protected]>
Date: Mon, 12 Mar 2018 09:12:14 +0000
Message-Id: 
Precedence: bulk
To: [email protected]
Subject: Gigmanager: Test
From: Maik Smith - Gigmanager
Reply-To: [email protected]
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 ....

Marlight avatar Mar 12 '18 09:03 Marlight

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.

Slamdunk avatar Mar 12 '18 10:03 Slamdunk

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

Marlight avatar Mar 12 '18 12:03 Marlight

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

Marlight avatar Mar 12 '18 12:03 Marlight

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

Slamdunk avatar Mar 12 '18 13:03 Slamdunk

  1. Email email_with_problem.txt private E-Mail (mailbox) changed to maiksmith

$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

Marlight avatar Mar 12 '18 13:03 Marlight

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] => <[email protected]>
    [toaddress] => [email protected]
    [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] => [email protected]
    [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.

Slamdunk avatar Mar 12 '18 13:03 Slamdunk

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.

Marlight avatar Mar 12 '18 13:03 Marlight

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

Slamdunk avatar Dec 04 '18 10:12 Slamdunk